Installing LAMP – Ubuntu 18.04

Introduction

LAMP aka Linux, Apache, MySQL, and PHP, allows for easy hosting of websites or any apps. We will be using Ubuntu 18.04 to install LAMP during this guide. The steps should be similar to other updates of Linux, but to be safe, I would recommend following a different guide for your specific Linux version. If you are using a different hosting service, do make sure you have root privileges as most of the commands will be sudo commands.

Hosting – VPS

Frist, you need to find a host which sells Ubuntu Virtual Private Servers. This is often easy to find, but it’s difficult finding the one which the right balance of price and support / usability. For our tutorial, we will be assuming you will be using a host which already does the pre-setup for the user. We will also provide VPS services that we recommend.

Server Space

Pricing
Updated on 26/10/2020

We have used Server Space before, and had a great experience using them. Their prices are cheap and flexible. However, the support may not be as quick as other services. We also have no downtimes with their services and overall had a great experience with them.

Creating The Server

Server Space allows users to create servers within 2 click, follow along

Click on create then create server

After clicking on create, they offer a few options, however, we will stick to these options

Make sure to select the ubuntu 18.04 x64

After creating a server, they should redirect you to their control panel. This step if really up to you, but you can choose to either use PuTTY or use their own web console. i Would recommend using Putty, since its easier to copy and paste commands and overall it’s much faster.

Installing Apache & Updating Firewall

Installing Apache

Before anything make sure to update your package list as it is a fresh install

sudo apt-get update

Once thats done, we will be installing Apache.

Apache is an open-source and free web server software that powers around 40% of websites around the world. The official name is Apache HTTP Server, and it’s maintained and developed by the Apache Software Foundation. … When someone wants to visit a website, they enter a domain name into the address bar of their browser.

Hostinger

We can install Apache using this command

sudo apt install apache2

The operation may ask you if you would like to continue and show the list of packages that it plans to install and how much disk space it takes up. Type Y then press ENTER.

Updating Firewall

Updating the firewall rules is pretty easy, but if you are using a different host, this may be different. For an example Google Cloud Platform has a different section for their firewall rules, make sure you know if your hosting services uses UFW or their own firewall system.

Assuming you are using Server Space, UFW will be enabled by default. You can check application profiles for Apache as such

sudo ufw app info "Apache Full"
Profile: Apache Full
Title: Web Server (HTTP,HTTPS)
Description: Apache v2 is the next generation of the omnipresent Apache web
server.

Ports:
  80,443/tcp

Above is a Sample Output. You can see that the ports are 80, HTTP and 443, HTTPS. Now we allow incoming traffic from both ports for the Apache profile by doing

sudo ufw allow in "Apache Full"

It’s as easy as that. Once done, you can try to check if it’s working by entering your server’s IP address to your browser and seeing if the default Apache webpage loads.

Server Space
For Server Space users, the IP of the server is located under your server list
Other Hosts

If you use other hosting services, the server’s IP should be quite easy to locate. If you are lost to where it is, it is usually the same IP you use for your SSH connection ( PuTTy ), if you are using their web console and still cant find the server’s IP address, here is how.

sudo apt install curl
curl http://icanhazip.com

If everything goes well and works. once you enter your IP into your browser, like so

http://your_ip/

 Installing MySQL

Now that you are done with your web server, its time to install your database for the server.

MySQL is an open-source relational database management system. Its name is a combination of “My”, the name of co-founder Michael Widenius’s daughter, and “SQL”, the abbreviation for Structured Query Language. 

Wikipedia

Just like how we installed our webserver, we can install MySQL by doing

sudo apt install mysql-server

The operation may ask you if you would like to continue and show the list of packages that it plans to install and how much disk space it takes up. Type Y then press ENTER.

Once its done installing, you can choose to run the security script that comes with MySQL. This is optional, however, its STRONGLY recommended you run it. You can start the script by typing

sudo mysql_secure_installation

Just follow the script and answer the questions how you would like. I would recommend enabling the VALIDATE PASSWORD PLUGIN, however you may not need to.

Now if you would like to use a password to connect to MySQL as root, you can do so by switching the authentication method from auth_socket to mysql_native_password. You can do this by opening up MySQL from your terminal

sudo mysql

Next, you can check what authentication method you are using if you like by running the following command

