Tuesday, 29 March 2016

Installing LEMP Stack on Ubuntu

Install Linux, Nginx, MySQL & PHP (LEMP) StackLEMP stack is a variation of LAMP used for development and deployment of Web Applications. LAMP consist of Linux, Apache, MySQL and PHP. Due to modular nature of LAMP, individual components can be swapped with alternatives. In LEMP the Apache Web Server is replaced with lightweight yet powerful Nginx web server. Other components such as Linux, MySQL and PHP remains the same.

In this article, we will see how to install and configure MySQL, Nginx, PHP on Ubuntu 14.04 LTS Server. We will also show you that how to deploy the phpMyAdmin application on this LEMP server.

Reading Advice: The Official Ubuntu Book (9th Edition)

 

System Specification:

We have provisioned a virtual machine with following specification:

CPU: 2.4 Ghz (2 Cores)
Memory: 1 GB
Storage: 40 GB

 

Install Linux:

Here, we will install Ubuntu 14.04 LTS Server. You can follow the guide Installation of Ubuntu 14.04 LTS Server to install operating system for your LEMP Server.

 

Install Nginx:

We are using Nginx to be used as the web server for our LEMP server. Use apt-cache to search for the available packages of Nginx. You may notice that, there are multiple packages are available for the Nginx like core, light, full, naxsi and extras. Each one of these packages contains a specific selection of Optional and Third Party modules, packaged together to address specific needs.

ahmer@myserver:~$ apt-cache search nginx
gunicorn - Event-based HTTP/WSGI server
nginx - small, powerful, scalable web/proxy server
nginx-common - small, powerful, scalable web/proxy server - common files
nginx-core - nginx web/proxy server (core version)
nginx-core-dbg - nginx web/proxy server (core version) - debugging symbols
nginx-doc - small, powerful, scalable web/proxy server - documentation
collectd-core - statistics collection and monitoring daemon (core system)
coquelicot - "one-click" file sharing web application with a focus on users' privacy
fcgiwrap - simple server to run CGI applications over FastCGI
gitweb - fast, scalable, distributed revision control system (web interface)
libghc-graceful-dev - Library to write graceful shutdown / upgrade service.
libghc-graceful-doc - Library to write graceful shutdown / upgrade service - doc
libghc-graceful-prof - Library to write graceful shutdown / upgrade service - prof
libplack-middleware-file-sass-perl - Sass and SCSS support for all Plack-based PSGI frameworks
lua-nginx-memcached - Pure Lua memcached client driver for the nginx embedded Lua language
lua-nginx-redis - Pure Lua redis client driver for the nginx embedded Lua language
mighttpd2 - classical web server on WAI/warp
nginx-extras - nginx web/proxy server (extended version)
nginx-extras-dbg - nginx web/proxy server (extended version) - debugging symbols
nginx-full - nginx web/proxy server (standard version)
nginx-full-dbg - nginx web/proxy server (standard version) - debugging symbols
nginx-light - nginx web/proxy server (basic version)
nginx-light-dbg - nginx web/proxy server (basic version) - debugging symbols
nginx-naxsi - nginx web/proxy server (version with naxsi)
nginx-naxsi-dbg - nginx web/proxy server (version with naxsi) - debugging symbols
nginx-naxsi-ui - nginx web/proxy server - naxsi configuration front-end
python-reconfigure - simple config file management library
rt4-fcgi - External FastCGI support for request-tracker4
stud - scalable TLS unwrapping daemon
ahmer@myserver:~$

we prefer to install the nginx-core and install the additional modules when required. But before installing Nginx, make sure you have updated your apt-get package index files.

