Introduction
The LAMP stack is an open-source web development stack used to deploy and run web-based applications and dynamic websites. Typically, the stack is made up of an Operating system, web server, database server, and a programming language; a blend that powers a commendable portion of the modern internet world. LAMP stands for Linux (Operating System), Apache (web server), MySQL/MariaDB (databases), PHP/Perl/Python (Programming language).
Traditionally, MySQL has been used as the database server in the LAMP stack. However, things have changed, and MariaDB has become more and more common. MariaDB is an excellent drop-in replacement for open-source purists looking to avoid Oracle’s MySQL. This tutorial will help you install Apache, MariaDB, and PHP on Ubuntu 18.04 LTS server. .
Assuming you have a good VPS hosting or cloud VPS hosting, let’s get started!
Prerequisites
Before you proceed, the following requirements must be in place.
- An Ubuntu 18.04 VPS or dedicated server
- A non-root user with sudo
Step 1: Installing the Apache2 Web Server
The first step when installing the LAMP stack is to install a web server. In our case, we’ll install the Apache2 web server Using the Ubuntu apt package manager.
First, execute the command below to update your package index:
$ sudo apt update
Next, run the command below to install Apache2:
$ sudo apt install apache2
Adjusting the Firewall Setting
Once you install Apache web server, the next thing is adjusting the firewall settings to permit HTTP and HTTPS web traffic.
Run the command below to check the ufw application profiles:
$ sudo ufw app list
This will give you the output below:
Available applications: Apache Apache Full Apache Secure OpenSSH
Now, if you inspect Apache Full profile, you will notice that it enables web traffic to both port 80 and port 443. To examine this profile, run the command below:
$ sudo ufw app info "Apache Full"
The output will be:
Profile: Apache Full Title: Web Server (HTTP,HTTPS) Description: Apache v2 is the next generation of the omnipresent Apache web server. Ports: 80,443/tcp
To permit inbound HTTP and HTTPS web traffic on both port 80 and port 443, execute the command below:
$ sudo ufw allow in"Apache Full"
To check if all went as planned, visit the public IP address of server through the web browser.
http://your_server_ip
This should give you a default Apache web page for Ubuntu 18.04. The page is available for testing and informational purposes, and it should be something similar to this:
This page shows that the Apache web server is successfully installed and easily accessible via the firewall.
Step 2 Installing MariaDB
By now, the Apache web server is correctly configured, its time we install the database management system. As aforementioned we’ll deviate from the norm and install MariaDB database instead of Oracle’s MySQL.
Run the command below to install MariaDB:
$ sudo apt install mariadb-server
Next, execute the command below to verify the installation:
$ sudo systemctl status mariadb
This will give you the output below:
â—mariadb.service-MariaDB10.1.34databaseserver Loaded: loaded(/lib/systemd/system/mariadb.service;enabled;vendorpreset Active: active(running)sinceSat2018-08-2503:26:34UTC;1weeksodaysago Docs: man:mysqld(8) : https://mariadb.com/kb/en/library/systemd/ Main PID: 12761(mysqld) Status: "Taking your SQL requests now..." Tasks: 27(limit:1152) CGroup: /system.slice/mariadb.service └─12761/usr/sbin/mysqld
Next, run the command below to verify the version of MariaDB:
$ mysql -V
This will give you an output similar to the one below:
mysql Ver 15.1Distrib 10.1.34-MariaDB, fordebian-linux-gnu (x86_64) usingreadline 5.2
Securing MariaDB
Now, your database management system (MariaDB) is up and running but its not secured. The next step is ensuring MariaDB is secure. Execute the commands below to secure MariaDB shell:
$ sudo mysql_secure_installation
Note: You will be requested to enter your current root user password. However, since you have just installed MariaDB, and have not set any password yet, the password is blank. Just Press ENTER to continue.
You will also be prompted to answer some question. Every step is detailed and should not be hard to follow. It recommended you pick Yes [Y] for all the answers. Here are the questions:
Set root password? [Y/n] type Y and press enter
New Password: Key in your preferred password and enter to continue.
Re-enter new password: Re-enter the password and press enter.
Remove anonymous users? [Y/n] type Y and press enter.
Disallow root login remotely [Y/n] type Y and press enter
Remove test database and access it? [Y/n] type Y and press enter
Reload privilege tables now? [Y/n] type Y and press enter
This will give you the output below:
All Done! If you've completed all of the above steps, your MariaDB installation should now be secure. Thank you for using MariaDB!
Step 3 Installing PHP
PHP is another layer of the LAMP stack that processes code to present dynamic content. It’s a vital component that can connect to the MariaDB database, run scripts, and relay processed content for the web server to display.
Run the command below to install PHP:
$ sudo apt install php libapache2-mod-php php-mysql
Modifying Apache settings
Once you install PHP, you should change the way Apache presents files whenever a directory is asked for. By default, Apache first looks for index.html file whenever a user asks for a directory. However, we want Apache web server to look for a PHP file called index.php first whenever a directory is requested.
To make this modification, we’ll have to edit the dir.conf file. Run the command below to open this file:
$ sudo nano /etc/apache2/mods-enabled/dir.conf
This is the file content:
<IfModule mod_dir.c> DirectoryIndex index.html index.cgi index.pl index.php index.xhtml index.htm </IfModule>
To complete the changes, move the index.php, just before index.html, to have the following:
<IfModule mod_dir.c> DirectoryIndex index.php index.html index.cgi index.pl index.xhtml index.htm </IfModule>
Save the file and press CTRL +X to close. Type Y and press ENTER to confirm the changes.
Next, Run the command below to restart your Apache web server.
$ sudo systemctl restart apache2
Now, we’ll use systemct1 to examine Apache2 status. To achieve this, run the command below:
$ sudo systemctl status apache2
This will give you an output similar to the one below:
apache2.service - LSB: Apache2 web server Loaded: loaded (/etc/init.d/apache2; bad; vendor preset: enabled) Drop-In: /lib/systemd/system/apache2.service.d └─apache2-systemd.conf Active: active (running) since Tue 2018-04-2314:28:43 EDT; 45s ago Docs: man:systemd-sysv-generator(8) Process: 13581 ExecStop=/etc/init.d/apache2 stop (code=exited, status=0/SUCCESS) Process: 13605 ExecStart=/etc/init.d/apache2 start (code=exited, status=0/SUCCESS) Tasks: 6 (limit: 512) CGroup: /system.slice/apache2.service ├─13623 /usr/sbin/apache2 -k start ├─13626 /usr/sbin/apache2 -k start ├─13627 /usr/sbin/apache2 -k start ├─13628 /usr/sbin/apache2 -k start ├─13629 /usr/sbin/apache2 -k start └─13630 /usr/sbin/apache2 -k start
Installing PHP Modules
Next, we need to install additional modules to enhance PHP modules. First, lets check the available PHP modules; we’ll use apt to search the modules
$ sudo apt-cachesearch php7.2
This will give you a list of the available PHP7.2 extensions. Each extension comes with a short description. If you find a module that you need to install, use the apt command to install it. For instance, if you want to install php7.2-cli module, execute the command below:
$ sudo apt install php7.2-cli
To install multiple modules all at once, use the apt command and type the modules and separate them using space.
$ sudo apt install package1 package2 ...
By, now the LAMP stack is successfully installed and configured on your Ubuntu 18.04. But, before you make any change or deploy any application, it’s advisable you test your PHP processing.
Testing PHP Processing
To test whether you web server is properly configured for PHP, we create a PHP script; info.php. For Apache to find the file and process it correctly, it should be saved in the web root directory. The web root directory is /var/www/htm/. Now, execute the command below to create a file in this locations:
$ sudo nano /var/www/html/info.php
Once the blank file is opened, add the text below:
<?php phpinfo(); ?>
Save and exit the file
Next, visit the address:
http://your_server_ip/info.php
This should open a page similar to the one below:
The page above shows that PHP is functioning as expected. Now, run the command below to remove the above file, as it could give unauthorized users access to your server information.
$ sudo rm /var/www/html/info.php
The page can be recreated when needed.
Conclusion
That’s it! You have successfully installed the LAMP stack. You should be able to install your desired web software and websites on your Ubuntu 18.04 server without a struggle.
Check out these top 3 Linux hosting services
- Do you need the best VPS? Read about our different offers.