Install Nginx, MySQL, PHP (LEMP) in Ubuntu 14.04/13.10/12.04

Steps to Install Nginx, MySQL, PHP (LEMP) in Ubuntu

Introduction

LEMP stack is a group of open source software to get web servers up and running. The acronym stands for Linux, nginx (pronounced Engine x), MySQL, and PHP. Since the server is already running Ubuntu, the linux part is taken care of. Lemp Represents Linux operating system with the Nginx web server and hosted site data is stored in a MySQL database and dynamic content is processed by PHP.Here are steps how to install Nginx, Mysql, PHP (LEMP) in Ubuntu server while the most of the same process can use for Centos.

In this guide, we’ll get a LEMP stack installed on an Ubuntu 12.04 and above. Where Ubuntu is an Linux Operating System which cover first requirement of LEMP Stack. There are different ways of installing packages/software in Linux Operating System from package manager, from Source, using binary files.

Here we will cover LEMP Installation using Ubuntu Package Manager,apt. A package manager allows us to install most software pain-free from a repository maintained by Ubuntu.

Install Nginx

Nginx (pronounced as engine-x) is a free, open-source, high-performance HTTP server and reverse proxy, as well as an IMAP/POP3 proxy server written by Igor Sysoev.

To install Nginx enter the following command in your terminal:

Note: If you have already installed apache2 in your system, remove it first to avoid conflicts. To uninstall apache, run the following commands:

You can confirm that nginx has installed an your web server by directing your browser to your IP address or by check status.

sudo service nginx status

Output as follow :

Nginx is running (pid xxxx).

You can check right away to verify that everything in working condition by visiting your Lan Ip or server’s public IP address in your web browser.

Open your web browser and navigate to http://localhost/or http://server-ip-address/.

If you see this page, then your web server is now correctly installed.

Install MySQL

We have our web server up and running now it’s time to install MySQL for organizing our website information and provide access to database with their stored information.

Install Mysql server by typing the below command in terminal . This command will install mysql-server and mysql-client along with dependencies .

sudo apt-get -y install mysql-server mysql-client

During the installation, your server will ask you to enter password for the MySQL “root” user and confirm it. This is an administrative account in MySQL that has full privileges. This is similar as root user account of Linux Operating System Server itself.

When the installation is complete, We can verify our mysql server Status by following command.

Following command will create its database directory structure where it will store its information –

sudo mysql_install_db

Our Mysql server is installed and running but our major concern should be to secure also so we have to check the security of mysql server and set it up.This script remove default access permission and checking for root password security.

For the rest of the questions, you should simply hit the “ENTER” key through each prompt to accept the default values. This will remove some sample users and databases, disable remote root logins, and load these new rules so that MySQL immediately respects the changes we have made.

Then the prompt will ask you if you want to change the root password. Go ahead and choose N and move on to the next steps.

It’s easiest just to say Yes to all the options. At the end, MySQL will reload and implement the new changes.

By default, a MySQL installation has an anonymous user, allowing anyone
to log into MySQL without having to have a user account created for
them. This is intended only for testing, and to make the installation
go a bit smoother. You should remove them before moving into a
production environment.
Remove anonymous users? [Y/n] y
... Success!
Normally, root should only be allowed to connect from 'localhost'. This
ensures that someone cannot guess at the root password from the network.
Disallow root login remotely? [Y/n] y
... Success!
By default, MySQL comes with a database named 'test' that anyone can
access. This is also intended only for testing, and should be removed
before moving into a production environment.
Remove test database and access to it? [Y/n] y
- Dropping test database...
... Success!
- Removing privileges on test database...
... Success!
Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.
Reload privilege tables now? [Y/n] y
... Success!
Cleaning up...

Now your Database Server is setup and running. Now we can move to our dynamic content access for our web stores.

Install PHP

To install PHP-FPM, open terminal and type in these commands. We will configure the details of nginx and php details in the next step:

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

Configure php

We need to make one small change in the php configuration.Open up php.ini:

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

Find the line, cgi.fix_pathinfo=1, and change the 1 to 0.

cgi.fix_pathinfo=0

If this number is kept as 1, the php interpreter will do its best to process the file that is as near to the requested file as possible. This is a possible security risk. If this number is set to 0, conversely, the interpreter will only process the exact file path—a much safer alternative. Save and Exit. We need to make another small change in the php5-fpm configuration.Open up www.conf:

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

Find the line, listen = 127.0.0.1:9000, and change the 127.0.0.1:9000 to /var/run/php5-fpm.sock.

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

Save and Exit.

Restart php-fpm:

sudo service php5-fpm restart

Configure nginx

Open up the default virtual host file.

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

The configuration should include the changes below (the details of the changes are under the config information):

UPDATE: Newer Ubuntu versions create a directory called ‘html’ instead of ‘www’ by default. If /usr/share/nginx/www does not exist, it’s probably called html. Make sure you update your configuration appropriately.

Ablout LinuxTweaks

Welcome to Linuxtweaks – the blog contains a lot of useful information for Linux System administrator ,newbies who are going to start there career as Linux System admin and also for linux desktop users. We basically share what we are getting issue on daily bases.We started this blog on November 2014 because we wanted to keep our knowledge on one place for future uses and also for sharing our knowledge with others on the basic of Linux technologies.