ahmer@myserver:~$ sudo apt-get update
Ign http://pk.archive.ubuntu.com trusty InRelease
Hit http://pk.archive.ubuntu.com trusty-updates InRelease
Hit http://pk.archive.ubuntu.com trusty-backports InRelease
Hit http://security.ubuntu.com trusty-security InRelease
Hit http://pk.archive.ubuntu.com trusty Release.gpg
Hit http://pk.archive.ubuntu.com trusty-updates/main Sources
Hit http://security.ubuntu.com trusty-security/main Sources
Hit http://pk.archive.ubuntu.com trusty-updates/restricted Sources
Hit http://security.ubuntu.com trusty-security/restricted Sources
Hit http://pk.archive.ubuntu.com trusty-updates/universe Sources
Hit http://pk.archive.ubuntu.com trusty-updates/multiverse Sources
Hit http://security.ubuntu.com trusty-security/universe Sources
Hit http://pk.archive.ubuntu.com trusty-updates/main amd64 Packages
Hit http://security.ubuntu.com trusty-security/multiverse Sources
Hit http://security.ubuntu.com trusty-security/main amd64 Packages
Hit http://pk.archive.ubuntu.com trusty-updates/restricted amd64 Packages
Hit http://pk.archive.ubuntu.com trusty-updates/universe amd64 Packages
Hit http://security.ubuntu.com trusty-security/restricted amd64 Packages
Hit http://pk.archive.ubuntu.com trusty-updates/multiverse amd64 Packages
Hit http://security.ubuntu.com trusty-security/universe amd64 Packages
Hit http://pk.archive.ubuntu.com trusty-updates/main i386 Packages
Hit http://security.ubuntu.com trusty-security/multiverse amd64 Packages
Hit http://pk.archive.ubuntu.com trusty-updates/restricted i386 Packages
Hit http://security.ubuntu.com trusty-security/main i386 Packages
Hit http://pk.archive.ubuntu.com trusty-updates/universe i386 Packages
Hit http://pk.archive.ubuntu.com trusty-updates/multiverse i386 Packages
Hit http://security.ubuntu.com trusty-security/restricted i386 Packages
Hit http://security.ubuntu.com trusty-security/universe i386 Packages
Hit http://pk.archive.ubuntu.com trusty-updates/main Translation-en
Hit http://security.ubuntu.com trusty-security/multiverse i386 Packages
Hit http://pk.archive.ubuntu.com trusty-updates/multiverse Translation-en
Hit http://pk.archive.ubuntu.com trusty-updates/restricted Translation-en
Hit http://pk.archive.ubuntu.com trusty-updates/universe Translation-en
Hit http://pk.archive.ubuntu.com trusty-backports/main Sources
Hit http://security.ubuntu.com trusty-security/main Translation-en
Hit http://pk.archive.ubuntu.com trusty-backports/restricted Sources
Hit http://security.ubuntu.com trusty-security/multiverse Translation-en
Hit http://security.ubuntu.com trusty-security/restricted Translation-en
Hit http://security.ubuntu.com trusty-security/universe Translation-en
Hit http://pk.archive.ubuntu.com trusty-backports/universe Sources
Hit http://pk.archive.ubuntu.com trusty-backports/multiverse Sources
Hit http://pk.archive.ubuntu.com trusty-backports/main amd64 Packages
Hit http://pk.archive.ubuntu.com trusty-backports/restricted amd64 Packages
Hit http://pk.archive.ubuntu.com trusty-backports/universe amd64 Packages
Hit http://pk.archive.ubuntu.com trusty-backports/multiverse amd64 Packages
Hit http://pk.archive.ubuntu.com trusty-backports/main i386 Packages
Hit http://pk.archive.ubuntu.com trusty-backports/restricted i386 Packages
Hit http://pk.archive.ubuntu.com trusty-backports/universe i386 Packages
Hit http://pk.archive.ubuntu.com trusty-backports/multiverse i386 Packages
Hit http://pk.archive.ubuntu.com trusty-backports/main Translation-en
Hit http://pk.archive.ubuntu.com trusty-backports/multiverse Translation-en
Hit http://pk.archive.ubuntu.com trusty-backports/restricted Translation-en
Hit http://pk.archive.ubuntu.com trusty-backports/universe Translation-en
Hit http://pk.archive.ubuntu.com trusty Release
Hit http://pk.archive.ubuntu.com trusty/main Sources
Hit http://pk.archive.ubuntu.com trusty/restricted Sources
Hit http://pk.archive.ubuntu.com trusty/universe Sources
Hit http://pk.archive.ubuntu.com trusty/multiverse Sources
Hit http://pk.archive.ubuntu.com trusty/main amd64 Packages
Hit http://pk.archive.ubuntu.com trusty/restricted amd64 Packages
Hit http://pk.archive.ubuntu.com trusty/universe amd64 Packages
Hit http://pk.archive.ubuntu.com trusty/multiverse amd64 Packages
Hit http://pk.archive.ubuntu.com trusty/main i386 Packages
Hit http://pk.archive.ubuntu.com trusty/restricted i386 Packages
Hit http://pk.archive.ubuntu.com trusty/universe i386 Packages
Hit http://pk.archive.ubuntu.com trusty/multiverse i386 Packages
Hit http://pk.archive.ubuntu.com trusty/main Translation-en
Hit http://pk.archive.ubuntu.com trusty/multiverse Translation-en
Hit http://pk.archive.ubuntu.com trusty/restricted Translation-en
Hit http://pk.archive.ubuntu.com trusty/universe Translation-en
Ign http://pk.archive.ubuntu.com trusty/main Translation-en_US
Ign http://pk.archive.ubuntu.com trusty/multiverse Translation-en_US
Ign http://pk.archive.ubuntu.com trusty/restricted Translation-en_US
Ign http://pk.archive.ubuntu.com trusty/universe Translation-en_US
Reading package lists... Done
ahmer@myserver:~$

