How to install PHP in nginx?

Member

by mac , in category: PHP General , 10 months ago

How to install PHP in nginx?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by dedrick , 5 months 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.