How to Install LAMP on Ubuntu 18.04

Guide of how to install Lamp on Ubuntu – A “LAMP” stack is a collection of open-source software that is installed together on a server to allow it to host dynamic websites and web applications. This term is an abbreviation for the Linux operating system, which is used in conjunction with the Apache webserver. The site’s data is kept in a MySQL database, while PHP handles dynamic content.

We’ll set up the process of how to install LAMP stack on an Ubuntu 18.04 server in this tutorial.

Prerequisites for how to install Lamp on Ubuntu

You’ll need an Ubuntu 18.04 server with a non-root sudo-enabled user account and a basic firewall installed to finish this tutorial. If you have already completed the requirements then follow the below steps:

Step 01: Installing Apache and Updating the Firewall

The Apache web server is a popular open-source web server that may be use to host dynamic webpages when combined with PHP. It’s well-documented and has been widely used throughout the majority of the internet’s history.

To begin, make sure your apt cache is up to date with the following:

$ sudo apt update

If this is your first time using sudo in this session, you’ll be asked to confirm your rights by entering your normal user’s password.

After the cache has been refreshed, you may install Apache using the following command:

$ sudo apt install apache2

apt will inform you which packages it wants to install and how much more disc space they’ll take up once you run this command. To confirm, press Y and then ENTER, and the installation will begin.

Adjust the Firewall to Allow Web Traffic

Next, verify that your firewall allows HTTP and HTTPS traffic, assuming you followed the initial server setup instructions and enabled the UFW firewall. You can see if UFW has an Apache application profile by doing the following:

$ sudo ufw app list

Output:

Install LAMP on Ubuntu img1

If you check at the specifics of the Apache Full profile, you’ll notice that it allows traffic on ports 80 and 443:

$ sudo ufw app info "Apache Full"

Output:

Install LAMP on Ubuntu img2

Run the following command to enable inbound HTTP and HTTPS access to this server:

$ sudo ufw allow "Apache Full"

You can perform an immediate spot check to ensure that everything went as planned by going to your server’s public IP address in your web browser (if you don’t know what your public IP address is, see the note under the next heading):

http://your_server_ip

The default Ubuntu 18.04 Apache web page will appear, which is provided for information and testing reasons. This is what it should look like:

Ubuntu

If this page appears, your web server is now properly configured and accessible across your firewall.

How To Find your Server’s Public IP Address

There are several techniques to determine your server’s public IP address if you don’t know it. This is usually the IP that you use to connect to your server using SSH.

You may accomplish this from the command line in a few different ways. To begin, type the following into the iproute2 tools to obtain your IP address:

$ ip addr show eth0 | grep inet | awk '{ print $2; }' | sed 's/\/.*$//'

You’ll get two or three lines back as a result of this. Although they are all valid addresses, your computer may only be able to utilize one of them, so try them all.

Another option is to use the curl function to contact an external party and ask them to tell you how they see your server. This is done by inquiring about your IP address with a certain server.

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

To view the default Apache page, type your IP address into your web browser’s address bar, regardless of how you got it.

Step 02: Installing MySQL – install Lamp on Ubuntu

It’s time to install MySQL now that your web server is up and running. MySQL is a relational database management system (RDBMS). Essentially, it will arrange and offer access to databases where your website’s data might be store.

To get and install this package, run apt once more:

$ sudo apt install mysql-server

This command will also provide a list of the packages that will be install, as well as the amount of disc space they will need. To continue, type Y.

Run a simple security script that is pre-installed with MySQL once the installation is complete to remove certain unsafe defaults and lock down access to your database server. To begin the interactive script, type:

$ sudo mysql_secure_installation

This will ask if you want to configure the VALIDATE PASSWORD PLUGIN.

Answer Y for yes, or anything else to continue without enabling.

Install LAMP on Ubuntu img4

If you say yes, you’ll be prompt to choose a password validation level. Keep in mind that if you choose level 2 for the toughest level, any password that does not contain numerals. Upper and lowercase letters, special characters, or is based on popular dictionary terms will result in an error.

Install LAMP on Ubuntu img5

Regardless of whether you used the VALIDATE PASSWORD PLUGIN or not, your server will prompt you to choose and confirm a password for the MySQL root user. This is not to be confuse with the root of the operating system. The database root user is a database administrator who has complete control over the database system. Even though the MySQL root user’s default authentication mechanism does not need the usage of a password. You should establish a strong password here as an added safety step. We’ll get to it in a minute.

Whether password validation is enabled, the strength of the root password you just typed will be shown, and your server will ask if you wish to alter it. Enter N for “no” at the popup if you’re OK with your existing password:

img6

At each prompt, press Y and then the ENTER key to complete the rest of the questions. This will delete some anonymous users and the test database, deactivate remote root logins, and load these new rules, ensuring that MySQL recognizes your changes right away.

When you’re finish, type: to see whether you can log into the MySQL console.

$ sudo mysql

The use of sudo when performing this command infers that this will connect to the MySQL server as the administrator database user root. This is what you should get as a result:

img7

To exit the MySQL console, type:

mysql> exit

