How to install PHP in nginx?

Member

by mac , in category: PHP General , 2 years ago

How to install PHP in nginx?

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers

Member

by dedrick , 2 years ago

@mac 

To install PHP in nginx, follow these steps:

  1. Make sure that you have nginx installed on your system. If you don't have it installed, you can install it by running the following command:
1
sudo apt install nginx


  1. Next, install PHP and the PHP-FPM (FastCGI Process Manager) package by running the following command:
1
sudo apt install php-fpm


  1. Once PHP is installed, you need to configure nginx to use it. To do this, open the default nginx configuration file in a text editor:
1
sudo nano /etc/nginx/sites-available/default


  1. In the file, you will see a block of configuration settings for the server directive. Within this block, add the following lines:
1
2
3
4
location ~ .php$ {
  include snippets/fastcgi-php.conf;
  fastcgi_pass unix:/run/php/php7.4-fpm.sock;
}


  1. Save the file and exit the text editor.
  2. Test the nginx configuration to make sure there are no syntax errors:
1
sudo nginx -t


  1. If the configuration test passes, restart the nginx server to apply the changes:
1
sudo systemctl restart nginx


That's it! PHP is now installed and configured in nginx. You can create PHP files in your web root directory (usually /var/www/html) and access them through your web browser.

by hal.littel , a year ago

@mac 

To install PHP in Nginx, you need to follow these steps:

  1. Update your system: Run the following command to update your system's package lists: sudo apt update
  2. Install PHP: Use the following command to install PHP and some commonly used PHP extensions: sudo apt install php-fpm php-mysql
  3. Configure PHP-FPM: Open the PHP-FPM configuration file using a text editor: sudo nano /etc/php/7.4/fpm/php.ini Inside the file, find the line that starts with cgi.fix_pathinfo and change its value to 0: cgi.fix_pathinfo=0 Save and close the file.
  4. Configure Nginx to use PHP: Open the default server block configuration file: sudo nano /etc/nginx/sites-available/default Inside the server block, find the line that serves PHP files and uncomment it by removing the # symbol. It should look like this: location ~ .php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/run/php/php7.4-fpm.sock; } Save and close the file.
  5. Restart PHP-FPM and Nginx: Use the following command to restart PHP-FPM service: sudo systemctl restart php7.4-fpm Then, restart Nginx: sudo systemctl restart nginx


PHP should now be installed and configured with Nginx. You can test it by creating a simple PHP file in the webroot directory (/var/www/html) and accessing it through a web browser.