Installing Apache, MySQL, PHP (LAMP) Stack on Ubuntu 18.04
Prerequisites
You should use a non-root user account with sudo privileges.
1. Install Apache
Let’s begin by updating the package lists and installing Apache on Ubuntu 18.04. Below we have two commands separated by &&
. The first command will update the package lists to ensure you get the latest version and dependencies for Apache . The second command will then download and install Apache. Press y
and ENTER
when asked to continue.
Installation may take a few minutes. Once installed, continue to Step 2 to configure the firewall.
2. Configure Firewall
It is highly recommended that you configure a firewall for added security.
We’ll start by adding a firewall rule for SSH because if you are configuring your server remotely, you don’t want to get locked out when enabling the firewall! If the rule already exists, the command will just skip it.
If you get an error “ERROR: could find a profile matching openSSH”, this probably means you are not configuring the server remotely and can ignore it.
Now we can add the firewall rules for Apache.
Now enable the firewall if it isn’t already.
Press y
if you see a message “Command may disrupt existing ssh connections”.
If the firewall was activated correctly, you should see “Firewall is active and enabled on system startup“.
You can also check the current firewall status with:
Above we can see the firewall is active and has two rules per service. v6
is short for IPv6. This is the new Internet Protocol, which was introduced to deal with the long-anticipated problem of IPv4 address exhaustion.
3. Test Apache
To see if Apache installed correctly, we can check the current Apache service status.
If it is up and running, you should see a green active state.
If you get the above error about a fully qualified domain name, you can ignore it.
You may need to press q
to exit the server status.
Now that the Apache service is up and running, you should be able to view the test Apache web page through your web browser. Enter the IP address of your server in the address bar and hit ENTER
.
If you don’t know your IP, you can find out with the following command.
You’re all set! You can find this Apache default welcome page in the folder /var/www/html
. To edit this file:
Press CTRL
+ X
to exit the nano text editor.
Your Apache web server is ready to go! You can now add your own html files and images the the /var/www/html
directory as you please. If you want to host multiple sites and domains on Apache, please see Installing Apache on Ubuntu 18.04 with Multiple Domains.
4. Install MySQL
Let’s begin by updating the repository and installing the MySQL package using apt
. APT (Advanced Package Tool) is the command line tool used in Ubuntu 18.04 to interact with the packaging system.
You can see below we have two commands combined into one using &&
.
Press y
and ENTER
when prompted to install the MySQL package.
Once the package installer has finished, we can check to see if the MySQL service is running.
If running, you will see a green Active status like below.
You may need to press q
to exit the service status.
5. Configure MySQL Security
You should now run mysql_secure_installation
to configure security for your MySQL server.
If you created a root password in Step 1, you may be prompted to enter it here. Otherwise you will be asked to create one.
You will be asked if you want to set up the Validate Password Plugin. It’s not really necessary unless you want to enforce strict password policies for some reason.
Press ENTER
here if you don’t want to set up the validate password plugin.
If you didn’t create a root password in Step 1, you must now create one here.
Generate a strong password and enter it. Note that when you enter passwords in Linux, nothing will show as you are typing (no stars or dots).
Press y
and ENTER
to remove anonymous users.
Press y
and ENTER
to disallow root login remotely. This will prevent bots and hackers from trying to guess the root password.
Press y
and ENTER
to remove the test database.
Press y
and ENTER
to reload the privilege tables.
All done!
As a test, you can log into the MySQL server and run the version
command.
Enter the MySQL root password you created earlier and you should see the following:
You have now successfully installed and configured MySQL!
6. Install PHP
Let’s begin by updating the repository and installing the PHP package using apt
. We will also install two more packages libapache2-mod-php
and php-mysql
, which will allow PHP to communicate with the MySQL database.
Press y
and ENTER
when prompted to install the PHP package.
7. Test PHP
Once the package has finished installing, we can test PHP in the command line.
If PHP installed correctly, you should see something similar below:
Great! Now, let’s test PHP for Apache.
Create a new file called info.php
in your document root directory.
The default document root in Ubuntu 18.04 is /var/www/html/
, or if you followed our previous Apache guide on setting up Virtual Hosts, your document root may be in somewhere like /var/www/mytest1.com/public_html
where mytest1.com is the name of your own domain.
Once you’ve confirmed the location of you document root directory, create a new file called info.php
using the nano
text editor.
In this example, we will create a new file in /var/www/html/
Once nano
editor has opened, paste in the following PHP code. (If using PuTTY, click to right mouse button to paste)
/var/www/html/info.php
Save file and exit. (Press CTRL
+ X
, press Y
and then press ENTER
)
We can now load this file in the browser by going to http://example.com/info.php
or http://your_ip/info.php
Tip: If you don’t know you IP, you can find out with:
Below we can see the PHP info page is working correctly.
Once you’ve confirmed PHP is working correctly, it’s important to delete info.php
as it contains information that could be useful to hackers.
8. Configure PHP (Optional)
If you plan on uploading files larger than 2MBs through WordPress or similar, you will need to alter the PHP config file and set the max upload size.
Congratulations! You’ve successfully installed and configured a LAMP Stack for Ubuntu 18.04.
Last updated