Even though you created a password while executing the mysql_secure_installation script, you didn’t need to supply one to join as the root user. Because the administrator MySQL user’s default authentication mechanism is uni_socket rather than password, this is the case. Although this may appear to be a security risk at first glance, it really strengthens the database server’s security. Since the only people who can log in as the root MySQL user are system users with sudo rights connecting through the console or through an application with the same capabilities.

In practice, this means you won’t be able to connect from your PHP application using the administrative database root user. If the default authentication method is changed from unix_socket to password, setting a password for the root MySQL account serves as a protection.

It’s recommend to set up separate user accounts with fewer rights for each database, especially if you intend on hosting numerous databases on your server, for added protection.

Your MySQL server is now up and running, as well as secure. The final component of the install LAMP stack on Ubuntu is PHP, which will be install next.

Step 03: Installing PHP – install Lamp on Ubuntu

PHP is the part of your system that processes code in order to show dynamic content. It can execute scripts, connect to your MySQL databases for data, and then provide the processed material to your web server, which will show the results to your visitors.

Use the apt system to install PHP once again. You’ll also need libapache2-mod-php to integrate PHP into Apache, and the php-mysql package to link PHP to MySQL databases, in addition to the PHP package. To install all three packages and their dependencies, use the following command:

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

This should complete the installation of PHP without any issues.

Step 04: Setting Up a Virtual Host

You may use virtual hosts (similar to server blocks in Nginx) to encapsulate configuration data and host many domains from a single Apache webserver. We’ll create a domain named your_domain, but you should use your own domain name instead.

One server block is enabled by default on Ubuntu 18.04, and it is set to serve content from the /var/www/html directory. While this is fine for a single site, it might become cumbersome if you have numerous sites to host. Let’s establish a directory structure within /var/www for your domain site instead of changing /var/www/html, leaving /var/www/html as the default directory to be deliver if a client requests doesn’t match any other sites.

Make the following directory for your domain:

$ sudo mkdir /var/www/your_domain

Then, using the $USER environment variable, which refers to the currently logged-in user, assign ownership of the directory:

$ sudo chown -R $USER:$USER /var/www/your_domain

If you haven’t changed the mask value for your webroot directory, the permissions should be correct, but you may double-check by typing:

$ sudo chmod -R 755 /var/www/your_domain

Next, create a sample index.html page using nano or your favorite editor:

$ nano /var/www/your_domain/index.html

Inside, add the following sample HTML:

<html>
    <head>
        <title>Welcome to Your_domain!</title>
    </head>
    <body>
        <h1>Success!  The your_domain server block is working!</h1>
    </body>
</html>

Save and close the file when you are finish.

Creating new config file for virstual host

It’s important to construct a virtual host file with the relevant directives for Apache to deliver this content. Let’s create a new configuration file at /etc/apache2/sites-available/your domain.conf instead of altering the default configuration file at /etc/apache2/sites-available/000-default.conf:

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

Replace the default configuration block with the following, which is adjust for our new directory and domain name: /etc/apache2/sites-available/your_domain.conf

<VirtualHost *:80>
    ServerAdmin [email protected]
    ServerName your_domain
    ServerAlias www.your_domain
    DocumentRoot /var/www/your_domain
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

You’ll see that we’ve changed the DocumentRoot to our new directory and the ServerAdmin to an email address that the site administrator for your domain can access. We’ve also added two directives: ServerName, which specifies the base domain that this virtual host definition should match, and ServerAlias, which specifies additional names that should match as if they were the base name.

When you’re finish, save and close the file.

Let’s enable the file with the a2ensite tool:

$  sudo a2ensite your_domain.conf

Disable the default site defined in 000-default.conf:

$ sudo a2dissite 000-default.conf

Next, let’s test for configuration errors:

$ sudo apache2ctl configtest

You should see the following output:

img8

Restart Apache to implement your changes:

$ sudo systemctl restart apache2

Your domain name should now be serve by Apache. You may verify this by going to http://your_domain and seeing something like this:

img9

That completes the setup of your virtual host. However, before making any further modifications or deploying an application. It’s a good idea to run a test on your PHP settings to see if there are any issues that need to be solve.

Step 05: Testing PHP Processing on your Web Server

Create a PHP script named info.php to see if your machine is correctly configure for PHP. This file must be save to your webroot directory in order for Apache to find it and serve it appropriately.

Run the following command in the webroot you created in the previous stage to generate the file:

$ sudo nano /var/www/your_domain/info.php

This will open a blank file. Add the following text, which is valid PHP code, inside the file: /var/www/your_domain/info.php

<?php
phpinfo();

Save and close the file when you complete the process.

Now you can see if your web server can show the material created by this PHP script successfully. To give it a go, go to this page in your browser. You’ll need the public IP address or domain name of your server once again.

You should go to the following address:

http://your_domain/info.php

Conclusion

Now that you’ve set up a LAMP stack, you have a lot of options for what to do next. You’ve set up a platform on your server that allows you to run most types of websites and web software.

That’s all for this article if you have any confusion contact us through our website or email us at [email protected] or by using LinkedIn

Suggested Articles:

  1. How to setup local WordPress environment on Ubuntu

Leave a Comment