You need to enable Javascript to enjoy the finer things in life... ;)

Setting up a LAMP Stack using Ubuntu 20.04 LTS

LAMP Stack basically means Linux + Apache + MySQL + PHP/Python/Perl. This is one of the most popular choice when hosting websites written in PHP. This stack can be extended to support any other web scripting languages as well. In this tutorial, I will be setting up a fully functional LAMP Stack with a simple PHP website.

Basic Assumptions

Before following this tutorial, below assumptions will be taken:

  1. Ubuntu OS is installed properly
  2. The server is connected to the internet with an IPv4/IPv6 address
  3. Root access to the server

Let’s Start

Introducing Apache 2

Apache 2 is one of the most popular open source web server application for handling HTTP requests. It runs by default using port 80 for HTTP and 443 for HTTPS. This configuration is set up by default when installing Apache 2. Apache 2 also supports features like, Virtual Hosting, User Directories, etc. and is quite versatile.

Installing Apache 2

Simply install Apache2 by using the following command:

sudo apt update
sudo apt install apache2

The main configuration file is stored at /etc/apache2/apache2.conf. Apache 2 sets some default values in this file, that can be changed accordingly.

Configure Apache 2 to serve only PHP and HTML pages

By default, Apache 2 opens almost all types of web scripts when serving pages. It is recommended to only allow the scripts that are getting used. Since, in this tutorial we are only going to use PHP and HTML, we will remove all the other entries. This can be achieved by editing the following file:

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

Just remove the other index file types from the DirectoryIndex tag. The only allowed files should be index.php, index.html and index.htm

Add ServerName and ServerAdmin fields to the Apache 2 Configuration File

By default, apache doesn’t set the value of ServerName in the configuration file. This field designates the Server a name for showing to the world. Also, apache sets a default value for the ServerAdmin field in the configuration file. This field is used to designate the webmaster’s email address to the world.

The ServerName field can added to the configuration at Line 70 of the /etc/apache2/apache2.conf file. Just add the following line there with the server name:

ServerName servername.com

Similarly, the ServerAdmin field can be found at Line 11 of the same file. Just update the following line with the webmaster email address:

ServerAdmin [email protected]

Restarting Apache 2 Service

The Apache 2 service needs to restarted to apply the changed settings in the configuration files. The apache service can be restarted using the following command:

sudo service apache2 restart

After executing the above command, the configuration file changes will be applied to the service and it will be restarted.

Accessing the Apache Web Page

The default apache web page can be accessed by just opening the ip using any browser with an Internet access. The default location of the site web files is:

/var/www/html

Just remove the index.html and replace it with your own index.html file.

This concludes the basic setup of Apache 2 in Ubuntu 20.04. Now, the next step is to install MySQL (mariaDB ) for maintaining backend databases.

Introducing MySQL (mariaDB)

MySQL is one of the most popular database technologies used by the modern web sites. After Oracle made all future versions of MySQL proprietary, mariaDB was developed following the existing MySQL code. We will be setting up mariaDB for managing MySQL Databases in this tutorial.

Installing MariaDB

MariaDB can be installed using the following command:

sudo apt update
sudo apt install mariadb-server

Initial MySQL Setup

After installing mariaDB, the initial MySQL setup can be run by using the following command:

sudo mysql_secure_installation

During the initial setup, it is recommended to follow the following guidelines:

  1. Set a unique and complex MySQL root password.
  2. Remove the anonymous users
  3. Disallow remote root login unless you actually need it.

After setting up the MySQL server. Execute the below command to login to the MySQL shell:

sudo mysql -u root -p

The MySQL database can now be managed using using this shell.

This concludes the basic setup of MySQL (mariaDB) in Ubuntu 20.04. Next we will set up PHP 7.4 for server side scripting. This will enable us to execute PHP file for server side processing.

Introducing PHP

PHP is one of the most popular server side scripting language for hosting websites and it acts as the foundation for some of the most popular web applications. In this tutorial, we will install PHP 7.4 and set it up so that the server can execute PHP files.

Installing PHP

PHP can be installed for Apache 2 in Ubuntu 20.04 using the following command:

sudo apt install php libapache2-mod-php php-common php-mysql php-curl php-cgi

After installation, restart the Apache 2 service using the following command:

sudo service apache2 restart

After this, the server can now execute PHP scripts. Just to test, the following command will show create a PHP file, that will show all the relevant PHP information when run from the browser:

echo '<?php phpinfo();?>' > /var/www/html/phpinfo.php

Running http://youripaddress/phpinfo.php, will now show you the full installed PHP information.

Extending PHP using extensions

PHP can be extended using extensions. For example, to allow PHP to support MySQL we need to install the following extension:

sudo apt install php-mysql

Similarly, curl can be enabled by installing the following extension:

sudo apt install php-curl

Configuring PHP installation using php.ini

PHP can be further customized by editing the php.ini file. For this purpose, first enable configuration for PHP 7.4 using the following command:

sudo a2enconf php7.4-cgi

Then edit the following file:

/etc/php/7.4/apache2/php.ini

Just go through the file and edit as needed. Though, it is recommended to leave the file in its default setting.

After updating the file, just restart Apache 2 service to complete the changes:

sudo service apache2 restart

Conclusion

The server is now configured to handle HTTP Requests to PHP files, and has a database engine for Database Management. I will share more advanced concepts of LAMP stack such as SSL, Virtual Hosting, etc. in future tutorials.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.