Designly Blog

How to Install a Production LAMP Server

How to Install a Production LAMP Server

Posted in IT & Infrastructure by Jay Simons
Published on February 13, 2022

You may have heard the term before: LAMP. It stands for Linux-Apache-Mysql-PHP, where Linux is the server operating system, Apache is the HTTP server, Mysql is the database server, and PHP is the hypertext pre-processor / scripting interpreter. These applications work together in unison to create dynamic, data-driven web applications and is sometimes referred to as the LAMP Stack.

You can deploy your own LAMP server in just under a half an hour using Google's Compute Engine. You'll need a valid Google account and you'll also need to setup an organization and billing account on Google Cloud.

So, without further ado, here is the tutorial video. A full explanation can be found below.

<iframe allow="autoplay; fullscreen" allowfullscreen="" frameborder="0" height="564" src="https://player.vimeo.com/video/677310710?h=b2ddad37f2" width="640"></iframe>

Create a new Google Cloud Project

First, we need to create a new project in the Cloud Console. Click the project drop down near the upper left of the screen. This will bring up a list of projects.

Then click Create Project

Create New Project
Create New Project

Now click on the left menu bar and scroll down to Compute Engine and then select VM Instances.

Go to VM Instances
Go to VM Instances

Enable the Compute Engine.

Enable Compute Engine
Enable Compute Engine

Go back to VM Instancesand launch a new Instance.

Launch New Instance
Launch New Instance

Name your instance and then scroll down to Boot Disk and click Change.

Change Boot Disk
Change Boot Disk

Select Ubuntu for operating system and Ubuntu 20.04 LTS for version.

Select OS and Version
Select OS and Version

Then scroll down to Firewall and check Allow HTTP and Allow HTTPS.

Allow HTTP and HTTPS
Allow HTTP and HTTPS

Now, save your instance and wait for it to launch. A green circle-check will appear once it's live. Notice the public IP address to the right. You'll want to copy that and create an A record with your DNS provider so our server can have a public hostname. Once that's set up, click the SSH button to launch the web-based Secure Shell.

Copy IP and Launch SSH
Copy IP and Launch SSH

Upgrade the server software

Run the following command:

sudo apt-get update; sudo apt-get upgrade -y; sudo apt-get dist-upgrade -y

This will take some time to complete.

Install tasksel and the LAMP stack

Run:

sudo apt install -y tasksel

Then:

sudo tasksel install lamp-server

Tasksel is an app that will automate the installation and configuration of our LAMP stack.

Set up MySQL for production

Run:

sudo mysql_secure_installation

Enter n for the first question. Then choose a root passwordand confirm. Then enter y for the remaining questions.

Create a MySQL superuser

Run:

sudo mysql

Enter the following queries:

CREATE USER 'your-name'@'localhost' IDENTIFIED BY 'your-password';
GRANT ALL PRIVILEGES ON *.* TO 'your-name'@'localhost' WITH GRANT OPTION;
FLUSH PRIVILEGES;
quit

Now let's see if our server is working. Type the hostname you chose for the IP address in the address bar of your web browser. You should see the default Apache It Works page:

Default Apache Page
Default Apache Page

Set up Apache VirtualHost

Go to:

cd /etc/apache2/sites-enabled/

Remove the default configuration file:

sudo rm -f 000-default.conf

Then go to:

cd ../sites-available/

And create a new configuration file:

sudo nano your-host-name.conf

Copy the following configuration and paste it into the SSH session:

<VirtualHost *:80>
    ServerName example.com
    ServerAdmin [email protected]
    DocumentRoot /home/{{YOUR USER NAME}}/wwwroot/pub
    ErrorLog ${APACHE_LOG_DIR}/example.com-error.log
    CustomLog ${APACHE_LOG_DIR}/example.com-access.log combined
    <Directory "/home/{{YOUR USER NAME}}/wwwroot/pub">
        Options FollowSymLinks
        AllowOverride None
        Require all granted
    </Directory>
</VirtualHost>

To save, hit CTRL-X, then Y, then ENTER.

Now we need to enable some Apache modules:

sudo a2enmod rewrite
sudo a2enmod ssl

Then reload the Apache configuration:

sudo systemctl reload apache2

Create Our Root Directory

Type:

cd
mkdir wwwroot
cd wwwroot
mkdir pub

Now let's make our default index page:

nano index.php

Enter the following code:

<?php
phpinfo();
?>

Then hit CTRL-X, Y, ENTER.

Now reload your page and you should see the PHP info page:

Default PHP Page
Default PHP Page

Secure With Let's Encrypt's Certbot

Run:

sudo snap install core; sudo snap refresh core

Then:

sudo snap install --classic certbot

Now we need to create a symlink so we can run the command from our execute path:

sudo ln -s /snap/bin/certbot /usr/bin/certbot

Now run certbot:

sudo certbot --apache

Enter your admin email address, then hit Yif you accept the Terms and Conditions. You can opt to receive news from the Electronic Frontier Foundationby hitting Yor N. Personally, I get enough spam, so I chose no.

Now a list of Apache virtualhosts will appear. You can simply hit enter to select the all, or chose the number associated with the list. You can choose multiple numbers by separating them by a space, but we should only have one at this point.

Hit enter and certbot will work it's magic. It will verify your site automaticall by creating a token in your web server public directory, download it, and then issue you an SSL certificate valid for 45 days. This is a much shorter period than commercial SSL certificates, and that's for two reasons: One, they want to reduce the number of orphaned certificates in their database, and Two, they want to continually verify that your site is in good standing.

Now refresh your page and it should automatically redirect you from http to https. If not, just add the s to your URL in the address bar. You should see the Lock Icon now to the left of the address bar. Click on that to view your certificate and see that it is valid!

Install PHPMyAdmin

We're going to want a nice web-based UI to manage our database. You could do it from the command line, but it's cumbersome and tedious. There are several MySQL/MariaDB management web apps, but I"m partial to PHPMyAdmin, probably because it's the one I've used for years and years and I don't want to change! But there are others that may be better.

Run:

sudo apt install -y phpmyadmin

Now go back to your web page and append /phpmyadmin to your URL. Hopefully, the PHPMyAdmin login page will appear and you can log in with the superuser account we created.

Well, I hope everything went according to plan and you now have a fully-functional LAMP server. Thanks for reading and/or viewing and look forward to more to come!

Happy coding! :-)


Loading comments...