How to Setup Linux, Apache, MySQL and PHP (LAMP) stack on Ubuntu

by hash3liZer . 17 July 2019

How to Setup Linux, Apache, MySQL and PHP (LAMP) stack on Ubuntu

"LAMP" stack refers to a group of softwares referred to as Linux, Apache, MySQL and PHP which allows quick hosting of dynamic and static websites with very less to configure. It allows quick hosting for many web applications and admin interfaces including wordpress & phpmyadmin.

Lately, PHP has been one of the most widely imeplemented languages in web applications. And most of the time, you would come across PHP & Apache applications. Though, you can find PHP with nginx, but it's very rare because nginx doesn't support execution of PHP natively. So, you can see the pros of LAMP environment. 

In this tutorial, we are going to use Ubuntu (18.4 for me) as our linux environment, apache2 as our webserver, MySQL for data storage and finally PHP 5.6 as our server side language. The latest version for PHP currently is 7.3 but i chosed 5.6 because even today developers prefer to choose 5.6. In this version, altough the famous mysql extension is deprecated but can still be used by some tweaks.

As for the versions (>=7.0), the support for mysql extension has been completely removed and you may found some applications to be completely bricked. Let's get to the installation part.

STEP 1

Setup Apache Webserver

At first, we will install apache which is the server, we will be using for hosting our site. Apache is the most popular server software and is currently used by million of users. And as for ubuntu & debian operating systems, it can easily be installed using apt package manager. First update the repo list:

$ apt update

The install apache2:

$ apt install apache2

After having apache2 installed, enable the service daemon & start the apache2 service. Enabling the service daemon will start the apache2 server everytime the server boots or restart. So: 

$ systemctl start apache2
$ systemctl enable apache2

Finally, check the status for apache2:

$ systemctl status apache2

apache2 running

Then check whether you are able to see the default apache page by visting the server ip address:

http://your-server-ip-address/

apache page

STEP 2

Setup MySQL Database

Now, come to MySQL part. Like apache2, MySQL is also the most widely adopted & probably easier to configure database for data storage. Most of the famous frameworks and web applications like wordpress, phpmyadmin, and django use MySQL.

So, the question: why require MySQL? Usually sites need databases to store data securely and MySQL is one of those famously used databases. It provides multiple security countermeasures & solve many integrity problems. Install MySQL server and client utilities:

$ apt install mysql-server mysql-client

When you have installed mysql-server, it would use the default configuration which is sort of vulnerable. So, finally install the mysql server with command:

$ sudo mysql_secure_installation

At first, you will be asked for database password. You can skip this step if you want to. For the rest of questions, you may simply press [ENTER] button. The default values will be used which in turn basically would remove some unwanted databases and some configuration. However, if you'd like to change any of it, you can pick one of the given choices.

Finally, enable and start mysql server:

$ systemctl start mysql
$ systemctl enable mysql

Check MySQL status:

$ systemctl status mysql

mysql

STEP 3

Install PHP & Modules

Here come's the PHP part. PHP is the core component of LAMP stack. In this step, we will see installation for both, the latest PHP version and the 5.6 version. The latest PHP packages & modules are by default included in ubuntu and debian repos. However, for the 5.6 version, we will use a foreign php repo:

$ add-apt-repository ppa:ondrej/php

Then update the repo package list:

$ apt update

Now, we can install packages for PHP version 5.6 as well. Install PHP according to your required envrionment:

$ apt install php5.6 libapache2-mod-php5.6 php5.6-mcrypt
# OR for latest release
$ apt install php libapache2-mod-php

The above will install necessary packages for PHP and apache to execute PHP scripts. However, we would need some other PHP modules for some scripts to properly work. Install these required packages:

$ apt install php5.6-cgi php5.6-cli php5.6-common php5.6-curl php5.6-dev php5.6-gd
# OR for latest release
$ apt install php-cgi php-cli php-common php-curl php-dev php-gd

php modules

These were the commonly used php modules. You might have to install more depending on your web application. But these are the most commonly used PHP packages. So, it's better to have them already on your server.

STEP 4

Test LAMP server

Let's test our LAMP server now. Create a PHP info script in the default apache hosting directory:

$ echo "<?php phpinfo() ?>" > /var/www/html/phpinfo.php

The above command will create a new file under /var/www/html with name phpinfo.php with content: "<?php phpinfo() ?>". At this point, we may need to change the file permissions. It's not necessary but is better. Sometimes, we don't get necessary permissions. Anyways, assign full permission to this file. You can remove it later after testing:

$ chmod 777 /var/www/html/phpinfo.php

Now, access the phpinfo.php file through your browser:

http://your-server-ip-address/phpinfo.php

If you see the following document which is basically a quick navigation of your current PHP configuration, it means your LAMP stack is fully setup. You can move forward to next step now.

phpinfo.php

STEP 5

Phpmyadmin (Optional)

This is completly optional. But in case you would like to have an admin interface for managing your database and controlling your server to some extent, you can install phpmyadmin. You can quickly install phpmyadmin using apt:

$ apt install phpmyadmin

To access the phpmyadmin panel:

http://your-server-ip-address/phpmyadmin

Conclusion

LAMP stack provides us an environment for PHP applications to be hosted on internet. The question is what comes next? After you have LAMP stack, you can do a number of tasks like hosting your first wordpress site, uploading a site panel to internet, setup an email server and lot more. For configuring a domain with your server, you can direct your domain to the server ip address and later in apache server configuration, add the ServerName directive to apache configuration.