It gave us all hits because our package index files are already updated. In your case, output may be different.

Now install Nginx.

ahmer@myserver:~$ sudo apt-get install nginx
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following extra packages will be installed:
  fontconfig-config fonts-dejavu-core libfontconfig1 libgd3 libjbig0 libjpeg-turbo8 libjpeg8 libtiff5 libvpx1 libxpm4 libxslt1.1 nginx-common nginx-core
Suggested packages:
  libgd-tools fcgiwrap nginx-doc
The following NEW packages will be installed:
  fontconfig-config fonts-dejavu-core libfontconfig1 libgd3 libjbig0 libjpeg-turbo8 libjpeg8 libtiff5 libvpx1 libxpm4 libxslt1.1 nginx nginx-common nginx-core
0 upgraded, 14 newly installed, 0 to remove and 214 not upgraded.
Need to get 143 kB/2,705 kB of archives.
After this operation, 9,057 kB of additional disk space will be used.
Do you want to continue? [Y/n] Y
Get:1 http://pk.archive.ubuntu.com/ubuntu/ trusty-updates/main libtiff5 amd64 4.0.3-7ubuntu0.4 [143 kB]
Fetched 143 kB in 6s (21.9 kB/s)
Selecting previously unselected package fonts-dejavu-core.
(Reading database ... 55312 files and directories currently installed.)
Preparing to unpack .../fonts-dejavu-core_2.34-1ubuntu1_all.deb ...
Unpacking fonts-dejavu-core (2.34-1ubuntu1) ...
Selecting previously unselected package fontconfig-config.
Preparing to unpack .../fontconfig-config_2.11.0-0ubuntu4.1_all.deb ...
Unpacking fontconfig-config (2.11.0-0ubuntu4.1) ...
Selecting previously unselected package libfontconfig1:amd64.
Preparing to unpack .../libfontconfig1_2.11.0-0ubuntu4.1_amd64.deb ...
Unpacking libfontconfig1:amd64 (2.11.0-0ubuntu4.1) ...
Selecting previously unselected package libjpeg-turbo8:amd64.
Preparing to unpack .../libjpeg-turbo8_1.3.0-0ubuntu2_amd64.deb ...
Unpacking libjpeg-turbo8:amd64 (1.3.0-0ubuntu2) ...
Selecting previously unselected package libjpeg8:amd64.
Preparing to unpack .../libjpeg8_8c-2ubuntu8_amd64.deb ...
Unpacking libjpeg8:amd64 (8c-2ubuntu8) ...
Selecting previously unselected package libjbig0:amd64.
Preparing to unpack .../libjbig0_2.0-2ubuntu4.1_amd64.deb ...
Unpacking libjbig0:amd64 (2.0-2ubuntu4.1) ...
Selecting previously unselected package libtiff5:amd64.
Preparing to unpack .../libtiff5_4.0.3-7ubuntu0.4_amd64.deb ...
Unpacking libtiff5:amd64 (4.0.3-7ubuntu0.4) ...
Selecting previously unselected package libvpx1:amd64.
Preparing to unpack .../libvpx1_1.3.0-2_amd64.deb ...
Unpacking libvpx1:amd64 (1.3.0-2) ...
Selecting previously unselected package libxpm4:amd64.
Preparing to unpack .../libxpm4_1%3a3.5.10-1_amd64.deb ...
Unpacking libxpm4:amd64 (1:3.5.10-1) ...
Selecting previously unselected package libgd3:amd64.
Preparing to unpack .../libgd3_2.1.0-3_amd64.deb ...
Unpacking libgd3:amd64 (2.1.0-3) ...
Selecting previously unselected package libxslt1.1:amd64.
Preparing to unpack .../libxslt1.1_1.1.28-2build1_amd64.deb ...
Unpacking libxslt1.1:amd64 (1.1.28-2build1) ...
Selecting previously unselected package nginx-common.
Preparing to unpack .../nginx-common_1.4.6-1ubuntu3.4_all.deb ...
Unpacking nginx-common (1.4.6-1ubuntu3.4) ...
Selecting previously unselected package nginx-core.
Preparing to unpack .../nginx-core_1.4.6-1ubuntu3.4_amd64.deb ...
Unpacking nginx-core (1.4.6-1ubuntu3.4) ...
Selecting previously unselected package nginx.
Preparing to unpack .../nginx_1.4.6-1ubuntu3.4_all.deb ...
Unpacking nginx (1.4.6-1ubuntu3.4) ...
Processing triggers for man-db (2.6.7.1-1) ...
Processing triggers for ureadahead (0.100.0-16) ...
ureadahead will be reprofiled on next reboot
Processing triggers for ufw (0.34~rc-0ubuntu2) ...
Setting up fonts-dejavu-core (2.34-1ubuntu1) ...
Setting up fontconfig-config (2.11.0-0ubuntu4.1) ...
Setting up libfontconfig1:amd64 (2.11.0-0ubuntu4.1) ...
Setting up libjpeg-turbo8:amd64 (1.3.0-0ubuntu2) ...
Setting up libjpeg8:amd64 (8c-2ubuntu8) ...
Setting up libjbig0:amd64 (2.0-2ubuntu4.1) ...
Setting up libtiff5:amd64 (4.0.3-7ubuntu0.4) ...
Setting up libvpx1:amd64 (1.3.0-2) ...
Setting up libxpm4:amd64 (1:3.5.10-1) ...
Setting up libgd3:amd64 (2.1.0-3) ...
Setting up libxslt1.1:amd64 (1.1.28-2build1) ...
Setting up nginx-common (1.4.6-1ubuntu3.4) ...
Processing triggers for ureadahead (0.100.0-16) ...
Processing triggers for ufw (0.34~rc-0ubuntu2) ...
Setting up nginx-core (1.4.6-1ubuntu3.4) ...
Setting up nginx (1.4.6-1ubuntu3.4) ...
Processing triggers for libc-bin (2.19-0ubuntu6) ...
ahmer@myserver:~$

