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

The term "LAMP" stack is a very general term whilst refereing to a range of softwares encompassing Linux, Apache, MySQL and PHP being operated for the purpose of hosting dynamic and static content. Whilst this classification is majorly deployed for hosting CMS & majorly sites.

Coming to a more specific side, LAMP provides a working environment for a website to be available on your network whether it be Internet or a local.

So, the enviornment can be functional for a number of purposes. Moreover, it's one of the most employed environments for websites using PHP.

Altough the prior configuration that comes along these classifications might be enough for your site but depending on how you are willing your site to operate and response, you may change your preferences.

This provides us with the flexibility of modifying and changing certain rules and policies for our network and site that might not be possible otherwise.

Whilst in below tutorial, we are to deploy this LAMP environment on one of our servers. Currently, the major versions of PHP that are used throughout are 5.6 and 7.3. Depending on your site functionality, you may choose a version accordingly.

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.

STEP 1

Setup Apache Webserver

At first, we will install apache which is the server software, 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, make a fresh install for MySQL:

$ 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, you 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

The term LAMP encircle a number of softwares for hosting sites and other static content to be seen on network. Further question, what's next? So, after deploying your server, you need to configure your environment according to your site and upload the files.

The files could be in form of a framework or individual static files. For the further exploration of what other changes can be made on to this environment, you may need to study apache server or PHP configuration rules.