LAMP Install on Ubuntu 18.04 LTS

Here is the basic information on installing a LAMP (Linux, Apache, MySQL and PHP) Web Server. We will be using putty to connect to our server to run commands through SSH. You can download putty from here.

-First we will update our server. Run these 2 commands to update your package lists and then upgrade the software.

sudo apt-get update
sudo apt-get upgrade

Then we will install Apache2 to serve up the actual webpages.

sudo apt install apache2

-Once Installed you should be able to navigate to your servers IP (http://your_server_IP) in a web browser and see this.

-If you do not see the page after apache is installed you may need to check the UFW firewall to make sure “Apache Full” is showing on the allowed applications list.

-To check UFW type the below command.

sudo ufw app list

-You should see “Apache Full” on the list. If not Just type the below command.

sudo ufw allow in "Apache Full"

-Now its time to install MySQL by typing the below command

sudo apt install mysql-server

-Once Install is complete its time to secure the MySQL installation.

sudo mysql_secure_installation

-The first thing it will ask you is if you want to enable the Validate Password Plugin. This will allow you to set certain parameters for your MySQL passwords such as password length and if special characters are required. I personally do not enable this feature so i select No.

-It will then ask you to set your MySQL root password. Enter something better than password123 which i used for this example.

-It will then ask if you want to remove anonymous users. Select Y for yes

-It will ask you if you want to disallow root login remotely. Since we will be using this MySQL instance locally only you can select Y for Yes here as well.

-Now it will ask if you want to remove the test database and access to it. Enter Y for yes since we will not be using it.

-Finally it will ask if you want to reload the privilege tables and once again enter Y for yes.

Now we can enter into the MySQL command interface. Once you do this your command interface will be waiting for MySQL commands so the prompt will change from $ to mysql>

sudo mysql

-Be sure to change the password from ‘password123’ to something more secure in the below command.

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password123';

-Now you should no longer be in the MySQL prompt and see your normal $ prompt.

-Next we will install the final part of our LAMP server PHP and the associated hooks for Apache and MySQL

sudo apt install php libapache2-mod-php php-mysql

-Once the install is complete we can edit the dir.conf file in apache to look for PHP files first.

-To edit the config file use the below command.

sudo nano /etc/apache2/mods-enabled/dir.conf

-Change the file from this

<IfModule mod_dir.c>
    DirectoryIndex index.html index.cgi index.php index.xhtml index.htm

-To this

<IfModule mod_dir.c>
    DirectoryIndex index.php index.html index.cgi index.xhtml index.htm

-To exit the nano program use CTRL + X then press Y to save changes and Enter to leave the file name the same.

-Now we need to restart Apache since we changed one of the config files.

sudo systemctl restart apache2

-Now we can check the status of Apache with this command

sudo systemctl status apache2

-To exit the status prompt and go back to the normal CLI interface press CTRL + Z

-Now we can test PHP by creating a .php file to load in our browser. Use nano to create a file in the /var/www/html directory called info.php

sudo nano /var/www/html/info.php

-This will open a blank document. Just enter the below 3 lines of code to create the file.


-To exit and save the file simply press CTRL + X then press Y to save and Enter to leave the file the same name.

-Now we can open our browser and navigate to
http://your_server_IP/info.php and check if the PHP file loads properly. If it does it will look like this.

-Finally the last step is to remove the info.php file since it is no longer needed since we have verified it is working. It is best to remove anything you do not need so it cannot be exploited later.

sudo rm /var/www/html/info.php

-We are done! Thanks for following along. If you have any questions please feel free to use my contact form to ask anything you would like.

-Here is the video version of this tutorial.