• Blog
  • Installation and Set-up of Apache2 Web Server + PHP + MySQL + PHPMyAdmin (LAMP) on Ubuntu 16.04
Installation and Set-up of Apache2 Web Server + PHP + MySQL + PHPMyAdmin (LAMP) on Ubuntu 16.04
Volodymyr Hodiak
Volodymyr Hodiak
111 September, 3, 2019 10 min

Software installation on Ubuntu 16.04 is done in several stages described below.
Remember to issue sudo command (sudo su -) when following the instruction.

Update the software

sudo apt-get update

Install Apache 2

apt-get install apache2

Configure Apache2

nano /etc/apache2/apache2.conf

## Fix the warning when restarting Apache
ServerName localhost

## Enable php
AddType application/x-httpd-php .php .phtml

## Set up UTF-8 encoding
AddDefaultCharset UTF-8

Restart Apache

service apache2 restart

Open http://localhost/ and enjoy.

Set up virtual hosts (VirtualHost)

For lazybones, we have a bitbucket template (only for local hosts).
Download the file. Put it into /var/www.
Give permission to execute the script: 

chmod +x /var/www/vhosts.sh

The example of creating a host file:

sudo /var/www/vhosts.sh mysite.loc

You can skip the next steps regarding virtual hosts.
Write in the hosts /etc/hosts (nano /etct/hosts)

127.0.0.1    mysite.loc

Go to “/etc/apache2/” directory.
Take a look what's inside:

root@HP:/etc/apache2# ls -l
total 88
-rw-r--r-- 1 root root  7728 apr 26 22:55 apache2.conf
-rw-r--r-- 1 root root  7691 apr 26 22:34 apache2.conf~
drwxr-xr-x 2 root root  4096 apr 26 22:52 conf-available
drwxr-xr-x 2 root root  4096 apr 26 22:52 conf-enabled
-rw-r--r-- 1 root root  1782 jan  3 16:48 envvars
-rw-r--r-- 1 root root 31063 jan  3 16:48 magic
drwxr-xr-x 2 root root 12288 apr 26 22:27 mods-available
drwxr-xr-x 2 root root  4096 apr 26 22:27 mods-enabled
-rw-r--r-- 1 root root   320 jan  7 15:23 ports.conf
drwxr-xr-x 2 root root  4096 apr 26 22:43 sites-available
drwxr-xr-x 2 root root  4096 apr 26 22:44 sites-enabled

In the “sites-available” directory you'll find configurations of the available hosts and in the “sites-enabled” directory there are links to active hosts.

Go to available hosts and create a configuration file for mysite.loc. By the way, if your default configuration file ends with .conf, create a new configuration files with *. conf extension.

nano sites-available/mysite.conf

There we write the following:

   ServerName mysite.loc
    DocumentRoot /var/www/mysite.loc
    
        AllowOverride All

This is enough to get your local host working. Now you should create a link on it in sites-enabled.

a2ensite mysite.conf

Mind the trap: if you get an error and you can't create a link, you can fix it this way:

ln /etc/apache2/sites-available/mysite.conf /etc/apache2/sites-enabled/

Now, you just need to reload the Apache2 configurations:

service apache2 reload
## or, if needed
service apache2 restart

So Apache2 is working, the first host has been added.

Install MySQL

MySQL is available in the Ubuntu repositories. It is divided into several packages.
To install the MySQL server, run the following command:

sudo apt-get install mysql-server

The configure script will ask a user for an admin password (root) of the database. 

Install PHP 5.6

First of all, you have to delete all older versions if you have any.

sudo apt-get purge `dpkg -l | grep php| awk '{print $2}' |tr "\n" " "`

Add PPA

sudo add-apt-repository ppa:ondrej/php

Update

sudo apt-get update

Install  PHP

sudo apt-get install php5.6

Install additional modules

sudo apt-get install php5.6-mbstring php5.6-mcrypt php5.6-mysql php5.6-xml

Review the version:

sudo php -v

PHP 5.6.26-1+deb.sury.org~xenial+1 (cli) 

Copyright (c) 1997-2016 The PHP Group

Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies

    with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2016, by Zend Technologies

Install PhpMyAdmin

apt-get install phpmyadmin

Configure it to your needs: point out that you use Apache2, write the database password and the PhpMyAdmin password.
Trap 1: http //localhost/phpmyadmin not found ubuntu
Solution:

nano /etc/apache2/apache2.conf
## add it at the end of the file
Include /etc/phpmyadmin/apache.conf

Trap 2: http //localhost/phpmyadmin  HTTP ERROR 500

sudo apt-get install php-mbstring php-gettext
sudo phpenmod mcrypt
sudo phpenmod mbstring
sudo service apache2 restart

If you've done everything right, you'll have a configured ready-to-use local server. Good luck!