How to deploy a PHP project using Nginx?
To deploy a PHP project using Nginx, follow these steps:
- To install Nginx, you can use package managers like apt or yum, or compile and install the source code downloaded from the official Nginx website.
- Configure Nginx: Open the Nginx configuration file (usually located in /etc/nginx/nginx.conf or /etc/nginx/conf.d/default.conf) and add the following content:
- server {
listen 80;
server_name your_domain.com;root /path/to/your/project;
index index.php index.html index.htm;location / {
try_files $uri $uri/ /index.php?$query_string;
}location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock; #Modify PHP-FPM location as needed
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
} - In the above configuration, replace “your_domain.com” with the domain name or IP address where you want to deploy your project, and replace “/path/to/your/project” with the root directory of your project.
- Configure PHP-FPM: Open the configuration file for PHP-FPM (usually located at /etc/php/php-fpm.conf or /etc/php-fpm.d/www.conf) and make sure the following settings are correct:
- Adjust the PHP-FPM version according to your specific situation.
- Restart the Nginx and PHP-FPM services by executing the following command to apply the configurations.
- Restart Nginx and PHP-FPM using systemctl.
- Deployment testing: Access your domain or IP address in a browser, if everything is working correctly, you should be able to see the homepage of your PHP project.
I hope the above steps can help you successfully deploy your PHP project!