Open port 80 in Ubuntu Firewall.

ahmer@myserver:~$ sudo ufw allow 80
Rule updated
Rule updated (v6)
ahmer@myserver:~$

Use the browser from another machine to check the response of Nginx web server.

nginx-default-webpage-01

 

Install MySQL:

The 3rd letter/member of LEMP stack is MySQL database. MySQL 5.5 Database Server is available from apt repository, therefore we are installing it. Although, you can also download and install a later version from http://dev.mysql.com/downloads/.

ahmer@myserver:~$ sudo apt-get install mysql-server
[sudo] password for ahmer:
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following extra packages will be installed:
  libaio1 libdbd-mysql-perl libdbi-perl libhtml-template-perl libmysqlclient18 libterm-readkey-perl mysql-client-5.5 mysql-client-core-5.5 mysql-common
  mysql-server-5.5 mysql-server-core-5.5
Suggested packages:
  libclone-perl libmldbm-perl libnet-daemon-perl libplrpc-perl libsql-statement-perl libipc-sharedcache-perl tinyca mailx
The following NEW packages will be installed:
  libaio1 libdbd-mysql-perl libdbi-perl libhtml-template-perl libmysqlclient18 libterm-readkey-perl mysql-client-5.5 mysql-client-core-5.5 mysql-common mysql-server
  mysql-server-5.5 mysql-server-core-5.5
