How to set up a modern web stack in Ubuntu

In this post, we will discuss the LERP (Linux, €nginx, Redis, and PHP) stack. With it, all your web needs will be provided for. This chapter will guide you on how to set up the stack, and then create an empty playground having all of the necessary assets needed for experimentation, learning, and building. The set up will be ready for use in a production environment.

My assumption is that you currently have Ubuntu installed on your system. If this is not the case, then download its ISO, and then prepare a bootable media which you will use for installation of the OS. Once the installation process completes, just execute the following command:

The above command will serve to update your system.

Learn how to use Devops, from beginner basics to advanced techniques, with online video tutorials taught by industry experts. Enroll for Free DEVOPS Training Demo!

The latest version of the LTS is highly recommended, due to its strong support and increased stability.

Nginx (the server)

Nginx can be found from the launch pad

You can now use the following command so as to create a repository, and then refresh the software resources used on your system:

The install command can be issued as follows:

With the above command, the nginx stable will be installed on your system. If you need to do a verification of this, you can open your browser, and then type in the IP address of your server. The output should be the welcome file for nginx. If the server is being used simply for local development, then you can use the IP address “”.

PostgreSQL (The Server)

With Ubuntu, this kind of database comes installed in the system. To verify whether this is the case in your system, just run the following command on your terminal:

To get the latest version of this, one can add the latest version of the postgreSQL APT repository as shown below:

This should be followed by these commands:

Finally, you can then install the PostreSQL version 9.4 as shown in the figure given below:

It will be good if all of the users install the latest version of the above for stability and strong support. Since here we are working with IT experts, the command line will be a good tool. This is why we are not going to discuss how to install the “pgAdmin” which is a graphical utility for this kind of database.

Configuration of PostgreSQL

By default, a user-group named “postgres” will be created in the Ubuntu system. However, you need to note that the user in this case will be a superuser, and this is why it is not recommended to use this. This is because they are capable of carrying out any task on the database. Our aim is to create a new user and a database. The user should also be in a position to login, posses a password, and then possess privileges which should allow him or her to access the newly created database only.

Frequently asked Devops Interview Questions

The concept of roles is supported in PostgreSQL rather the concept of users and user groups. We
should begin by a login to the default account, that is, postgres, and then create a new database and a new role from the account. The login can be done as follows:

The above command will log you into the default account. Change the password for the account by use of the following command:

After executing the above command, a prompt will be prompted asking you to provide the new password. You will also be prompted to confirm it. The creation of the new database can now be done, and the normal SQL commands can be used as shown below:

We can now create a role on the database, and this will have some limited privileges and this will allow us to perform our operations on the database.

With the above command, a new role will be created for the username which you specify. In the case of the password, it must be written inside the single quotes. Note that the username in the above case should be similar to the one of your computer. This is how users are managed in postgreSQL. The last step should be to provide the user with some privileges, so that they can be able to operate on the newly created database. This can be done by use of the command given below:

After the above step, your PostgreSQL will be ready for use, since you will be through with the

Redis (The cache)

It is easy for us to install Redis. With the Ubuntu Trusty repositories, the latest version of this is already available. Just run the command given below:

It is after execution of the above command that you can begin to play around with the Redis. Begin by launching the “redis-cli” program from your computer’s terminal. This can be done by executing the following command on the terminal:

Learning Redis is very easy, and very powerful. If you do not know how to use it, you can consult the tutorials which are available online.

PHP (The Language)

The purpose of the nginx server is to receive all of the requests which are incoming. However, it is unable to process the PHP scripts which are stored in the server. However, “Common Gateway Interface” is used in this case. The gateway works by routing all of the requests from the nginx to the PHP engine which is responsible for processing of the script.

We should begin by installing the “PHP-fpm” package by running the command given below:

The installation of the php5-common package will be installed automatically, and this will be responsible for allowing the PHP scripts to be parsed. Before continuing to begin routing of the requests, there are two additional packages which you should install, and they will help you in the process of development. These are “php5-cli” and “php5-xdebug.” These can be installed by running the following command:

Once you execute the above command, you will be done. You can now test the command line of the PHP interactively by executing the following command:

You will be notified that the interactive mode has been enabled. You can then run the “Hello world” example to see if it will run. This is shown in the figure given below:

That shows that our set up was successful.

Stitching together the pieces

Now that all of the subcomponents of our web stack have been installed, we need to make them work together. We should begin by installation of the PostgreSQL drivers for PHP, and these will facilitate the connection between the PHP and the PostgreSQL database. Just run the following command, so as to perform the installation of these drivers:

The presence of the PostgreSQL PDO Driver in the system can now be tested by writing the following code:

php -r ‘var_dump(PDO::getAvailableDrivers());’
array(1) {
[0] =>
string(5) “pgsql”

We can now set up the nginx so that it can begin to route the requests via the FastCGI to PHP-FPM. Note that the configuration of the nginx has been divided into two parts, one of which is the global configuration, and the other one is a default configuration. The config files of your system have to be stored in the directory “sites-enabled” only. However, the default setting is for all of the files to be stored in this directory and each of the configuration file in this directory will contain the config of a single website. A simple config file looks as follows:

server {
listen 80 default_server;
root /usr/share/nginx/html;
index index.php index.html index.htm;
server_name localhost;
location / {
try_files $uri $uri/ =404;
location ~ .php$ {
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;

The keyword “listen” has been used to enable the system to listen to the port number 80, which is the default server. The “root” keyword has been used to define where the scripts for the website will be placed. This indicates that all of the files stored in the directory “/usr/share/nginx/html” will be accessible from the outside.

The keyword “index” will be used for defining the entry point for the website. This means that it will form the first file in the directory of the website. If this file is not found, then the next file will be matched. The keyword “localhost” has been used for the purpose of the local development. If the development is not done locally, then the URL of the server should be added here rather than the “localhost.”

You can then save the file in the directory “/etc/nginx/sites-enabled” and then give it the name “server.conf.” Once you are done, restart the nginx server by executing the following command:

You can next navigate to the directory “/usr/share/nginx/html” and then create a file named “index.php.” The following code should be added to the file:

The file can then be saved, and a navigation to the url http://localhost done. This should give you the PHP info page.

At this point, only one thing will be remaining, which is using the Redis with PHP. For this to be done, a Redis Client library together with PHP has to be used. The installation of this will depend, and it will be determined by the client that you choose.

Explore Devops Sample Resumes! Download & Edit, Get Noticed by Top Employers!Download Now!



Get Updates on Tech posts, Interview & Certification questions and training schedules