How to Install LAMP in Ubuntu 18.04 (With PHPMyAdmin)

A LAMP (Linux, Apache, MySQL & PHP) stack is a bundle of the open-source, free and commonly used web stack used for host dynamic website and web apps. In this guide, we’ll show you how to install LAMP in Ubuntu 18.04 with PHPMyAdmin.

Every popular web hosting company or managed web hosting providers come with pre-installed LAMP or LEMP stack. But these hosting provider has a lot of limitation. On shared hosting or managed hosting (traditional hosting) providers doesn’t give you full control over the server. If you want full control on the server then follow this guide.

Note: If you are a beginner then we recommend you start with shared hosting like Bluehost & Siteground. They provide everything in 1-click without any coding knowledge. This guide for those users who want freedom on the server and have some coding knowledge.

Before starting, read our step by step guide on how to set up your first VPS

Requirements:

  • Ubuntu 18.04 server Installation
  • sudo user privileges
  • Server access
  • Coding Knowledge
  • 15m – 20m time
  • Active internet connection
  • Terminal (Mac/Linux) or Putty (Windows)

Windows users can download Putty from Putty official website (It’s free)

How to Install LAMP in Ubuntu 18.04 with PHPMyAdmin

In this article, we will explain how to install LAMP stack with PhpMyAdmin in Ubuntu 18.04 (same for all VPS providers like AWS, Digitalocean, Linode or GCP). To install LAMP on a cloud server or VPS, follow the below steps:

Step 1: Install Apache Web Server

Apache web server is the most popular web server in the world and has over 60% market share. To install apache server, first of all, Login to server, update your software packages and install the Apache webserver using following commands.

sudo apt update && sudo apt install apache2

It’ll ask for confirmation. Just type y and press ENTER. After the complete the installation, the apache service should start automatically. You can check the status by the following command.

sudo service apache2 status

Success message: active (running)

Press Q for quit.

You can also check the status by opening server IP address or domain name in your browser. If you see the apache default web page, it means your installation is working fine.

You can find your server IP by the following command

ip addr show eth0 | grep inet | awk '{ print $2; }' | sed 's/\/.*$//'

Configure Firewall

Configure your server firewall by following 3 commands.

sudo ufw allow OpenSSH
sudo ufw allow in "Apache Full"
sudo ufw enable

After applying the last command, it’ll ask your conformation. Just press y and press ENTER. You can check firewall status by following the below command.

sudo ufw status

Output:

To Action From
— —— —-
OpenSSH ALLOW Anywhere
Apache Full ALLOW Anywhere
OpenSSH (v6) ALLOW Anywhere (v6)
Apache Full (v6) ALLOW Anywhere (v6)

Step 2: Install MySQL Database Server

Now time to install the MySQL database server. MySQL is an open-source database management system where all data of your website will be stored.

Update your server and install MySQL server by following the below command.

sudo apt update && sudo apt install mysql-server

It’ll ask your conformation. Just press y and press ENTER. The MySQL services should start automatically after complete installation, you can check its status to ensure that it is up and running by the following command.

sudo service mysql status

Success message: active (running)

Press Q for quit.

The MySQL server installation is not secure by default. You need to secure it manually by the following command.

sudo mysql_secure_installation

Once you execute the command, it will ask you a question for VALIDATE PASSWORD plugin. Press y and ENTER. and then 2 (for strong password)

Next, enter your password 2 time and press ENTER. Again it’ll ask 4 questions.

  • Question No.1: Do you wish to continue with the password provided? Ans: y
  • Question No.2: Remove anonymous users? Ans: y
  • Question No.3: Disallow root login remotely? Ans: y
  • Question No.4: Remove test database and access to it? Ans: y

Just press y for every time.

You have successfully installed and secure MySQL database server. Now time to install PHP. Follow below mentioned steps to install PHP.

Step 3: Install PHP

PHP is one of the most widely used server-side scripting language used to generate dynamic content on websites. It can run scripts, connect to your MySQL databases and hand the processed content over to your webserver to display.

Use the following command to install PHP (default version PHP 7.2) and other required modules.

sudo apt update && sudo apt install php libapache2-mod-php php-mysql php-bcmath php-imagick

press y and ENTER to install new packages.

You can also install the most widely used image plugin Imagemagick by executing the below command (Optional). If you prepare this server for WordPress then must install this PHP plugin.

