Mondoze Knowledge Base

Search our articles or browse by category below

Configuring Nginx With PHP On Linux VPS Server In 2 Steps

Last modified: October 7, 2022
You are here:
Estimated reading time: 1 min

In this tutorial, we will be guiding you on how to configure Nginx, a web server application with PHP, a scripting language for your Linux VPS server. VPS users may find this useful, especially those who use LEMP stack.

Step 1: Create Config File

Firstly, make sure that both Nginx and PHP are installed in your VPS. The version of the PHP used in this tutorial is PHP 7, and the VPS here used CentOS.

Then we will need to create an Nginx configuration file. Create the file and run it on nano text editor using the following command below.

nano /etc/nginx/conf.d/default.conf

After that, within the editor, insert the following code into the configuration file.

server {
    listen 80;
    server_name server_ip;

    root 
    /usr/share/nginx/html;
    index index.php index.html index.htm;

    location / {
        try_files $uri $uri/ =404;
    }
    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /usr/share/nginx/html;
    }

    location ~ .php$ {
        try_files $uri =404;
        fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

Do note that you have to replace the “server_ip” with your server IP address in line 3. Save the file and reboot Nginx using the following command for the changes to take effect.

systemctl restart nginx

Step 2: PHP Configuration

The next step is to change some of the lines for php configuration file. To do so, open the following file using nano text editor with the command below.

nano/etc/php-fpm.d/www.conf

After that, change the following list of line to the new list of lines shown below.

  • “user = apache” => “user = nginx”
  • “group = apache” => “group = nginx”
  • “listen.owner = nobody” => “listen.owner = nginx”
  • “listen.group = nobody” => “listen.group = nginx”

After finish replacing the above variables to nginx add the following line under the line “;listen = 127.0.0.1:900”.

listen = /var/run/php-fpm/php-fpm.sock

Finally, save the file with Ctrl+X. Then enable PHP-FPM to start on boot using the following commands.

systemctl start php-fpm.service
systemctl enable php-fpm.service
Was this article helpful?
Dislike 0
Views: 84