Install Apache, MariaDB, PHP (LAMP stack) In Ubuntu 18.04 LTS Server
As you may know already, LAMP stack is a popular, open source web development platform that can be used to run and deploy dynamic websites and web-based applications. Typically, LAMP stack consists of Apache webserver, MariaDB/MySQL databases, PHP/Python/Perl programming languages. LAMP is the acronym of Linux, MariaDB/MYSQL, PHP/Python/Perl. In this tutorial, we will see how to install Apache, MariaDB, PHP (LAMP stack) in Ubuntu 18.04 LTS server.
Install Apache, MariaDB, PHP (LAMP stack) in Ubuntu 18.04 LTS Server
For the purpose of this tutorial, I will be using the following testbox.
- Operating System : Ubuntu 18.04 64 bit LTS server
- IP address : 192.168.225.22/24
1. Install Apache webserver
To install Apache web server, run the following command from the Terminal:
$ sudo apt install apache2
Check if Apache web server is running or not:
$ sudo systemctl status apache2 ● apache2.service - The Apache HTTP Server Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled) Drop-In: /lib/systemd/system/apache2.service.d └─apache2-systemd.conf Active: active (running) since Fri 2018-07-06 11:42:35 UTC; 27s ago Main PID: 2079 (apache2) Tasks: 55 (limit: 2322) CGroup: /system.slice/apache2.service ├─2079 /usr/sbin/apache2 -k start ├─2081 /usr/sbin/apache2 -k start └─2082 /usr/sbin/apache2 -k start Jul 06 11:42:35 ubuntuserver systemd: Starting The Apache HTTP Server... Jul 06 11:42:35 ubuntuserver systemd: Started The Apache HTTP Server.
Apache service is loaded and running!
1.1 Adjust firewall to allow Apache web server
By default, the apache web browser can’t be accessed from remote systems if you have enabled the UFW firewall in Ubuntu 18.04 LTS. You must allow the http and https traffic via UFW by following the below steps.
First, let us view which applications have installed a profile using command:
$ sudo ufw app list Available applications: Apache Apache Full Apache Secure OpenSSH
As you can see, Apache and OpenSSH applications have installed UFW profiles.
If you look into the “Apache Full” profile, you will see that it enables traffic to the ports 80 and 443:
$ sudo ufw app info "Apache Full" 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
Now, run the following command to allow incoming HTTP and HTTPS traffic for this profile:
$ sudo ufw allow in "Apache Full" Rules updated Rules updated (v6)
If you don’t want to allow https traffic, but only http (80) traffic, run this command instead:
$ sudo ufw app info "Apache"
Now, open up your web browser and navigate to http://localhost/ or http://IP-Address/.
If you are see a screen something like above, you are good to go. Apache server is working!
2. Install MariaDB
MariaDB is the drop-in replacement of MySQL database server.
To install it, run:
$ sudo apt install mariadb-server mariadb-client
The version of MariaDB in the Ubuntu official repositories might be outdated. If you want to install a latest MariaDB, add the MariaDB official repository for Ubuntu and install it as shown below.
First, add MariaDB repository and import the key as shown below.
$ sudo apt-get install software-properties-common $ sudo apt-key adv --recv-keys --keyserver hkp://keyserver.ubuntu.com:80 0xF1656F24C74CD1D8 $ sudo add-apt-repository 'deb [arch=amd64,arm64,ppc64el] http://sgp1.mirrors.digitalocean.com/mariadb/repo/10.3/ubuntu bionic main'
After adding the repository, run the following commands to install MariaDB.
$ sudo apt update
$ sudo apt install mariadb-server
Verify if MariaDB service is running or not using command:
$ sudo systemctl status mysql
● mariadb.service - MariaDB database server Loaded: loaded (/lib/systemd/system/mariadb.service; enabled; vendor preset: enabled) Active: active (running) since Fri 2018-07-06 11:52:12 UTC; 2min 39s ago Main PID: 3869 (mysqld) Status: "Taking your SQL requests now..." Tasks: 27 (limit: 2322) CGroup: /system.slice/mariadb.service └─3869 /usr/sbin/mysqld Jul 06 11:52:13 ubuntuserver /etc/mysql/debian-start: mysql Jul 06 11:52:13 ubuntuserver /etc/mysql/debian-start: performance_schema Jul 06 11:52:13 ubuntuserver /etc/mysql/debian-start: Phase 6/7: Checking and upgrading tables Jul 06 11:52:13 ubuntuserver /etc/mysql/debian-start: Processing databases Jul 06 11:52:13 ubuntuserver /etc/mysql/debian-start: information_schema Jul 06 11:52:13 ubuntuserver /etc/mysql/debian-start: performance_schema Jul 06 11:52:13 ubuntuserver /etc/mysql/debian-start: Phase 7/7: Running 'FLUSH PRIVILEGES' Jul 06 11:52:13 ubuntuserver /etc/mysql/debian-start: OK Jul 06 11:52:13 ubuntuserver /etc/mysql/debian-start: Checking for insecure root accounts. Jul 06 11:52:13 ubuntuserver /etc/mysql/debian-start: Triggering myisam-recover for all MyISAM tables and aria-recover for all Aria tables
Mysql is running!
2.1 Setup database administrative user (root) password
During MariaDB installation, It will set password for the administrative user account (root).
If you try to setup password manually using command:
You can’t login set the password. You will see an error like below.
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY! In order to log into MariaDB to secure it, we'll need the current password for the root user. If you've just installed MariaDB, and you haven't set the root password yet, the password will be blank, so you should just press enter here. Enter current password for root (enter for none): ERROR 1698 (28000): Access denied for user 'root'@'localhost' Enter current password for root (enter for none):
To fix this problem, log in as MySQL database administrator using command:
$ sudo mysql -u root
After logging into the MySQL prompt, run the following commands one by one.
update user set plugin='' where User='root';
Now, you can set database administrative password using command:
Enter password password, and hit ENTER key to accept the default values.
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY! In order to log into MariaDB to secure it, we'll need the current password for the root user. If you've just installed MariaDB, and you haven't set the root password yet, the password will be blank, so you should just press enter here. Enter current password for root (enter for none): # Press ENTER OK, successfully used password, moving on... Setting the root password ensures that nobody can log into the MariaDB root user without the proper authorisation. Set root password? [Y/n] # Press ENTER New password: # Enter password Re-enter new password: # Re-enter password Password updated successfully! Reloading privilege tables.. ... Success! By default, a MariaDB installation has an anonymous user, allowing anyone to log into MariaDB without having to have a user account created for them. This is intended only for testing, and to make the installation go a bit smoother. You should remove them before moving into a production environment. Remove anonymous users? [Y/n] # Press ENTER ... Success! Normally, root should only be allowed to connect from 'localhost'. This ensures that someone cannot guess at the root password from the network. Disallow root login remotely? [Y/n] # Press ENTER ... Success! By default, MariaDB comes with a database named 'test' that anyone can access. This is also intended only for testing, and should be removed before moving into a production environment. Remove test database and access to it? [Y/n] # Press ENTER - Dropping test database... ERROR 1008 (HY000) at line 1: Can't drop database 'test'; database doesn't exist ... Failed! Not critical, keep moving... - Removing privileges on test database... ... Success! Reloading the privilege tables will ensure that all changes made so far will take effect immediately. Reload privilege tables now? [Y/n] # Press ENTER ... Success! Cleaning up... All done! If you've completed all of the above steps, your MariaDB installation should now be secure.
That’s it. Password for the database administrative user account has been set.
3, Install PHP
To install PHP, run:
$ sudo apt install php libapache2-mod-php php-mysql
After installing PHP, create info.php file in the Apache root document folder. Usually, the apache root document folder will be /var/www/html/ or /var/www/ in most Debian based Linux distributions. In Ubuntu 18.04 LTS, it is /var/www/html/.
Create info.php in the apache root folder as shown below.
$ sudo vi /var/www/html/info.php
Add the following lines:
<?php phpinfo(); ?>
Save and quit the file. Restart apache service to take effect the changes.
$ sudo systemctl restart apache2
Now, open up your web browser and type: http://IP-address/info.php in the address bar.
You will be pleased with a screen like below.
Install PHP modules
To improve the functionality of PHP, you can install some additional PHP modules.
To list the available PHP modules, run:
$ sudo apt-cache search php- | less
Use and arrows to move up and down between the result. To exit from the result, type q.
To find the details of any particular php module, for example php-gd, run:
$ sudo apt-cache show php-gd
To install a php module run:
$ sudo apt install php-gd
To install all modules (not necessary though), run:
$ sudo apt-get install php*
Do not forget to restart Apache service after installing any php module.
Congratulations! We have successfully setup LAMP stack in Ubuntu 18.04 LTS server.
- Install Apache, MySQL, PHP (LAMP) Stack On Ubuntu 18.04 LTS
- Install Nginx, MariaDB, PHP (LEMP Stack) in Ubuntu 18.04 LTS
- Install Apache, MariaDB, PHP (LAMP) stack on Arch Linux
- Install Nginx, MariaDB, PHP (LEMP) stack on Arch Linux
And, that’s all for now. As you can see, Setting up LAMP stack in Ubuntu is absolutely easy and straight-forward. Anyone will limited experience can easily setup LAMP stack easily. More good stuffs to come. Stay tuned!