0 upgraded, 12 newly installed, 0 to remove and 214 not upgraded.
Need to get 9,329 kB of archives.
After this operation, 96.7 MB of additional disk space will be used.
Do you want to continue? [Y/n] Y
Get:1 http://pk.archive.ubuntu.com/ubuntu/ trusty/main libaio1 amd64 0.3.109-4 [6,364 B]
Get:2 http://pk.archive.ubuntu.com/ubuntu/ trusty-updates/main mysql-common all 5.5.47-0ubuntu0.14.04.1 [13.5 kB]
Get:3 http://pk.archive.ubuntu.com/ubuntu/ trusty-updates/main libmysqlclient18 amd64 5.5.47-0ubuntu0.14.04.1 [597 kB]
Get:4 http://pk.archive.ubuntu.com/ubuntu/ trusty/main libdbi-perl amd64 1.630-1 [879 kB]
Get:5 http://pk.archive.ubuntu.com/ubuntu/ trusty/main libdbd-mysql-perl amd64 4.025-1 [99.3 kB]
Get:6 http://pk.archive.ubuntu.com/ubuntu/ trusty/main libterm-readkey-perl amd64 2.31-1 [27.4 kB]
Get:7 http://pk.archive.ubuntu.com/ubuntu/ trusty-updates/main mysql-client-core-5.5 amd64 5.5.47-0ubuntu0.14.04.1 [703 kB]
Get:8 http://pk.archive.ubuntu.com/ubuntu/ trusty-updates/main mysql-client-5.5 amd64 5.5.47-0ubuntu0.14.04.1 [1,578 kB]
Get:9 http://pk.archive.ubuntu.com/ubuntu/ trusty-updates/main mysql-server-core-5.5 amd64 5.5.47-0ubuntu0.14.04.1 [3,599 kB]
Get:10 http://pk.archive.ubuntu.com/ubuntu/ trusty-updates/main mysql-server-5.5 amd64 5.5.47-0ubuntu0.14.04.1 [1,748 kB]
Get:11 http://pk.archive.ubuntu.com/ubuntu/ trusty/main libhtml-template-perl all 2.95-1 [65.5 kB]
Get:12 http://pk.archive.ubuntu.com/ubuntu/ trusty-updates/main mysql-server all 5.5.47-0ubuntu0.14.04.1 [11.8 kB]
Fetched 9,329 kB in 1min 22s (114 kB/s)
Preconfiguring packages ...
Selecting previously unselected package libaio1:amd64.
(Reading database ... 55526 files and directories currently installed.)
Preparing to unpack .../libaio1_0.3.109-4_amd64.deb ...
Unpacking libaio1:amd64 (0.3.109-4) ...
Selecting previously unselected package mysql-common.
Preparing to unpack .../mysql-common_5.5.47-0ubuntu0.14.04.1_all.deb ...
Unpacking mysql-common (5.5.47-0ubuntu0.14.04.1) ...
Selecting previously unselected package libmysqlclient18:amd64.
Preparing to unpack .../libmysqlclient18_5.5.47-0ubuntu0.14.04.1_amd64.deb ...
Unpacking libmysqlclient18:amd64 (5.5.47-0ubuntu0.14.04.1) ...
Selecting previously unselected package libdbi-perl.
Preparing to unpack .../libdbi-perl_1.630-1_amd64.deb ...
Unpacking libdbi-perl (1.630-1) ...
Selecting previously unselected package libdbd-mysql-perl.
Preparing to unpack .../libdbd-mysql-perl_4.025-1_amd64.deb ...
Unpacking libdbd-mysql-perl (4.025-1) ...
Selecting previously unselected package libterm-readkey-perl.
Preparing to unpack .../libterm-readkey-perl_2.31-1_amd64.deb ...
Unpacking libterm-readkey-perl (2.31-1) ...
Selecting previously unselected package mysql-client-core-5.5.
Preparing to unpack .../mysql-client-core-5.5_5.5.47-0ubuntu0.14.04.1_amd64.deb ...
Unpacking mysql-client-core-5.5 (5.5.47-0ubuntu0.14.04.1) ...
Selecting previously unselected package mysql-client-5.5.
Preparing to unpack .../mysql-client-5.5_5.5.47-0ubuntu0.14.04.1_amd64.deb ...
Unpacking mysql-client-5.5 (5.5.47-0ubuntu0.14.04.1) ...
Selecting previously unselected package mysql-server-core-5.5.
Preparing to unpack .../mysql-server-core-5.5_5.5.47-0ubuntu0.14.04.1_amd64.deb ...
Unpacking mysql-server-core-5.5 (5.5.47-0ubuntu0.14.04.1) ...
Processing triggers for man-db (2.6.7.1-1) ...
Setting up mysql-common (5.5.47-0ubuntu0.14.04.1) ...
Selecting previously unselected package mysql-server-5.5.
(Reading database ... 55889 files and directories currently installed.)
Preparing to unpack .../mysql-server-5.5_5.5.47-0ubuntu0.14.04.1_amd64.deb ...
Unpacking mysql-server-5.5 (5.5.47-0ubuntu0.14.04.1) ...
Selecting previously unselected package libhtml-template-perl.
Preparing to unpack .../libhtml-template-perl_2.95-1_all.deb ...
Unpacking libhtml-template-perl (2.95-1) ...
Selecting previously unselected package mysql-server.
Preparing to unpack .../mysql-server_5.5.47-0ubuntu0.14.04.1_all.deb ...
Unpacking mysql-server (5.5.47-0ubuntu0.14.04.1) ...
Processing triggers for man-db (2.6.7.1-1) ...
Processing triggers for ureadahead (0.100.0-16) ...
ureadahead will be reprofiled on next reboot
Setting up libaio1:amd64 (0.3.109-4) ...
Setting up libmysqlclient18:amd64 (5.5.47-0ubuntu0.14.04.1) ...
Setting up libdbi-perl (1.630-1) ...
Setting up libdbd-mysql-perl (4.025-1) ...
Setting up libterm-readkey-perl (2.31-1) ...
Setting up mysql-client-core-5.5 (5.5.47-0ubuntu0.14.04.1) ...
Setting up mysql-client-5.5 (5.5.47-0ubuntu0.14.04.1) ...
Setting up mysql-server-core-5.5 (5.5.47-0ubuntu0.14.04.1) ...
Setting up mysql-server-5.5 (5.5.47-0ubuntu0.14.04.1) ...
160329 18:37:39 [Warning] Using unique option prefix key_buffer instead of key_buffer_size is deprecated and will be removed in a future release. Please use the full name instead.
160329 18:37:39 [Note] /usr/sbin/mysqld (mysqld 5.5.47-0ubuntu0.14.04.1) starting as process 2694 ...
mysql start/running, process 2826
Setting up libhtml-template-perl (2.95-1) ...
Processing triggers for ureadahead (0.100.0-16) ...
Setting up mysql-server (5.5.47-0ubuntu0.14.04.1) ...
Processing triggers for libc-bin (2.19-0ubuntu6) ...
ahmer@myserver:~$

