Step-by-Step Guide
- Update the Package Index
Open a terminal and update the package index to ensure you have the latest information about available packages:
sudo apt update
- Install PHP
Install PHP along with some common PHP modules using theapt
package manager:
sudo apt install php-fpm php-mysql
This command installs PHP-FPM (FastCGI Process Manager) and the PHP MySQL extension.
- Verify PHP Installation
Check the PHP version to ensure it is installed correctly:
php -v
You should see the PHP version information displayed.
- Install Nginx
Install Nginx using theapt
package manager:
sudo apt install nginx
- Configure PHP with Nginx
Open the Nginx default server block configuration file for editing:
sudo nano /etc/nginx/sites-available/default
Modify the server block to use PHP. Replace the existing content with the following configuration:
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
index index.php index.html index.htm index.nginx-debian.html;
server_name _;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
Save and close the file. Note that the fastcgi_pass
directive should match the PHP-FPM socket file for your PHP version. The example uses php7.4-fpm.sock
for PHP 7.4.
- Restart Nginx and PHP-FPM
Restart Nginx and PHP-FPM to apply the changes:
sudo systemctl restart nginx
sudo systemctl restart php7.4-fpm
- Test PHP with Nginx
To test PHP with Nginx, create a new PHP file in the web root directory:
sudo nano /var/www/html/info.php
Add the following PHP code to the file:
<?php
phpinfo();
?>
Save and close the file.
- Access the PHP Info Page
Open your web browser and visithttp://your_server_ip/info.php
. You should see the PHP information page, indicating that PHP is working correctly with Nginx. - Install Additional PHP Modules (Optional)
You can install additional PHP modules as needed. To search for available PHP modules, use:
sudo apt-cache search php-
To install a specific PHP module, use:
sudo apt install php-module_name
Replace module_name
with the name of the module you wish to install.
- Configure PHP Settings (Optional)
You can configure PHP settings by editing thephp.ini
file. The location of thephp.ini
file may vary depending on your PHP version. For PHP 7.4, the file is typically located at/etc/php/7.4/fpm/php.ini
:sudo nano /etc/php/7.4/fpm/php.ini
After making changes, restart PHP-FPM to apply them:sudo systemctl restart php7.4-fpm
- Remove the PHP Info Page
For security reasons, it’s a good idea to remove the PHP info page after testing:bash sudo rm /var/www/html/info.php