This article will explain how to install Apache, MySQL, and PHP on Ubuntu 18.04 (Bionic Beaver). This setup is commonly called a LAMP stack, an acronym for Linux, Apache, MySQL, and PHP. The instructions will use the terminal to install the required software.
Updating the System
sudo apt update sudo apt upgrade
Before installing any new software on an Ubuntu system, it is good practice to update the apt package index and upgrade any packages that need to be updated. Using the
sudo command will require an administrative password. If there are any updates or upgrades that need to be installed, the terminal will ask if you’d like to install the updates. Press Y for ‘Yes’.
sudo apt install apache2 -y
The -y command answers ‘yes’ automatically when the terminal asks if you’re sure you want to install the required packages. The system will install Apache and its required packages. Afterwards, you can check the status of the newly install Apache server using the command:
sudo systemctl status apache2
If there were no errors during the installation, you should see the status of the Apache server as ‘active’.
To exit the status screen, press the ‘q’ button. Now that Apache has been installed, you can visit http://localhost and you will see the default Apache index page.
The Apache server is installed and running.
MySQL Server Installation
The next step is to install the MySQL server. MySQL is a database to which web applications can connect. At the Ubuntu terminal, enter the following commad:
sudo apt install mysql-server -y
After MySQL is installed, the status can be checked with the command:
sudo systemctl status mysql
The status should read active, and you can exit the status screen by pressing the ‘q’ key. Next, you should run the secure installation script for the MySQL server.
MySQL Secure Installation
You will be given a prompt during the secure installation.
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?
If you answer yes, you will be provided with the plugin options.
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:
This plugin forces a requirement on passwords for MySQL user profiles. There are LOW, MEDIUM, and STRONG settings to choose from requiring future passwords of a certain strength. Choose a setting and enter 0, 1, or 2.
Next, you will be asked to set the root password, or if you want to change the root password if the password is already set. You’ll also be presented with another option to choose.
By default, a MySQL installation has an anonymous user, allowing anyone to log into MySQL 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? (Press y|Y for Yes, any other key for No) :
After making a selection, the user will be asked about disallowing root logins remotely.
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) :
You’ll also be asked if you want to delete the “test” database that comes preloaded on the MySQL server.
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) :
Lastly, you’ll be asked about reloading the privilege tables to make any changes you’ve made effective immediately.
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) :
The secure installation of MySQL should now be complete.
sudo apt install php libapache2-mod-php php-mysql -y
Installing PHP on its own will only install PHP on the system; however, the Apache and MySQL servers will not have PHP support. That is why
php-mysql are included in the installation. After the installation, the Apache server will need to be restarted because of the libapache2-mod-php package; the package allows the Apache server to support the PHP language. The other package, php-mysql offers support to the php language itself.
sudo systemctl restart apache2
If you followed the instructions above on a Ubuntu 18.04 system, you should now have functioning Apache and MySQL servers. PHP should also be working on the system.