Add mysql service to system startup.

ahmer@myserver:~$ sudo update-rc.d mysql defaults
[sudo] password for ahmer:
Adding system startup for /etc/init.d/mysql ...
   /etc/rc0.d/K20mysql -> ../init.d/mysql
   /etc/rc1.d/K20mysql -> ../init.d/mysql
   /etc/rc6.d/K20mysql -> ../init.d/mysql
   /etc/rc2.d/S20mysql -> ../init.d/mysql
   /etc/rc3.d/S20mysql -> ../init.d/mysql
   /etc/rc4.d/S20mysql -> ../init.d/mysql
   /etc/rc5.d/S20mysql -> ../init.d/mysql
ahmer@myserver:~$

Execute MySQL post-installation scripts.

ahmer@myserver:~$ /usr/bin/mysql_secure_installation
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!
In order to log into MySQL to secure it, we'll need the current
password for the root user.  If you've just installed MySQL, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.
Enter current password for root (enter for none):
OK, successfully used password, moving on...
Setting the root password ensures that nobody can log into the MySQL
root user without the proper authorisation.
You already have a root password set, so you can safely answer 'n'.
Change the root password? [Y/n] n
... skipping.
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...
ERROR 1008 (HY000) at line 1: Can't drop database 'test'; database doesn't exist
... Failed!  Not critical, keep moving...
- 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...
All done!  If you've completed all of the above steps, your MySQL
installation should now be secure.
Thanks for using MySQL!
ahmer@myserver:~$

 

Install PHP:

Finally, to add the application server functionality in our LEMP stack, we have to install PHP. We are using the following command to install php-fpm from apt repository. In our case, it installed PHP 5.5. Although, stable version 7 of PHP is available at this time. If you want to use a latest version than you can download it from http://www.php.net

ahmer@myserver:~$ sudo apt-get install php5-fpm
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following extra packages will be installed:
  php5-common php5-json
Suggested packages:
  php5-user-cache php-pear
The following NEW packages will be installed:
  php5-common php5-fpm php5-json
