6 Steps to Configure Virtual Host and CakePHP in Nginx Webserver


The use of shared hosting is limited designers and developers for creativity, time to move to a VPS or dedicated server. If you are a beginner, meaning that so far only use shared hosting, such as hostgator and bluehost, we recommend using the VPS. Technically, VPS is similar to a dedicated server, it’s just a different ability. If you want to learn, use the VPS.

I also am learning VPS. My goal is to use a web server Nginx + PHP FPM and MariaDB version 10. This is the system I wanted, because rarely shared hosting using Nginx web server. For the record, Nginx web server is a alternative web server for Apache and LiteSpeed  that have the ability to process requests faster and more memory efficient.

In this post I want to show you how to create a virtual web server hosting on Nginx. Maybe this is a personal record for me, so do not forget. You need to prepare is

  1. VPS hosting with OS Debian 6 or 7. I’m using version 7.
  2. Access as root
  3. And of course already installed Nginx, PHP FPM and MariaDB 10.x I will make this tutorial.

Ok, we started to just create a virtual host. In this experiment, I use a free domain from freenom. You can also get a free domain : cakephp.cf

1. Prepare Directory To Store Your Website Files

Sign in to SSH, and type this command

mkdir -p /srv/www/cakephp.cf/htdocs
mkdir -p /srv/www/cakephp.cf/logs

website files will be stored in the htdocs directory.

2. Creating Virtual Host in Nginx

cd /etc/nginx/sites-available/
touch cakephp.cf

3. Edit Your Virtual Host File

We use the nano editor to edit the configuration of the virtual host.

nano cakephp.cf

Then full fill of the configuration as below. For the case in this post, I want to use cakephp at this domain. Each virtual host configuration is different, depending on web technology you use.

server {
    listen   80;
    server_name www.cakephp.cf;
    rewrite ^(.*) http://cakephp.cf$1 permanent;

server {
    listen   80;
    server_name cakephp.cf;

	root /srv/www/cakephp.cf/htdocs/app/webroot/;
    index  index.php;

    access_log /srv/www/cakephp.cf/logs/access.log;
    error_log /srv/www/cakephp.cf/logs/error.log;

    # main cakephp rewrite rule
    location / {
        try_files $uri $uri/ /index.php?$uri&$args;

        location ~ .php$ {
            root           /srv/www/cakephp.cf/htdocs/app/webroot/;
            try_files $uri =404;
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
            fastcgi_buffer_size 128k;
            fastcgi_buffers 256 4k;
            fastcgi_busy_buffers_size 256k;
            fastcgi_temp_file_write_size 256k;


If you want to use wordpress, please use the setting as below

server {
       listen 80;
       #listen [::]:80 ipv6only=on default_server;
       server_name cakephp.cf;
       root /srv/www/cakephp.cf/htdocs;
    access_log /srv/www/cakephp.cf/logs/access.log;
    error_log /srv/www/cakephp.cf/logs/error.log;
       if ($http_host != "www.cakephp.cf") {
                 rewrite ^ http://www.cakephp.cf$request_uri permanent;
       index index.php index.html;
       location = /favicon.ico {
                log_not_found off;
                access_log off;
       location = /robots.txt {
                allow all;
                log_not_found off;
                access_log off;
 # Use gzip compression
 # gzip_static on; # Uncomment if you compiled Nginx using --with-http_gzip_static_module
 gzip on;
 gzip_disable "msie6";
 gzip_vary on;
 gzip_proxied any;
 gzip_comp_level 5;
 gzip_buffers 16 8k;
 gzip_http_version 1.0;
 gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript image/png image/gif image/jpeg;
        location ~ ^/wp-content/cache/minify/[^/]+/(.*)$ {
                try_files $uri /wp-content/plugins/w3-total-cache/pub/minify.php?file=$1;
 #location ~ ^/wp-content/plugins/wp-minify/min/[^/]+/(.*)$ {
 # try_files $uri /wp-content/plugins/w3-total-cache/pub/minify.php?file=$1;
 # wp-minify/cache
 # Don't cache uris containing the following segments
 if ($request_uri ~* "(\/wp-admin\/|\/xmlrpc.php|\/wp-(app|cron|login|register|mail)\.php|wp-.*\.php|index\.php|wp\-comments\-popup\.php|wp\-links\-opml\.php|wp\-locations\.php)") {
 set $cache_uri "no cache";
 # Don't use the cache for logged in users or recent commenters
 if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp\-postpass|wordpress_logged_in") {
 set $cache_uri 'no cache';
 # Deny all attempts to access hidden files such as .htaccess, .htpasswd, .DS_Store (Mac).
       location ~ /\. {
                deny all;
                access_log off;
                log_not_found off;
       location / {
                try_files $uri $uri/ /index.php?$args;
       # Add trailing slash to */wp-admin requests.
       rewrite /wp-admin$ $scheme:http://$host$uri/ permanent;
 # Cache static files for as long as possible - removed xml as an extension to avoid problems with Yoast WordPress SEO plugin which uses WP rewrite API.
 location ~* \.(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|css|rss|atom|js|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)$ {
 try_files $uri =404;
 expires max;
 access_log off;
 # Pass PHP scripts on to PHP-FPM
 location ~* \.php$ {
 fastcgi_split_path_info ^(.+\.php)(/.+)$;
 try_files $uri /index.php;
 fastcgi_index index.php;
 fastcgi_pass unix:/var/run/php5-fpm.sock;
 include fastcgi_params;
 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
 fastcgi_param SCRIPT_NAME $fastcgi_script_name;

4. Download and Install CakePHP

cd /srv/www/cakephp.cf/htdocs
wget https://github.com/cakephp/cakephp/zipball/2.6.2
unzip 2.6.2
cp -rvf cakephp-cakephp-ec00c2d/* .
rm -R cakephp-cakephp-ec00c2d
rm 2.6.2

5. Creating Symbolic Link to Nginx

ln -s /etc/nginx/sites-available/cakephp.cf /etc/nginx/sites-enabled/cakephp.cf

Symbolic links useful for Nginx know new virtual host.

6. Provide Access Rights to the World

chown -R www-data:www-data /srv/www/cakephp.cf/htdocs

Now, look at cakephp.cf, runs perfectly.

Note: To follow this tutorial, you’ve directed your DNS to cakephp.cf domain. I use cloudflare for free and fast.

Dont forget to restart nginx with command like this.

service nginx restart