sudo apt install imagemagick

press y and ENTER to install new packages.

You have successfully installed PHP 7.2 and can check the version by executing the below command

php -version

Once PHP installed, you can test your PHP setup by creating a simple info.php page in your web server document root by executing the below command.

echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/info.php

Now open your browser and enter the below URL to view the PHP information.

http://domain-name/info.php
OR
http://SERVER-IP/info.php

Step 4: Install PhpMyAdmin

Now time to install PHPMyAdmin. With PHPMyAdmin, you can easily manage your MySQL database server. Execute the below command to install PHPMyAdmin.

sudo apt update && sudo apt install phpmyadmin

press y and ENTER to install new packages.

It will be asked to choose the webserver. Just press ENTER

Next, Enter and confirm the password.

Now active PHPMyAdmin by executing below 2 commands.

sudo ln -s /etc/phpmyadmin/apache.conf /etc/apache2/conf-available/phpmyadmin.conf
sudo a2enconf phpmyadmin.conf

Restart Apache services After successfully installed and active PHPMyAdmin by executing the below command.

sudo service apache2 restart

Now you can access your PHPMyAdmin by opening http://your_domain/phpmyadmin URL in a browser.

Change PHPMyAdmin default URL

PHPMyAdmin default URL is http://your_domain/phpmyadmin but you can change it. For security, we recommend changing your default PHPMyAdmin URL to custom URL.

to do this, edit phpmyadmin.conf file and change Alias /phpmyadmin to a custom one. (Refer screenshot)

sudo nano /etc/apache2/conf-available/phpmyadmin.conf

(Use Keyboard)

Press Ctrl + x then y and ENTER to save the file.

Now Reload Apache

sudo service apache2 reload

Create MySQL User

Create a MySQL user who can access your MySQL database through PHPMyAdmin. Execute the following below commands one by one.

Must change username & password with your own.

sudo mysql
CREATE USER 'username'@'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON * . * TO 'bmilan662'@'localhost';
FLUSH PRIVILEGES;
exit

Now you can log in to PHPMyAdmin using your username & password.

Enable Rewrite

Mod_rewrite is a powerful Apache module that provides URL manipulation capability. By default, Apache prohibits using an .htaccess file to apply rewrite rules. You can easily enable mod_rewrite by executing the below command and restart apache to apply the change.

sudo a2enmod rewrite
sudo service apache2 restart

Setting Up .htaccess in Ubuntu

A .htaccess file allows us to modify our rewrite rules without touching server configuration files.

You will need to edit the configuration of each website’s virtual host file by adding the following code. But in this guide, we are using the default Apache virtual host file.

First, open default virtual host file in edit mode

sudo nano /etc/apache2/sites-enabled/000-default.conf

and add below file under DocumentRoot /var/www/html (refer screenshot)

<Directory "/var/www/html">
  AllowOverride All
</Directory>

Press Ctrl + x then y and ENTER to save the file.

Install ZIP/Unzip (Optional)

If you want to zip or unzip in future then must install this software. Execute the below command to install zip and unzip in ubuntu.

sudo apt update && sudo apt install zip unzip

Configure apache2.conf file

Open apache2.conf file in edit mode

sudo nano /etc/apache2/apache2.conf

Find the below code

<Directory /var/www/>
    Options Indexes FollowSymLinks
    AllowOverride None
    Require all granted
 </Directory>

and change AllowOverride None to AllowOverride All also add the below code under it to prevent directory listing (Refer screenshot)

 <Directory /var/www/html>
    Options -Indexes
 </Directory>

Also, add below code at the end of the file to hide ubuntu version. (optional)

ServerSignature Off
ServerTokens Prod
Before
After

Press Ctrl + x then y and ENTER to save the file.

Finally, restart Apache services to apply changes.

sudo service apache2 restart

Boom! You have successfully installed LAMP stack in Ubuntu 18.04 with PHPMyAdmin.

Conclusion (How to Install LAMP in Ubuntu)

In this post, we learned how to install LAMP in Ubuntu 18.04 with PHPMyAdmin. Now LAMP stack is up and running in your server. You have many choices for what to do next. You can install WordPress (most popular CMS).

If you find this article helpful then share this article or if have any query regarding this article then comment below, we will happy to solve them.

Recommended Posts

No comment yet, add your voice below!


Add a Comment

Your email address will not be published. Required fields are marked *