The number of choices to make when you create a new server are unlimited. And thats not always positive. Did you develop your whole website, created a server for it, deployed it and then found out that you after all prefer nginx instead of apache?

Well, don’t fear, cause we had some other reasons to switch, I made a quick walktrough. I might even add some help for rewriting your .htaccess ;-)

Here we go!

Of course, always update apt-get

sudo apt-get update

Lets already start the magic…. (we are not done then)

sudo apt-get install nginx

To keep Nginx clean and lean we also need to install a PHP handler as these are not standard included.

sudo apt-get install php5-fpm

At this moment we should look if nginx is working.
For this, you should edit the config file

sudo nano /etc/nginx/sites-available/default

Here you will find something like:

server {
        listen 80 default_server;
        listen [::]:80 default_server ipv6only=on;

Change it to:

server {
        listen 8000 default_server;
        listen [::]:8080 default_server ipv6only=off;

Now restart the server (or check first if there are now problems..)

nginx -t //check for errors
sudo service nginx restart

Navigate to yourdomain or yourip with port 8000 (eg http://www.mydomain.com:8000) and see if you have a confirmation page.

Nginx confirmation page

Nginx confirmation page

Now its time to change our server variables. I changed it to the following, this should be more than enough for a normal setup:

server {
    listen 8000;   # We're deliberately leaving this as-is to avoid conflict at the moment

    root /var/www;
    server_name your_site.com www.your_site.com;
    index index.php;    

    location / {
        try_files $uri $uri/ /index.html;
    }

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
    }

    location /doc/ {
        alias /usr/share/doc/;
        autoindex on;
        allow 127.0.0.1;
        deny all;
    }

    location ~/\.ht {
        deny all;
    }

}

We also need to configure PHP FPM.

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

There is a possibility that this already works, none the less you should find the following and change it.

cgi.fix_pathinfo=0 #originally this is 1, make it 0

Save the file and open the next

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

Find and change listen to the value that is in the server block configuration file:

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

Restart both:

sudo service php5-fpm restart
sudo service nginx restart

If you didn’t have any php files in your root it becomes time to do this and test them out. The same url applies, so be sure to use the 8000 port.

Nginx works!

We needed to change something further because we had an .htaccess. How? Well, read on… (this in this case only works for Laravel, but might work for more frameworks)

Adjust your server block config file.

sudo nano /etc/nginx/sites-available/default

 

Here is my  new server configuration:

server {
    listen 8000;   # We're deliberately leaving this as-is to avoid conflict at the moment

    root /var/www/html;
    server_name test.mydomain.nl www.test.mydomain.nl;

    index index.php test.php;
    location / {
        try_files $uri $uri/ /index.php?$query_string;

    }

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

    location /doc/ {
        alias /usr/share/doc/;
        autoindex on;
        allow 127.0.0.1;
        deny all;
    }

    location ~/\.ht {
        deny all;
    }

}

And there also Laravel works.

Good luck! With any question, please leave a comment ;-)