How To Install Linux, Apache, MySQL, PHP (LAMP) + PHPMyAdmin stack on Ubuntu 18.04

30 March 2020
How To Install Linux, Apache, MySQL, PHP (LAMP) + PHPMyAdmin stack on Ubuntu 18.04

Introduction

If you are on this page then probably you already know what is LAMP server, and why you may use it, so we are skipping this step and starting from installation process.

Let's begin.

 

Step 1 — Installing Apache

 

sudo apt update
sudo apt install apache2 

 

Since this is a sudo command, these operations are executed with root privileges. It will ask you for your regular user’s password to verify your intentions.

 

Once you’ve entered your password, apt will tell you which packages it plans to install and how much extra disk space they’ll take up. Press Y and hit ENTER to continue, and the installation will proceed.

Also some common mods need to be enabled

sudo a2enmod rewrite
sudo a2enmod headers

 

Step 2 — Installing MySQL

 

sudo apt install mysql-server 

When the installation is complete, run a simple security script that comes pre-installed with MySQL which will remove some dangerous defaults and lock down access to your database system. Start the interactive script by running:


 sudo mysql_secure_installation 

 

This will ask if you want to configure the VALIDATE PASSWORD PLUGIN.

Then you will be asked few more questions in process.

If you prefer to use a password when connecting to MySQL as root, you will need to switch its authentication method from auth_socket to mysql_native_password. To do this, open up the MySQL prompt from your terminal:

sudo mysql

Next, check which authentication method each of your MySQL user accounts use with the following command:

 

SELECT user,authentication_string,plugin,host FROM mysql.user;

To configure the root account to authenticate with a password, run the following ALTER USER command. Be sure to change password to a strong password of your choosing:

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';

Then, run FLUSH PRIVILEGES which tells the server to reload the grant tables and put your new changes into effect:

FLUSH PRIVILEGES;
exit

At this point, your database system is now set up and you can move on to installing PHP, the final component of the LAMP stack.

 

Step 3 — Installing PHP

 

sudo apt install php libapache2-mod-php php-mysql

 

This should install PHP without any problems.

Currently, if a user requests a directory from the server, Apache will first look for a file called index.html. We want to tell the web server to prefer PHP files over others, so make Apache look for an index.php file first.

Edit dir.conf file

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

remove everything and update with this code

<IfModule mod_dir.c>
    DirectoryIndex index.php index.html index.cgi index.pl index.xhtml index.htm
</IfModule>

So we just changed the index order.

When you are finished, save and close the file by pressing CTRL+X. Confirm the save by typing Y and then hit ENTER to verify the file save location.

Also you can install some common used php packages

sudo apt install php-curl
sudo apt install php-mbstring
or add yours sudo apt install php-[..package name]

After this, restart the Apache web server in order for your changes to be recognized. Do this by typing this:

sudo systemctl restart apache2

 

 

Create a Domain file using command below:

sudo nano /etc/apache2/sites-available/your_domain.conf

Paste code below into your .conf file

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    ServerName your_domain
    ServerAlias www.your_domain
    DocumentRoot /var/www/your_domain
<Directory /var/www/your_domain>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
</Directory>
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

</VirtualHost>

Also don't forget to create a domain directory in /var/www/

next step will be enabling your new added configs

sudo a2ensite your_domain.conf

and disable your default config file

sudo a2dissite 000-default.conf

the last step will be restarting apache2 service

sudo systemctl restart apache2

 

Step 5 — Installing PHPMyAdmin

 

We will install phpMyAdmin from the default Ubuntu repositories.

This is done by updating your server’s package index and then using the apt packaging system to pull down the files and install them on your system:

    sudo apt update
    sudo apt install phpmyadmin php-mbstring php-gettext

This will ask you a few questions in order to configure your installation correctly.

Warning: When the prompt appears, “apache2” is highlighted, but not selected. If you do not hit SPACE to select Apache, the installer will not move the necessary files during installation. Hit SPACE, TAB, and then ENTER to select Apache.
  • For the server selection, choose apache2
  • Select Yes when asked whether to use dbconfig-common to set up the database
  • You will then be asked to choose and confirm a MySQL application password for phpMyAdmin

The installation process adds the phpMyAdmin Apache configuration file into the /etc/apache2/conf-enabled/ directory, where it is read automatically. The only thing you need to do is explicitly enable the mbstring PHP extension, which you can do by typing:

sudo phpenmod mbstring

Afterwards, restart Apache for your changes to be recognized:

sudo systemctl restart apache2

phpMyAdmin is now installed and configured. However, before you can log in and begin interacting with your MySQL databases, you will need to ensure that your MySQL users have the privileges required for interacting with the program.

So, in this step we are going to setup a new user for phpmyadmin with it's own password. Now you will have to open mysql command line

sudo mysql -u root -p

As you remember on step 2 we have set a password for the root user.

The command above is saying that we are connecting to mysql with user root (-u root) and with password (-p)
So you will have to enter the password you have set on step 2

CREATE USER 'develop'@'localhost' IDENTIFIED BY 'password';

in this step we have created a new user develop, with password : password, please replace it with yours. Next command will give all the PRIVILEGES to the user develop

GRANT ALL PRIVILEGES ON *.* TO 'develop'@'localhost' WITH GRANT OPTION;
exit

That's it now you can access phpmyadmin by the url

http://your_domain_or_IP/phpmyadmin

 

Step 6 — Installing Secure Apache with Let's Encrypt

 

Installing Certbot

sudo add-apt-repository ppa:certbot/certbot

You’ll need to press ENTER to accept.

Install Certbot’s Apache package with apt:

sudo apt install python-certbot-apache
sudo certbot --apache -d your_domain -d www.your_domain

This runs certbot with the --apache plugin, using -d to specify the names you’d like the certificate to be valid for.

If this is your first time running certbot, you will be prompted to enter an email address and agree to the terms of service. After doing so, certbot will communicate with the Let’s Encrypt server, then run a challenge to verify that you control the domain you’re requesting a certificate for.

If that’s successful, certbot will ask how you’d like to configure your HTTPS settings:

Output
Please choose whether or not to redirect HTTP traffic to HTTPS, removing HTTP access.
-------------------------------------------------------------------------------
1: No redirect - Make no further changes to the webserver configuration.
2: Redirect - Make all requests redirect to secure HTTPS access. Choose this for
new sites, or if you're confident your site works on HTTPS. You can undo this
change by editing your web server's configuration.
-------------------------------------------------------------------------------
Select the appropriate number [1-2] then [enter] (press 'c' to cancel):

Select your choice then hit ENTER. The configuration will be updated, and Apache will reload to pick up the new settings. certbot will wrap up with a message telling you the process was successful and where your certificates are stored:

Your certificates are downloaded, installed, and loaded. Try reloading your website using https:// and notice your browser’s security indicator. It should indicate that the site is properly secured, usually with a green lock icon.

Verifying Certbot Auto-Renewal

 

sudo certbot renew --dry-run

If you see no errors, you’re all set. When necessary, Certbot will renew your certificates and reload Apache to pick up the changes. If the automated renewal process ever fails, Let’s Encrypt will send a message to the email you specified, warning you when your certificate is about to expire.