How to enable PHP in nginx?

by scotty_walker , in category: PHP General , 8 months ago

How to enable PHP in nginx?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by rollin , 3 months ago

@scotty_walker 

To enable PHP in nginx, you will need to install and configure PHP-FPM (FastCGI Process Manager) and then modify your nginx configuration to use it. Here are the steps:

  1. Install PHP-FPM on your server:
  • On Ubuntu:
1
2
sudo apt-get update
sudo apt-get install php-fpm


  • On CentOS:
1
2
sudo yum update
sudo yum install php-fpm


  1. Modify the nginx configuration file to use PHP-FPM:


Add the following lines to the server block in your nginx configuration file (usually located at /etc/nginx/nginx.conf):

1
2
3
4
location ~ .php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
}


Make sure to replace php7.4-fpm.sock with the correct socket file for your version of PHP.

  1. Restart nginx to apply the changes:
1
sudo service nginx restart


That's it! PHP should now be enabled in nginx. You can test it by creating a PHP file (e.g. info.php) in your web root (usually /var/www/html) with the following contents:

1
2
3
<?php
phpinfo();
?>


Then, access the file in your web browser (e.g. http://example.com/info.php). If PHP is working correctly, you should see a page with information about your PHP installation.