0 upgraded, 3 newly installed, 0 to remove and 214 not upgraded.
Need to get 2,670 kB of archives.
After this operation, 10.6 MB of additional disk space will be used.
Do you want to continue? [Y/n] Y
Get:1 http://pk.archive.ubuntu.com/ubuntu/ trusty/main php5-json amd64 1.3.2-2build1 [34.4 kB]
Get:2 http://pk.archive.ubuntu.com/ubuntu/ trusty-updates/main php5-common amd64 5.5.9+dfsg-1ubuntu4.14 [443 kB]
Get:3 http://pk.archive.ubuntu.com/ubuntu/ trusty-updates/universe php5-fpm amd64 5.5.9+dfsg-1ubuntu4.14 [2,193 kB]
Fetched 2,670 kB in 1min 9s (38.4 kB/s)
Selecting previously unselected package php5-json.
(Reading database ... 55990 files and directories currently installed.)
Preparing to unpack .../php5-json_1.3.2-2build1_amd64.deb ...
Unpacking php5-json (1.3.2-2build1) ...
Selecting previously unselected package php5-common.
Preparing to unpack .../php5-common_5.5.9+dfsg-1ubuntu4.14_amd64.deb ...
Unpacking php5-common (5.5.9+dfsg-1ubuntu4.14) ...
Selecting previously unselected package php5-fpm.
Preparing to unpack .../php5-fpm_5.5.9+dfsg-1ubuntu4.14_amd64.deb ...
Unpacking php5-fpm (5.5.9+dfsg-1ubuntu4.14) ...
Processing triggers for man-db (2.6.7.1-1) ...
Processing triggers for ureadahead (0.100.0-16) ...
Setting up php5-common (5.5.9+dfsg-1ubuntu4.14) ...
Creating config file /etc/php5/mods-available/pdo.ini with new version
php5_invoke: Enable module pdo for fpm SAPI
Creating config file /etc/php5/mods-available/opcache.ini with new version
php5_invoke: Enable module opcache for fpm SAPI
Setting up php5-fpm (5.5.9+dfsg-1ubuntu4.14) ...
Creating config file /etc/php5/fpm/php.ini with new version
php5_invoke pdo: already enabled for fpm SAPI
php5_invoke opcache: already enabled for fpm SAPI
php5-fpm start/running, process 3960
Setting up php5-json (1.3.2-2build1) ...
php5_invoke: Enable module json for fpm SAPI
Processing triggers for ureadahead (0.100.0-16) ...
ahmer@myserver:~$

Also install the php5-mysqlnd package as follows. This package provides modules for MySQL database connections directly from PHP scripts. It includes the generic mysql module which can be used to connect to all versions of MySQL

ahmer@myserver:/etc/nginx/sites-enabled$ sudo apt-get install php5-mysqlnd
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following NEW packages will be installed:
  php5-mysqlnd
0 upgraded, 1 newly installed, 0 to remove and 214 not upgraded.
Need to get 0 B/134 kB of archives.
After this operation, 544 kB of additional disk space will be used.
Selecting previously unselected package php5-mysqlnd.
(Reading database ... 56064 files and directories currently installed.)
Preparing to unpack .../php5-mysqlnd_5.5.9+dfsg-1ubuntu4.14_amd64.deb ...
Unpacking php5-mysqlnd (5.5.9+dfsg-1ubuntu4.14) ...
Processing triggers for php5-fpm (5.5.9+dfsg-1ubuntu4.14) ...
php5-fpm stop/waiting
php5-fpm start/running, process 5954
Setting up php5-mysqlnd (5.5.9+dfsg-1ubuntu4.14) ...
php5_invoke: Enable module mysqlnd for fpm SAPI
php5_invoke: Enable module mysql for fpm SAPI
php5_invoke: Enable module mysqli for fpm SAPI
php5_invoke: Enable module pdo_mysql for fpm SAPI
Processing triggers for php5-fpm (5.5.9+dfsg-1ubuntu4.14) ...
php5-fpm stop/waiting
php5-fpm start/running, process 6606
ahmer@myserver:/etc/nginx/sites-enabled$ sudo service nginx restart
* Restarting nginx nginx                                                                                                                                        [ OK ]
ahmer@myserver:/etc/nginx/sites-enabled$

Add php-fpm to system startup.

ahmer@myserver:/etc/nginx$ sudo update-rc.d php5-fpm defaults
Adding system startup for /etc/init.d/php5-fpm ...
   /etc/rc0.d/K20php5-fpm -> ../init.d/php5-fpm
   /etc/rc1.d/K20php5-fpm -> ../init.d/php5-fpm
   /etc/rc6.d/K20php5-fpm -> ../init.d/php5-fpm
   /etc/rc2.d/S20php5-fpm -> ../init.d/php5-fpm
   /etc/rc3.d/S20php5-fpm -> ../init.d/php5-fpm
   /etc/rc4.d/S20php5-fpm -> ../init.d/php5-fpm
   /etc/rc5.d/S20php5-fpm -> ../init.d/php5-fpm
ahmer@myserver:/etc/nginx$

