To install Nginx and PHP on your server, follow the instructions based on your operating system:
On Ubuntu/Debian:
Step 1: Install Nginx
- Update package index:
sudo apt update
- Install Nginx:
sudo apt install nginx
- Start Nginx service:
sudo systemctl start nginx
- Enable Nginx to start on boot:
sudo systemctl enable nginx
- Check Nginx status:
sudo systemctl status nginx
Step 2: Install PHP
- Install PHP:
sudo apt install php-fpm
- Check PHP version:
php -v
On CentOS/RHEL:
Step 1: Install Nginx
- Install Nginx:
sudo yum install epel-releasesudo yum install nginx
- Start Nginx service:
sudo systemctl start nginxsudo systemctl enable nginx
Step 2: Install PHP
- Install PHP:
sudo yum install php php-fpm
- Start PHP-FPM service:
sudo systemctl start php-fpm
sudo systemctl enable php-fpm
Configure Nginx to Use PHP
- Edit Nginx configuration for your site (e.g.,
/etc/nginx/sites-available/default
):
sudo nano /etc/nginx/sites-available/default
- Update the
location
:
Update thelocation ~ \.php$
block with the correct PHP-FPM socket (or port):
server {
listen 80;
server_name your_domain_or_ip;
root /var/www/html;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php8.3-fpm.sock; # Ensure this matches the PHP-FPM socket path
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
# Other necessary Nginx configuration
}
- Test the Nginx configuration:
sudo nginx -t
- Reload Nginx to apply the changes:
sudo systemctl reload nginx
Verify PHP is Working
- Create a
phpinfo()
test file to verify PHP is working:
sudo nano /var/www/html/info.php
- Add the following PHP code:
<?php
phpinfo();
?>
- Visit the test file in your browser:
http://your_server_ip/info.php
