How to Install nginx and configure it for CakePHP

| By Webner

How to Install nginx and configure it for CakePHP

Quick Summary:

1. What is Web Server?
2. What is Nginx?
3. Nginx Installation.
4. Create server block (VirtualHost).
5. Install CakePHP and configure it for Nginx.
6. How to check CakePHP version.

What is Web Server?

Web Server is a software program which delivers web contents (like web pages, web applications, web files etc.) over the internet to user who made the request for that specific content. It uses HTTP (Hypertext Transfer Protocol) to serve web contents. HTTP refers to the set of rules that are agreed upon between two or more parties for communication.

Web Server Operation

Famous Web Servers:

1. Apache
2. IIS (Internet Information Server by MicroSoft)
3. Nginx
4. GWS (Google Web Server)
All Web Servers have their unique properties and performance criteria. In this post we’ll discuss about Nginx (pronounced engine X) web server.

What is Nginx?

Nginx is high performance HTTP and proxy server or open source web server. Nginx an alternative to Apache web server. Nginx it is better in term of performance, it takes less memory under high load and serves faster.

Nginx is the reverse proxy server.

Nginx Installation

My system configurations are:

Operating System: Ubuntu 14.04
Architecture: 64 bit
PHP version: PHP 7.2.4

Installation of Nginx is very simple process which would not take more than 2 minutes. You must have non-root access with sudo privileges to complete this installation. This installation is for Debian based operating system Ubuntu 14.04. Installation steps are given below:

Step 1: Update Packages

Update the list of available packages in repository by the following command:

:~$ sudo apt-get update

Step 2: Install Nginx

Install the nginx web server on your ubuntu machine with this command:

:~$ sudo apt-get install nginx   

Step 3: Verification

Now Nginx has been installed on your system. To verify use the below URL in your web browser:

http://server_ip_address or localhost

You will get Nginx home page like below:

You can check the service status by following command, I have pasted my result.

:~$ sudo /etc/init.d/nginx status
[sudo] password for test: *******
nginx.service – A high performance web server and a reverse proxy server
Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
Active: active (running) since Thu 2018-04-19 10:29:10 IST; 1 day 1h ago
Process: 1214 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
Process: 858 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
Main PID: 1217 (nginx)
CGroup: /system.slice/nginx.service
├─1217 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
└─1218 nginx: worker process

You can start/stop the Nginx service with the following commands:

:~$ sudo /etc/init.d/nginx start
:~$ sudo /etc/init.d/nginx stop

Now we need to create server block (Virtual Host) for nginx and configure CakePHP for it.

Create server block (VirtualHost)

First of all we need to create a server block (VirtualHost in Apache) for Nginx server where we need to configure CakePHP. I have written quick steps below:

1. Create the parent directory for block:

:~$ sudo mkdir -p /var/www/admin.local/html

2. Give the ownership to user:

:~$ sudo chown -R $USER:$USER /var/www/admin.local/html

3. Confirm the permission for /var/www it should be 755 or run following command:

~$ sudo chmod -R 755 /var/www

Install CakePHP and configure it for Nginx

PHP Package Requirements:

:~$ sudo apt-get install curl php7.2-cli git php7.2-zip php7.2-sqlite php7.2-intl php7.2-xml php7.2–mbstring php7.2-mysql

Create the project via COMPOSER dependency manager:

:~$ cd /var/www/admin.local/html
:~$ composer create-project –prefer-dist cakephp/app admincake
:~$ mv admincake/* ../html/

I created the project with name “admincake”

4. Now create server block file and configure it for CakePHP:

~$: sudo vi /etc/nginx/sites-available/admin.local

You can remove default file, If you don’t need it.

~$: sudo rm -rf /etc/nginx/sites-available/default

Now, copy the following code and paste it on your server block file (admin.local).

server {
    listen   80;
    server_name admin.local;
    rewrite 301 http://admin.local$request_uri permanent;
    root   /var/www/admin.local/html/webroot;

location / {
		index index.html index.htm index.php;
    location ~ \.php$ {
        fastcgi_pass unix:/run/php/php7.2-fpm.sock;
        fastcgi_index index.php;
        include fastcgi.conf;

You need to enable server block file by creating the symlink of “admin.local” to sites-enabled directory. Use following command:

:~$ sudo ln -s /etc/nginx/sites-available/admin.local /etc/nginx/sites-enabled/

Now edit your HOSTS file

:~$ sudo nano /etc/hosts localhost
192.168.x.x admin.local

Save the file and restart the Nginx Service, Command is:

:~$ sudo /etc/init.d/nginx restart

Now, CakePHP has been configured for your local server. For the verification goto your web browser and type your IP address and you will get following page on your screen.

In above screenshot in “Database” section, It say ‘CakePHP is NOT able to connect to the database’. Run following steps to solve it.

Open terminal and login to your MySql server.

:~$ mysql -u root -p

After login to mysql run following commands

Configure the app.php file, check below:

:~$ sudo nano config/app.php

And edit the following section as given below screenshot:

Now you are able to connect to the database.

All Done !!


How to check CakePHP version.

You can check CakePHP version through “VERSION.txt” text file. Check below:

:~$ cat /var/www/admin.local/html/vendor/cakephp/cakephp/VERSION.txt

// +——————————————————————————————–+ //
// CakePHP Version
// Holds a static string representing the current version of CakePHP
// CakePHP(tm) : Rapid Development Framework (
// Copyright (c) Cake Software Foundation, Inc. (
// Licensed under The MIT License
// Redistributions of files must retain the above copyright notice.
// @copyright Copyright (c) Cake Software Foundation, Inc. (
// @link
// @since CakePHP(tm) v 0.2.9
// @license MIT License
// +——————————————————————————————–+ //


The CakePHP version is 3.6.2.

Leave a Reply

Your email address will not be published. Required fields are marked *