Nginx is a high-performance HTTP and reverse proxy server that also supports IMAP/POP3 proxying. Due to its excellent concurrency handling and low resource usage, it has become an essential component in modern web services.
Installing Nginx on most Linux distributions is straightforward. For example, on Ubuntu, run the following commands:
sudo apt update sudo apt install nginx
After installation, start the Nginx service with:
sudo systemctl start nginx
Open a browser and enter your server's IP address. If you see the default Nginx welcome page, the installation was successful.
Next, install PHP and the required modules to support dynamic web pages:
sudo apt install php-fpm php-mysql
PHP-FPM (FastCGI Process Manager) significantly improves PHP performance. Edit the PHP-FPM configuration file and modify key parameters:
sudo nano /etc/php/7.x/fpm/php.ini
Find and set the following:
cgi.fix_pathinfo=0
Save the file and restart the PHP-FPM service:
sudo systemctl restart php7.x-fpm
Add PHP support configuration to the Nginx config file as shown below:
server { listen 80; server_name your-domain.com; 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:/run/php/php7.x-fpm.sock; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } }
Replace your-domain.com and the PHP version number with actual values. After saving, restart Nginx:
sudo systemctl restart nginx
To verify the setup, create a simple PHP info page:
echo "<?php phpinfo(); ?>" > /var/www/html/info.php
Then visit http://your-domain.com/info.php in your browser. If the PHP info page displays, the configuration is successful.
After the basic setup, it is recommended to further enhance server security and performance by disabling unnecessary PHP functions, enabling Nginx gzip compression, and setting access controls to ensure system stability and efficiency.
This article systematically explains how to install and configure Nginx and PHP on Linux, covering everything from initial setup to performance tuning, helping you quickly build a stable and efficient web server environment.