Set Up the Web Server

Nginx server

Set Up the Web Server

Now that we have verified that our web server can access the WordPress database, we need to actually make this a web server by configuring Nginx, PHP and the necessary components.

Since you updated your package index to run the test above, we don’t need to do that again. Let’s install all of the packages we need:

sudo apt-get install nginx php5-fpm php5-mysql

 

When everything is installed, you can begin configuring the software.

Configure PHP

Let’s begin with PHP since that is quite easy.

Open the PHP configuration file for php-fpm, which will handle our dynamic content. We just need to modify one value in this:

sudo nano /etc/php5/fpm/php.ini

 

Search for the cgi.fix_pathinfo parameter. It will likely be commented out with the “;” character and set to “1”. We need to reverse both of these conditions by uncommenting the line and setting it to “0”:

cgi.fix_pathinfo=0

 

Set Up the Web Server

 

This is a security measure. By setting this option, we tell PHP not to try to guess the file that the user was trying to access if an exact match is not found. If we didn’t set this, a malicious user could take advantage of this opportunity and get our server to execute code that we don’t want it to.

Save and close the file when you are finished.

Next, we’ll need to open another file to modify how our PHP processor and web server communicate:

sudo nano /etc/php5/fpm/pool.d/www.conf

 

Look for the listen directive, which should be set to 127.0.0.1:9000. Rather than using a port, we’re going to set this to a unix domain socket:

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

 

Save and close the file when you are finished.

Now that we have our values, restart our PHP processor:

sudo service php5-fpm restart

 

Configure Nginx

Now we’re ready to configure Nginx. We can start by copying the default virtual host file to a new file that we can work with. We’ll name this after the domain of our site. I’m going to use the placeholder “example.com”:

sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/example.com

 

Now, open the file we just copied over:

sudo nano /etc/nginx/sites-available/example.com

 

Inside, we’ll modify our server block (the sections contained within the server brackets). Begin by uncommenting the directive to listen to port 80. We’re also going to change the root directory and make Nginx serve a PHP index file by default:

server {
    listen 80;
    root /var/www/example.com;
    index index.php index.hmtl index.htm;

 

Next, we’ll modify the server_name directive to use our domain name, ensure that our try_files is set up correctly (passing requests to PHP if no files are found) and that our error pages are configured:

server {
    listen 80;
    root /var/www/example.com;
    index index.php index.hmtl index.htm;
    server_name example.com;
    location / {
        try_files $uri $uri/ /index.php?q=$uri&$args;
    }
    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /usr/share/nginx/www;
    }

 

Finally, we need to set up the actual PHP processing by using a location block that will match all of our PHP requests. We will immediately return a 404 if an exact match is not found. We’ll also use the socket we configured for PHP:

server {
    listen 80;
    root /var/www/example.com;
    index index.php index.hmtl index.htm;
    server_name example.com;
    location / {
        try_files $uri $uri/ /index.php?q=$uri&$args;
    }
    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /usr/share/nginx/www;
    }
    location ~ .php$ {
        try_files $uri =404;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
    }
}

 

This is the end of our server block configuration. Save and close the file.

Now, we link this to our “enabled” directory and remove the link for the default server block file:

sudo rm /etc/nginx/sites-enabled/default
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/

 

Restart Nginx to make these changes happen:

sudo service nginx restart

 

(www.digitalocean.com)