SELECT user,authentication_string,plugin,host FROM mysql.user;

Beside root, by default, you should see auth_socket. To configure this setting, you can run the following command ALTER USER, Make sure you choose a very effective password and replace the code with your chosen password

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'PLEASE CHANGE ME, DEAR LORD';

One done, run this to update the grant table which puts your new change into effect

FLUSH PRIVILEGES;

You can now recheck your table and the plugin beside root should now be mysql_native_password by running the same command above

SELECT user,authentication_string,plugin,host FROM mysql.user;

Once checked if everything is set, you can then exit MySQL by running

exit

This is the end of installing MySQL, We will now move onto installing the last part, PHP

Installing PHP

Once you are done with installing MySQL, we will move onto PHP. PHP is really important to web development as it allows dynamic web pages and also gives a way to connect to your MySQL database to retrieve data

PHP is a general-purpose scripting language especially suited to web development. It was originally created by Danish-Canadian programmer Rasmus Lerdorf in 1994. The PHP reference implementation is now produced by The PHP Group. 

Wikipedia

We will be installing PHP the same way we installed the other packages. However, this time, we will be installing multiple packages at once rather than one. The extra packages will basically run PHP code under your web server and also being able to talk to your MySQL databse.

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

The operation may ask you if you would like to continue and show the list of packages that it plans to install and how much disk space it takes up. Type Y then press ENTER.

Changing the page order

This part is optional, however, it is recommended as if you have two index files in the same folder, index.php would be chosen rather than the other indexes. The config for this is located under the apache2 folder, so lets open it up and change the order

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

you can use any other editor, but we are using nano. The file itself will look like this

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

All we have to do is move the index.php to the beginning.

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

For nano, you can enter Control+X then Y then ENTER, to save the file. One this is done, you can then restart the Apache service by running this

sudo systemctl restart apache2

Once this is done, you can try to check for any errors by checking the status of the Apache service, you can do this by this command

sudo systemctl status apache2

You can press Q to exit the output

If everything is fine, then you have properly installed PHP. You can try to check if it works by writing your own index.php or a php webpage and going to it. The simplest way you can check this is by running creating a file called info.php then writing this into it

<?php
    // Show all information, defaults to INFO_ALL
    phpinfo();
?>

If everything went well, this should be how the page will look

Installing Additional Packages ( Optional )

If you like to find additional packages to improve PHP. You can see the packets for PHP by running the apt search command.

apt search php- | less

Use your arrow keys to navigate and Q to exit. The results you see will provide you with the package name and a short description for what it does. Using this, you can try to find packages that interests you and install it the same way we installed all the other packages. If you like to see a more longer description, you can do so by using this

apt show package_name

If you like to install multiple packages at once, you can do so by typing

sudo apt install package_1 package_2 package_3

Configuring Others

In this part we will be configuring your virtual host. This part is pretty much optional, however, if you like to follow some of the configuration here, feel free to do so

Link Domain with IP

DNS

Before setting your virtual host, make sure your DNS is configured right. Make sure your Domain points to your IP address of the server.

Example of how it lokos on CloudFlare
Virtual Host

Let us organize our web folders by creating a new folder for our website. Please make sure to add all of your web pages in that folder as we will use it as our document root.

sudo mkdir /var/www/your_domain

Once the directory is created, we will create our virtual host configuration and get to work. Lets do this by running this command

sudo nano /etc/apache2/sites-available/your_domain.com.conf

Once you are in the nano editor, you can choose to either create your own configuration or use this.

<VirtualHost *:80>
    ServerName your_domain.com
    ServerAlias www.your_domain.com
    ServerAdmin [email protected]_domain.com
    DocumentRoot /var/www/your_domain


    <Directory /var/www/your_domain>
        Options -Indexes +FollowSymLinks
        AllowOverride All
    </Directory>
   
</VirtualHost>

Now once done, we need to enable the virtual host by just running the a2ensite command

sudo a2ensite your_domain.com

You can now try to test the configuration to check if there any syntax errors with it by running the following command

sudo apachectl configtest

The output if everything goes according to plan should be Syntax OK. Now restart Apache to make it take effect

sudo systemctl restart apache2

Finally, to verify everything is working, try to enter the domain in your browser, and you should see your webpage.

Leave a Reply