How To: LAMP / LEMP Ubuntu 14.04 LTS Production Server Part I

This is Part I of our Guru series of tutorials that will provide an insight into various aspects of running a production ready server. One of the most important but often overlooked part of development is the environment.The positive difference that a properly deployed production server can make to your business is undeniable. So this tutorial will guide you through one of our favourite and arguably fastest growing server configuration among developer community.

Tutorial updated To Ubuntu 14.04 LTS

What is a LAMP/LEMP server ?

LAMP/LEMP is the one of the most popular web application environment and rightly so, it is extremely easy to use, stable, and open source. LAMP/LEMP is acronym for

  1. Linux (Operating System )
  2. Apache (Web Server )
  3. MySQL (Database )
  4. PHP (Scripting Language )

and in the case of LEMP replace Apache with Nginx which is pronounced as Engine-X, hence the ‘E’. This is not an exact definition but an example of a LAMP stack. You can mix and match various OS’s such as Cent OS, RHEL, or Ubuntu with a server of your choice ( Apache, Nginx, Openlitespeed etc.. ). Even the default database server can be swapped with drop-in replacements like MariaDB or Percona. We use MariaDB so does Google.

Now that we know what is a LAMP stack, we can actually move on to the LAMP setup that we are going to install and deploy in this tutorial.

OS

Ubuntu 14.04 LTS has been chosen as the OS for the server for its ease of use, precise repositories and finally the 5 year life-cycle of support that every LTS release has from Canonical Ltd.

HTTP Server

Apache or Nginx as your HTTP server? We use Apache 2.4.6 (needs a series of articles to explain the reasons).You must choose your HTTP server based on your needs, its flexibility, stability, and performance.A recent study conducted by W3techs showed adoption rate for Nginx increased by 52% over the past year. However, Nginx is still not the default HTTP server of the web and most probably the CMS software you are using, would have been developed for an Apache environment. WordPress, Joomla, & PHPBB etc only support Apache. Your Apache .htaccess rules needs to be adopted to Nginx on your own but Nginx does offer extensive DOCS and a few converters are available to do the same.

MySQL

High performance database schemas are the in-thing now as even web giants like Google and Wikipedia are making the move away from Oracle’s MySQL. MariaDB 5.5.X is a drop-in replacement for MySQL meaning its backward compatible with MySQL databases. MariaDB offers a lot more features focuses on scaling, replication, Indexing and Memory. It also comes with extensions that you can install to increase its features. Similarly, Percona an open source project supported by Oracle has seen increasing adoption rate due to its InnoDB optimisations that are providing excellent results during benchmarks.

PHP

Most of the open source software that we use for our clients is made with PHP and most of you are probably using software made with PHP such as WordPress, Joomla or Xenforo. So, the default LAMP stack in Ubuntu installs PHP automatically in case you need Perl or Phyton, you need to install them before it can be used instead of PHP.

Let’s Start

Wow! That was long but trust me it always better to know what you are doing.

Hosting & Deploying

If you are using web hosts then most probably your host would install the OS for you. Make sure that you get a Ubuntu 14.04 LTS machine with root access.

Updating OS

Sometimes the OS will not be updated and its good to start by updating the OS and apt-get ( command-line packaging tool ). You would need a SSH Client or Terminal access to your server to use the following commands. If you are on windows you can use Putty

sudo apt-get update && sudo apt-get upgrade

Let’s break the command into digestible pieces:

  • sudo lets you run programs as super user.
  • apt-get refers to the command-line packaging tool.
  • upgrade does exactly what you think it does, it tells apt-get to update/upgrade.

Installing LAMP

Ubuntu comes with a default LAMP setup. At the time of writing Ubuntu 14.04 LTS installs PHP 5.3.X, MySQL 4.1 and Apache 2.To install this use the following command

sudo apt-get install lamp-server^

Custom Stack

If you don’t want to install the default stack, following commands will guide you through customising your stack.

  • HTTP Server
  • PHP
  • SQL Server
  • Additional Tools

Apache
We are installing apache 2 and the apache module for php 5

sudo apt-get install apache2 libapache2-mod-php5

Need to restart for it to work