LEMP Stack has been installed and configured on Ubuntu server.

 

Deploy phpMyAdmin:

To test the functionality of our LEMP server we are deploying a PHP application on it.

Go to https://www.phpmyadmin.net/downloads/ and select a version according to your PHP and MySQL versions. we have selected the latest phpMyAdmin 4.6.0.

ahmer@myserver:~$ wget https://files.phpmyadmin.net/phpMyAdmin/4.6.0/phpMyAdmin-4.6.0-english.tar.gz
--2016-03-29 19:47:27--  https://files.phpmyadmin.net/phpMyAdmin/4.6.0/phpMyAdmin-4.6.0-english.tar.gzResolving files.phpmyadmin.net (files.phpmyadmin.net)... 185.93.3.27
Connecting to files.phpmyadmin.net (files.phpmyadmin.net)|185.93.3.27|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 6184667 (5.9M) [application/octet-stream]
Saving to: âphpMyAdmin-4.6.0-english.tar.gzâ
100%[==============================================================================================================================>] 6,184,667   84.2KB/s   in 96s
2016-03-29 19:49:05 (63.2 KB/s) - âphpMyAdmin-4.6.0-english.tar.gzâ saved [6184667/6184667]
ahmer@myserver:~$ ls
phpMyAdmin-4.6.0-english.tar.gz  README.txtahmer@myserver:~$

Now, create a directory to be used for deploying web applications, extract the phpMyAdmin in a sub-directory within that directory, and change the ownership of that directory to www-data (the default user for Nginx web server).

ahmer@myserver:~$ sudo mkdir /websites
ahmer@myserver:~$ sudo tar -xvzf phpMyAdmin-4.6.0-english.tar.gz
ahmer@myserver:~$ sudo mv phpMyAdmin-4.6.0-english /websites/phpmyadmin
ahmer@myserver:~$ sudo chown -R www-data:www-data /websites

Now, add a new website to Nginx as follows:

ahmer@myserver:~$ cd /etc/nginx/sites-available/
ahmer@myserver:/etc/nginx/sites-available$ ls
default
ahmer@myserver:/etc/nginx/sites-available$ sudo cp default phpmyadmin
ahmer@myserver:/etc/nginx/sites-available$ sudo vi phpmyadmin

under the server directive, change listening ports to 8081, because we want to reserve the port 80 for our main website.

listen 8081 default_server;
listen [::]:8081 default_server ipv6only=on;

update the document root location to your phpMyAdmin directory.

root /websites/phpmyadmin/;

Add index.php to index files list.

index index.php index.html index.htm;

and uncomment the php section as follows.

location ~ \.php$ {
               fastcgi_split_path_info ^(.+\.php)(/.+)$;
       #       # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
       #
       #       # With php5-cgi alone:
       #       fastcgi_pass 127.0.0.1:9000;
       #       # With php5-fpm:
               fastcgi_pass unix:/var/run/php5-fpm.sock;
               fastcgi_index index.php;
               include fastcgi_params;
       }

to enable this website, we create a symbolic link of the phpMyAdmin configuration file at /etc/nginx/sites-enabled/ and restart the Nginx service. Also allow the port 8081 thru Ubuntu Firewall.

ahmer@myserver:/etc/nginx/sites-available$ cd /etc/nginx/sites-enabled
ahmer@myserver:/etc/nginx/sites-enabled$ sudo cp -s /etc/nginx/sites-available/phpmyadmin .
ahmer@myserver:/etc/nginx/sites-enabled$ sudo service nginx restart
* Restarting nginx nginx                            [ OK ]
ahmer@myserver:/etc/nginx/sites-enabled$ sudo ufw allow 8081
Rule added
Rule added (v6)
ahmer@myserver:/etc/nginx/sites-enabled$

Browse the website from another machine to check the configurations are correct.

phpmyadmin-login-page

Now, configure the phpMyAdmin as mentioned in our previous post Deploy phpMyAdmin on LAMP Server.

LEMP Stack with Nginx, MySQL & PHP has been installed successfully on Ubuntu server.

Installing LEMP Stack on Ubuntu


YOU MIGHT ALSO LIKE:

1 comment:

  1. Great Article. I also found Debian Jessie best suited for a LEMP stack, as there were issues with CENTOS and UBUNTU. The best way to configure was with MARIADB and PHP7. Full Tutorial with Google Pagespeed Module and Lets Encrypt https://www.markbarry.info/

    ReplyDelete