How to deploy a PHP project using Nginx?

To deploy a PHP project using Nginx, follow these steps:

  1. 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.
  2. 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:
  3. 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;
    }
    }

  4. 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.
  5. 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:
  6. Adjust the PHP-FPM version according to your specific situation.
  7. Restart the Nginx and PHP-FPM services by executing the following command to apply the configurations.
  8. Restart Nginx and PHP-FPM using systemctl.
  9. 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!

广告
Closing in 10 seconds
bannerAds