How to Install and Configure a LAMP Server on Ubuntu Server

According to Netcraft, Apache is the second-most used web server on the planet as of October 2019. In the first place, a mere 4 percentage points ahead, is NGINX. Both of these servers run on Linux. In the case of Apache, it makes up the A in LAMP. The complete LAMP stack includes:

  • Linux
  • Apache
  • MySQL
  • PHP

This combination of pieces makes for an incredibly powerful and flexible web server. With these bits in place, you can serve up static and dynamic content with ease.

But how is a LAMP server installed and configured? I’m going to show you just that. I’ll be doing so on the Ubuntu Server 18.04 platform.

What You’ll Need

To make this happen, you’ll need to have a running instance of Ubuntu Server 18.04 and a user account with sudo privileges. With these two pieces in hand, let’s get that LAMP server up and running.

Update/Upgrade

The first thing to do is to update and upgrade your Ubuntu Server. By taking care of this yourself, you can rest assured that any outsource quality assurance company (if applicable) will give your server operating system a thumbs up. To update and upgrade Ubuntu, log in with an account that has sudo privileges and issue the following commands:

sudo apt-get update

sudo apt-get upgrade

Check the list of all the apps that are included in the upgrade (Figure 1). If you note the kernel listed, know that you’ll have to reboot the server, in order for that particular upgrade to take effect. Because of this, make sure you run the update/upgrade at a time when a reboot is feasible.

Figure 1

The kernel is listed, so a reboot would be necessary.

When prompted, type “y” (no quotes) to okay the upgrade.

Installing AMP

Next, you can install the AMP portion of LAMP. You’ll be surprised to find out this can be done with a single command. That’s right, one command will install Apache, MySQL, and PHP. This command will also install all of the necessary dependencies and a number of PHP extensions. What is that magical command? From the terminal, issue:

sudo apt-get install lamp-server^

The carrot in the command indicates that this is a group install, which will take care of everything necessary for the LAMP server installation. However, there are a couple of configurations you’ll need to take care of.

Configure MySQL

By installing the LAMP server with this method, the MySQL database installation isn’t secured. In order to work with MySQL with any level of security, you must create an admin password. To do that, issue the command:

sudo mysql_secure_installation

The first thing you will be asked is if you want to include the MySQL VALIDATE PASSWORD PLUGIN (Figure 2). What this plugin does is require strong passwords for all MySQL users. For anyone concerned with security, it’s a good idea to enable it.

Figure 2

Enabling the MySQL Validate Password Plugin.

Next, you’ll be asked to type and verify a new password for the MySQL admin user (Figure 3).

Figure 3

Creating a password for the MySQL admin user.

You will then be asked the following questions:

  • Remove anonymous users?
  • Disallow root login remotely?
  • Remove test database and access to it?
  • Reload privilege tables now?

Answer yes (by typing y) for all of the above questions unless you have a powerful reason to leave one or several of those options available.

What’s Next

Point a web browser to http://SERVER_IP (Where SERVER_IP is the IP address of the LAMP server) and you should see the Apache welcome screen (Figure 4).

Figure 4

The Apache welcome screen.

At this point, you’re probably wondering where you can configure your new sites. This is done from within the /etc/apache2/sites-available folder. Let’s create one. First, we’re going to create a new directory in /var/www/html/ and name it test. Issue the command:

sudo mkdir /var/www/html/test

Now we’ll create a test index file with the command:

sudo nano /var/www/html/test/index.html

In that file, paste the following contents:

<h1>This is my first Apache site!</h1>

Save and close the file.

Now create a configuration file for the new site with the command:

sudo nano /etc/apache2/sites-available/test.conf

In that file, paste the following:

Alias /test “/var/www/html/test/”

<Directory /var/www/html/test/>

Options +FollowSymlinks

AllowOverride All

<IfModule mod_dav.c>

Dav off

</IfModule>

SetEnv HOME /var/www/html/test

SetEnv HTTP_HOME /var/www/html/test

</Directory>

Save and close the file.

Next, enable the site with the command:

sudo a2ensite test

Reload Apache with the command:

sudo systemctl reload apache2

Go back to your web browser and point it to http://SERVER_IP/test (Where SERVER_IP is the IP address of the LAMP server) and you should see the index.html we created (Figure 5).

Figure 5

Our new index.html file on display.

Congratulations, you’ve just installed a LAMP server and created your first (albeit very basic) web site. If you so desire, you can always have Q&A testing services take a look at your work, but if you’re seeing the index.html page, you can trust that your work is spot on.

ORIGINALLY PUBLISHED ON

in

Software

Leave a Comment