1. Academy
  2. Compute & Instances

How to create a LAMP webserver

Introduction 

LAMP stands for Linux, Apache, MySQL and PHP. Together they provide a set of software to deliver a web application. For LEMP, instead of using Apache, it uses nginx. 

In this tutorial we show you how to set up your own website.

Prerequisites

For this tutorial you need the following:

  • Active fuga Cloud Account
  • Domain name you would like to use
  • An Instance OR create a new one by using Cloud-init.

https://my.fuga.cloud/

Step 1 - install the packages 

With Cloud-init

Go to the fuga.cloud dashboard, and Create Instance.

Select under Boot Source, a distribution (in this tutorial Ubuntu 20.04 LTS is used) 
Select a Boot disk size, flavor, network, key pair, security group, and give your instance a name.

Next unfold the Advanced Settings and deploy your custom script.

#cloud-config
apt_update: true
apt_upgrade: true
packages:
- apache2
- php-mysql
- libapache2-mod-php
- php-gd
 - php-curl

Without Cloud-init

If you already have an instance running that you want to use, you can install the packages by running the following command:

$ sudo apt install {apache2,php-mysql,mysql-server,libapache2-mod-php,php-gd,php-curl}

You can check the status of apache2, to see if it's running.

$ sudo systemctl status apache2

Step 2 - Create a directory structure

This will be the location where the website files will be stored. To create the directories for your website use the following command:

$ sudo mkdir -p /var/www/<domain.com>

When you have done that, you need to make an index to store the code.

$ sudo nano /var/www/<domain.com>/index.html

Opening nano you can enter the following code to have a small head start on your journey.

<html>
       <head>
       <titel> Welcome to your own website</title>
       </head>
       <body>
       <h1>Success! Thanks to fuga tutorials</h1>
       </Body>
</html>

Create read permissions for the domain directory using.

$ sudo chmod -R 755 /var/www

Step 3 - Set up the virtual host

Let’s start the configuration. To go there use the following command.

$ cd /etc/apache2/site-available/

Next you need to make a copy of the 000-default.conf file, to make it organized, name the copy to the domain name. 

$ sudo cp 000-default.conf  <domain.com>.conf

Modify the .conf file you’ve just copied, by using:

$ sudo nano <domain.com>.conf

This is the virtual host, in here you need to add ServerName and ServerAlias, this will make the site available. Change the direction of the DocumentRoot to your domain index file. Add the following:

ServerAdmin   admin@<domain.com>
ServerName <domain.com>
ServerAlias <www.domain.com>
DocumentRoot /var/www/<domain.com>

Save and exit the configuration file, and manually enable your website by using the command:

$ sudo a2ensite <domain.com>.conf 

While enabling the domain configuration file, you need  to disable the default configuration file by using:

$ sudo a2dissite 000-default.conf

Now reload Apache

$ sudo systemctl reload apache2

You’re done with apache for now, and need to open port 80 of the server firewall.

Check the status of the firewall by using:

$ sudo ufw status

You see the firewall is inactive, you need to enable it. To do so, you need to add port 22 (SSH) first, it may disrupt the existing SSH connection. Or in a worst-case scenario, you might not be able to login again.

$ sudo ufw allow 22
$ sudo ufw enable

For adding port 80 use:

$ sudo ufw add 80

By enabling and adding port 80, it is possible to view your site. Just type your http://domain.com:80 in a browser or use the IP address of the server.

Conclusion

You now know how to set up and configure a web server by using Apache2 You also learned how to enable the UWF and open port 80 so you can see your domain.