How To Install LEMP on Debian & Ubuntu (Linux, nginx, MySQL, PHP,PHPMyAdmin)

The LEMP (Linux, nginx, MySQL, PHP,PHP,MyAdmin)software stack is a group of software that can be used to serve dynamic web pages and web applications.In this guide, I will walk you through installing all of these components on Debian & Ubuntu .

This tutorial series helps sysadmins set up a new web server using the LEMP stack, as well as all of the required repos to download nginx and php-fpm. It will also review how to configure php and nginx, as well as how to create a php info page.

How To Install LEMP on Debian & Ubuntu (Linux, nginx, MySQL, PHP,PHPMyAdmin)

You can read the following articles to know more about Cheap VPS Hosting and VPS Tutorials .

  1. How to test VPS Speed and Performance? Install and Run UnixBench & sysbench
  2. RamNode VPS Review-Cheap KVM and SSD VPS Starts at $3.50/mo
  3. host1plus VPS Review-Affordable & Quick VPS Hosting RAM 256 MB Only $2.00/mo

How to Install the Nginx Web Server

Nginx is a open source web server. Generally, Apache is installed on majority of the servers but Nginx tends to give better performance out of the box compared to Apache.

Nginx is a open source web server

There are several ways to install Nginx.

1.Install Nginx from Repositories

1
2
sudo apt-get update && sudo apt-get upgrade
sudo apt-get install nginx

There are several ways to install Nginx

2.Install Nginx from a Source Distribution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
sudo apt-get install libpcre3-dev build-essential libssl-dev
cd /opt/
sudo wget http://nginx.org/download/nginx-1.9.2.tar.gz
sudo tar -zxvf nginx*.tar.gz
cd /opt/nginx-*
sudo ./configure --prefix=/opt/nginx --user=nginx --group=nginx --with-http_ssl_module --with-ipv6
sudo make
sudo make install
sudo adduser --system --no-create-home --disabled-login --disabled-password --group nginx
 
#Create a script to run Nginx: /lib/systemd/system/nginx.service
[Unit]
Description=A high performance web server and a reverse proxy server
After=network.target
[Service]
Type=forking
PIDFile=/opt/nginx/logs/nginx.pid
ExecStartPre=/opt/nginx/sbin/nginx -t -q -g 'daemon on; master_process on;'
ExecStart=/opt/nginx/sbin/nginx -g 'daemon on; master_process on;'
ExecReload=/opt/nginx/sbin/nginx -g 'daemon on; master_process on;' -s reload
ExecStop=-/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /opt/nginx/logs/nginx.pid
TimeoutStopSec=5
KillMode=mixed
[Install]
WantedBy=multi-user.target
 
sudo chmod +x /lib/systemd/system/nginx.service
sudo systemctl start nginx

3.Configure Server Blocks

Now, we need to configure nginx.Create the server block file /etc/nginx/sites-available/howsvps.com(replace howsvps.com with your domain:)

1
2
3
4
5
6
7
8
9
10
11
server {
listen   80;
server_name www.example.com example.com;
access_log /var/www/html/example.com/logs/access.log;
error_log /var/www/html/example.com/logs/error.log;
 
location / {
    root   /var/www/html/example.com/public_html;
    index  index.html index.htm;
}
}

Configure Server Blocks

4.Create the public_html and log directories.

1
sudo mkdir -p /var/www/html/howsvps.com/{public_html,logs}

5.Enable the site and restart the web server.

1
2
sudo rm /etc/nginx/sites-enabled/howsvps.com
sudo /etc/init.d/nginx restart

After installation, we can verify nginx installation by opening the ip of server. It should display default nginx page.

we can verify nginx installation by opening the ip of server

Install and Configure PHP

PHP is a server-side scripting language designed for web development. The scripting role can also be filled with Python or Perl. We can do so by the running the following commands:

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

Install and Configure PHP

Then open up php.ini:

1
2
3
sudo nano /etc/php5/fpm/php.ini
# Find the line cgi.fix_pathinfo=1 and change the 1 to 0.
cgi.fix_pathinfo=0

open up php.ini

php5-fpm configuration. Open up www.conf:

1
2
3
4
5
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
sudo service php5-fpm restart

php5-fpm configuration

Install and Configure MySQL Database Server

MySQL is commonly used as the database for most LEMP stacks or other LAMP stacks.Open terminal and type in these commands:

1
sudo apt-get install mysql-server

During the installation,You should set a root password.

During the installation,You should set a root password

Create a database and grant permissions.

1
2
3
4
5
mysql -u root -p
create database howsvps;
grant all on howsvps.* to 'howsvpsu' identified by '1234567';
quit
sudo /etc/init.d/php-fastcgi restart

Create a database and grant permissions.

OR

1
2
3
#to generate the directory structure
sudo mysql_install_db
sudo mysql_secure_installation

Install and Configure phpMyAdmin

PhpMyAdmin is a powerful PHP based gui for MySQL. phpMyAdmin was originally developed by Tobias Ratschiller.Installing phpMyAdmin is almost as easy as installing MySQL, just a few more steps.

Install phpMyAdmin

1
sudo apt-get install phpmyadmin

During the installation,You could choose the web server that will automatically configured to run PhpMyadmin .

Install and Configure phpMyAdmin

The next prompt will ask if you would like dbconfig-common to configure a database for phpmyadmin to use. Select “Yes” to continue.

dbconfig-common to configure a database

Afterward, you will be asked to select and confirm a password for a new database that will hold phpMyAdmin’s own data.

confirm a password for a new database

You just need to create a symbolic link from the installation files to our Nginx document root directory by typing this:

1
2
3
4
5
sudo ln -s /usr/share/phpmyadmin /usr/share/nginx/html
 
#enable the mcrypt PHP module
sudo php5enmod mcrypt
sudo service php5-fpm restart

You should now have a configured and secured PHPMyAdmin instance and MySQL database ready to use. Go to your server’s domain name or public IP address followed by /phpmyadmin, in your web browser:

configured and secured PHPMyAdmin instance

Conclusion

You should now have Linux, nginx, MySQL, PHP,PHPMyAdmin configured and ready to use on your Debian & Ubuntu server. If you want to host websites on your server running NGINX ,you can use NGINX server blocks.

Server blocks are the NGINX equivalent of Apache’s virtual hosts – they let you direct incoming HTTP traffic for a domain name to a particular directory on your server.

 
<

1 Comment

  • Noe Reply

    July 7, 2016 at pm11:49

    How to add SSL and mod_perl support to Apache?

     

Leave a Comment

Login

Welcome! Login in to your account

Remember me Lost your password?

Lost Password