Install Apache, MySQL and PHP on Ubuntu is not difficult. The LAMP stack is a popular open source web development platform that can be used to run and use dynamic websites and web-based applications. Typically, the LAMP stack consists of Apache web servers, MariaDB / MySQL databases, the PHP / Python / Perl programming language. LAMP is an acronym from Linux, MariaDB / MYSQL, PHP / Python / Perl. This tutorial explains how to install Apache, MySQL, PHP (LAMP stack) on the Ubuntu 18.04 LTS server.
How to Install Apache, MySQL and PHP in Ubuntu 18.04 LTS Part 1
For this tutorial, just follow the following steps. Please open a Terminal in each of your Ubuntu.
1. Install Apache
For the first time, please update ubuntu with syntax:
1 |
sudo apt update |
And then for upgrading :
1 |
sudo apt upgrade |
And then Install Apache Server :
1 |
sudo apt install apache2 |
Then check whether the apache2 service is running correctly:
1 |
sudo systemctl status apache2 |
Example output from syntax above is:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
apache2.service - The Apache HTTP Server Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: en Drop-In: /lib/systemd/system/apache2.service.d └─apache2-systemd.conf Active: active (running) since Tue 2019-02-05 10:48:03 UTC; 1min 5s ago Main PID: 2025 (apache2) Tasks: 55 (limit: 2320) CGroup: /system.slice/apache2.service ├─2025 /usr/sbin/apache2 -k start ├─2027 /usr/sbin/apache2 -k start └─2028 /usr/sbin/apache2 -k start Feb 05 10:48:02 ubuntuserver systemd[1]: Starting The Apache HTTP Server... Feb 05 10:48:03 ubuntuserver apachectl[2003]: AH00558: apache2: Could not reliably Feb 05 10:48:03 ubuntuserver systemd[1]: Started The Apache HTTP Server. |
Congratulations, your Apache is active. Then provide access to the firewall for Apache: For default settings of Ubuntu 18.04, Apache is not allowed by the firewall to be remote / accessed from outside. Let’s look at the list of applications that are allowed by the firewall.
1 |
sudo ufw app list |
Then it will appear as follows:
1 2 3 4 5 |
Available applications: Apache Apache Full Apache Secure OpenSSH |
Then run the following syntax:
1 |
sudo ufw allow in "Apache Full" |
Now try running the Apache webserver in the browser by accessing http://ip_address or http://localhost. If it works well, that means we have installed apache and the configuration has been completed.
2. Install MySQL
Please run the following syntax on your ubuntu terminal:
1 |
sudo apt install mysql-server |
Then verify whether the mysql service is running well:
1 |
sudo systemctl status mysql |
If it appears like this, it means your MySQL service is running well.
1 2 3 4 5 6 7 8 9 |
mysql.service - MySQL Community Server Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enab Active: active (running) since Tue 2019-02-05 11:07:50 UTC; 17s ago Main PID: 3423 (mysqld) Tasks: 27 (limit: 2320) CGroup: /system.slice/mysql.service └─3423 /usr/sbin/mysqld --daemonize --pid-file=/run/mysqld/mysqld.pid Feb 05 11:07:49 ubuntuserver systemd[1]: Starting MySQL Community Server... Feb 05 11:07:50 ubuntuserver systemd[1]: Started MySQL Community Server. |
Congratulations your MySQL service has been going well. Then let’s set the User Administrator (ROOT) account for your Ubuntu. By default, mysql on ubuntu has an empty password. But we must add a password for our root passwort. We are required to secure our mysql server by running the following syntax:
1 |
sudo mysql_secure_installation |
You will be asked if you want to set the VALIDATE PASSWORD plugin or not. This plugin allows users to configure strong passwords for database credentials. If enabled, It will automatically check the strength of the password and force the user to only set a password that is safe enough. It’s safer if we disable this plugin. However, you must use a strong and unique password for the database credentials. If you don’t want to activate this plugin, just press any key to skip the password validation section and continue the next steps.
If your answer is Yes, you will be asked to choose a level of password validation.
1 2 3 4 5 6 7 |
Securing the MySQL server deployment. Connecting to MySQL using a blank password. VALIDATE PASSWORD PLUGIN can be used to test passwords and improve security. It checks the strength of password and allows the users to set only those passwords which are secure enough. Would you like to setup VALIDATE PASSWORD plugin? Press y|Y for Yes, any other key for No y |
Validation of available passwords is low, medium and strong. Just enter the appropriate number (0 for low, 1 for medium and 2 for strong password) and press the ENTER button.
1 2 3 4 5 |
There are three levels of password validation policy: LOW Length >= 8 MEDIUM Length >= 8, numeric, mixed case, and special characters STRONG Length >= 8, numeric, mixed case, special characters and dictionary file Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: |
Now, enter the password for the MySQL root user. Please remember that you have to use a password for mysql root users depending on the password policy you chose in the previous step. If you do not activate this plugin, just use a strong and unique password of your choice.
1 2 3 4 5 |
Please set the password for root here. New password: Re-enter new password: Estimated strength of the password: 50 Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y |
After you enter the password twice, you will see the strength of the password (In our case it is 50). If you agree, press Y to continue with the password provided. If not satisfied with the password length, press the other button and set a strong password. If you agree with my current password, so I choose y.
For the rest of the questions, just type y and press ENTER. This will delete anonymous users, prohibit remote root user logins and delete the test database.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
Remove anonymous users? (Press y|Y for Yes, any other key for No) : y 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? (Press y|Y for Yes, any other key for No) : y Success. By default, MySQL 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? (Press y|Y for Yes, any other key for No) : y - Dropping test database... Success. - 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? (Press y|Y for Yes, any other key for No) : y Success. All done! |
Congratulations, the password for your MySQL ROOT user has been set. Now continue to change the Authentication Method for the Root user. By default, the MySQL root user is set to authenticate using the auth_socket plugin in MySQL 5.7 and newer versions on Ubuntu. Although it increases security, it will also be difficult when you access your database server using any external program, for example phpMyAdmin. To fix this problem, you need to change the authentication method from auth_socket to mysql_native_password. To do this, enter your MySQL prompt using the command:
1 |
sudo mysql |
Run the following command at the mysql prompt to find the current authentication method for all mysql user accounts:
1 |
SELECT user,authentication_string,plugin,host FROM mysql.user; |
Then the output is as follows:
1 2 3 4 5 6 7 8 9 |
+------------------+-------------------------------------------+-----------------------+-----------+ | user | authentication_string | plugin | host | +------------------+-------------------------------------------+-----------------------+-----------+ | root | | auth_socket | localhost | | mysql.session | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | mysql_native_password | localhost | | mysql.sys | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | mysql_native_password | localhost | | debian-sys-maint | *F126737722832701DD3979741508F05FA71E5BA0 | mysql_native_password | localhost | +------------------+-------------------------------------------+-----------------------+-----------+ 4 rows in set (0.00 sec) |
Look at this picture :
As you can see, the mysql root user uses the auth_socket plugin for authentication.
To change this authentication to the mysql_native_password method, run the following command at the mysql prompt. Don’t forget to replace “password” with a password of your choice that is strong and unique. If you have activated the VALIDATION plugin, make sure you have used a strong password based on current policy requirements.
1 |
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password'; |
Now please update what we have just run:
1 |
FLUSH PRIVILEGES; |
Now check again whether the authentication method was changed or not using the command :
1 |
SELECT user,authentication_string,plugin,host FROM mysql.user; |
Look at this image :
Great!!! now the root user has access to all mysql shells. Please close mysql by adding the following syntax:
1 |
exit |
Congratulations, your MySQL server is running well.
3. Install PHP
Because this article is too long, I divided this article into 2 parts. Please continue reading at:
Comments 2