sudo service apache2 restart

Nginx
Installing nginx

sudo apt-get install nginx

Again, need we need to start it.

sudo service nginx start

Ubuntu doesn’t have an official repository that offers PHP 5.4+. So, you have to use the PHP Packages maintained by Ondřej Surý or similar PPA.
To manage PPA‘s you would need Python Software Properties

sudo apt-get install python-software-properties

Choose your PHP.
To install Ondřej Surý old stable (PHP 5.4.X)

sudo add-apt-repository ppa:ondrej/php5-oldstable

To install PHP 5.5.X.
Remember, using PHP 5.5.X version can break your website, as there are significant changes from previous version. For example MySQL functions have been dropped in favour for MySQLi, so you would need to lookout for deprecated code before using this version.

sudo add-apt-repository ppa:ondrej/php5

Above command has added the required PHP version repository to the apt-get list now let us update it to download the necessary version with the following command.

sudo apt-get update

Finally let us install PHP along with the modules that we need with the following command.

sudo apt-get install php5 php5-curl php5-cli php5-cgi php5-pear php5-imagick php5-gd php5-fpm php5-pdo php5-mcrypt

You see the complete list of PHP modules using the following command

apt-cache search php5-

MySQL
Make sure that you install MySQL after you have added the Ondřej Surý PPA mentioned during PHP installation. Use the following command to install.

sudo apt-get install mysql-server libapache2-mod-auth-mysql php5-mysql mysql-client

MariaDB
Now if you don’t want to use MySQL and start using MariaDB. Just avoid the previous step and use the following commands.

sudo apt-get install python-software-properties
sudo apt-key adv --recv-keys --keyserver hkp://keyserver.ubuntu.com:80 0xcbcb082a1bb943db
sudo add-apt-repository 'deb http://ftp.kaist.ac.kr/mariadb/repo/5.5/ubuntu precise main'

Ondřej Surý PPA doesn’t contain MariaDB so we need to add another PPA that offers MariaDB 5.5.X. _For now we recommend using MariaDB 5.5.X as it’s a Drop-in replacement for MySQL. MariaDB 10.X is not. Once, the package has been added its time to update and install using the following commands

sudo apt-get update
sudo apt-get install mariadb-server libapache2-mod-auth-mysql php5-mysql mysql-client

While installing you will be asked for a root login info and other details. Just read the questions and answer them and use a secure password you will be fine.

To start the MySQL Server use command

  sudo service mysql start

Or

sudo /etc/init.d/mysql start

Command for both MySQL and mariaDB

phpMyAdmin
You can choose to manage your databases through phpMyAdmin.

sudo apt-get install phpmyadmin

While Installing Choose your HTTP Server ( Apache or Nginx ) and then YES for dbcommon_config. phpMyAdmin configuration files will be located at /etc/dbconfig-common/phpmyadmin.conf.
Fail2Ban
A useful tool in your arsenal that automatically adds IPTable rules to ban malicious IP Address based on logs.

sudo apt-get install fail2ban

Webmin
Webmin , a graphical server management tool. You can manager your through a web browser without needing terminal.
use the following command to edit source list with nano.

sudo nano /etc/apt/sources.list

Scroll to the bottom of the file and paste the following sources for Webmin.

deb http://download.webmin.com/download/repository sarge contrib
deb http://webmin.mirror.somersettechsolutions.co.uk/repository sarge contrib

Now, we need an unique key to install Webmin. Import it using the following command:

wget http://www.webmin.com/jcameron-key.asc && sudo apt-key add jcameron-key.asc

update apt-get & install webmin

sudo apt-get update
sudo apt-get install webmin

Start it

sudo service webmin start

Webmin uses port 10000 by default and if everything went well you should be able to access it at http://localhost:10000. Login in with your root login details.

End of Part I

Hurray! You made it to the end of Part I of our series on setting up a LAMP/LEMP server. I know, this is a really big article but the good news is you have a working LAMP/LEMP setup at the end of it. By no means this is a production ready server, we still have to configure and secure our server. Stay tuned as we dwell deeper into server setup in subsequent parts of this series.

Thank you. Subscribe to get updates as soon as we post the next part.