To install PHP with Nginx, you’ll need to follow a few steps. Here’s a general guide for setting up PHP with Nginx on a Linux-based system. Please note that specific commands might vary depending on your distribution.
1. Install Nginx:
sudo apt-get update
sudo apt-get install nginx
For other distributions, you can use the package manager specific to that distribution.
2. Install PHP:
Install PHP and the PHP FastCGI Process Manager (PHP-FPM). PHP-FPM is a process manager to manage PHP processes.
sudo apt-get install php-fpm
3. Configure Nginx to use PHP:
Edit the Nginx default site configuration file. This file is typically located in /etc/nginx/sites-available/default
.
sudo nano /etc/nginx/sites-available/default
Add or modify the following lines inside the server
block to enable PHP processing:
server {
# ... other configurations ...
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock; # Adjust the version number based on your PHP version
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
# ... other configurations ...
}
Save the file and exit.
4. Restart Nginx:
After making changes to the Nginx configuration, restart the Nginx service to apply the changes:
sudo service nginx restart
5. Test PHP:
Create a test PHP file to ensure that PHP is working correctly with Nginx. For example, create a file named info.php in your web root directory (usually /var/www/html):
<?php
phpinfo();
?>
Open a web browser and navigate to http://your_server_ip/info.php
. You should see the PHP information page.
Remember to replace your_server_ip
with the actual IP address or domain of your server. Additionally, make sure that the PHP-FPM socket path matches the version you have installed.
This is a basic setup, and you may need to adjust configurations based on your specific requirements or server environment.