Two Ways To Run Nginx With PHP

When installing both Nginx and PHP on a VPS successfully, how to combine them and run Nginx with PHP support?

As mentioned before, for Nginx, you just need to manage the nginx.conf file to run whatever you want to, however, there are two different ways for you to mange that file:

Way 1. Run Nginx with PHP directly

Enter the following command to open the nginx.conf file in your Terminal:

vi /usr/local/nginx/conf/nginx.conf

And enter "dG" to delete all the contents, then press the "I" key and paste the following ones:


user www-data www-data;
worker_processes 2;

events {
worker_connections 1024;
}

http {
include mime.types;
default_type application/octet-stream;

sendfile on;
keepalive_timeout 65;

server {
listen 80 default;
server_name localhost;

access_log /usr/local/nginx/logs/test.access.log;
root /usr/local/nginx/html;

location / {
index index.html index.htm index.php;
}

location ~ \.php$ {
include /usr/local/nginx/conf/fastcgi.conf;
fastcgi_pass localhost:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
}

Change the path of access_log, root and include if put them somewhere else, press the "ESC" key and enter ":wq" to save the file, then you can run PHP webpages on your VPS.

Way 2. Run Nginx with PHP indirectly

Another popular way to run Nginx with PHP is to create a "sites-enabled" folder with the sites you want to run with PHP webpages, such as to run a WordPress blog.

Step 1. Edit Nginx.conf

Enter the following command to open the nginx.conf file in your Terminal:

vi /usr/local/nginx/conf/nginx.conf

And enter "dG" to delete all the contents, then press the "I" key and paste the following ones:


user www-data www-data;
worker_processes 2;

events {
worker_connections 1024;
}

http {
include mime.types;
default_type application/octet-stream;

sendfile on;
keepalive_timeout 65;

include /usr/local/nginx/conf/sites-enabled/*;

}

Step 2. Create the "sites-enabled" folder

Comparing to the default Nginx.conf file, you can see that we only change the values of user and worker_processes, as well as add the following line:

include /usr/local/nginx/conf/sites-enabled/*;

The above command lets the nginx.conf file to run all the configuration files in a folder called "sites-enabled&quot, which you can create by entering the following command:

mkdir /usr/local/nginx/conf/sites-enabled

In fact, you can create the folder anywhere you like.

Step 3. Create a test configuration file

Enter the following command:

nano /usr/local/nginx/conf/sites-enabled/test

And paste the following content:


server {
listen 80;
server_name localhost;

access_log /usr/local/nginx/logs/test.access.log;
root /usr/local/nginx/html;

location / {
index index.html index.htm index.php;
}

location ~ \.php$ {
include /usr/local/nginx/conf/fastcgi.conf;
fastcgi_pass localhost:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}

Save the file, then you can run Nginx with PHP support.

Test if those two ways work

To check if the above two ways work, we can create a sample PHP webpage. Enter the following command:

nano /usr/local/nginx/html/test.php

And enter the following content:

<?php phpinfo() ?>

Save it and open your.vps.ip/test.php in your browser, if you can see the standard PHP info page like the following:

Run Nginx with PHP

Then your Nginx does run with PHP support.

Inclusion

1. Comparing to above two ways, we can see that Way 1 puts all things in one file, and Way 2 puts one thing in two files.

2. Comparing to the default contents, the new contents of the nginx.conf file have changed the following two lines:

#user nobody;
worker_processes 1;

to

user www-data www-data;
worker_processes 2;

User is the ID that can run the server, and it should be same as PHP‘s, while worker_processes is for CPUs or cores, and you can keep it as default. For more details, you can check out the CoreModule page.

3. For the contents used in the "test" file, you can check out Nginx for more details.

Related free web apps:

  1. How To Install Nginx On A VPS
  2. How To Install WordPress On A VPS
  3. How To Install phpMyAdmin On A VPS
  4. An Easier Way To Install PHP On A VPS
  5. How To Install PHP On A VPS

This entry was posted in Other Free Nuts and tagged , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.
Post comment as twitter logo facebook logo
Sort: Newest | Oldest