1. Installing Apache
Install Apache using Ubuntu’s package manager,
$ 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.
To see if your Apache works, go to your internetbrowser and go to: http://localhost/
You will see the following screen:
2. Installing MySQL
Now that you have your web server up and running, it is time to install MySQL. MySQL is a database management system. Basically, it will organize and provide access to databases where your site can store information.
apt to acquire and install this software:
$ sudo apt install mysql-server
When the installation is complete, run a simple security script that comes pre-installed with MySQL which will remove same dangerous defaults and lock down access to your database system. Start the interactive script by running:
$ sudo mysql_secure_installation
After that you get a few questions. Answer them like i did below:
1st question: No (N)
2nd question: Yes (Y)
3rd question: Yes (Y)
4th question: Yes (Y)
5th question: Yes (Y)
3. Installing PHP
Once again, leverage the
apt system to install PHP. In addition, include some helper packages this time so that PHP code can run under the Apache server and talk to your MySQL database:
$ sudo apt install php libapache2-mod-php php-mysql
In most cases, you will want to modify the way that Apache serves files when a directory is requested. 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.
To do this, type this command to open the
dir.conf file in a text editor with root privileges:
$ sudo nano /etc/apache2/mods-enabled/dir.conf
Move the PHP index file (highlighted above) to the first position after the
DirectoryIndex specification, like this:
When you’re done, press CTRL + X and Y to save the file. Press Enter to leave this directory.
After this, restart the Apache web server in order for your changes to be recognized. Do this by typing this:
$ sudo systemctl restart apache2
If you want to check the status of apache2:
$ sudo systemctl status apache2
4. Setting Up Virtual Hosts (Recommended)
Create the directory for your_domain as follows:
sudo mkdir /var/www/your_domain
Next, assign ownership of the directory with $USER environment variable:
$ sudo chown -R $USER:$USER /var/www/your_domain
The permissions of your web roots should be correct if you haven’t modified your
unmask value, but you can make sure by typing:
$ sudo chmod -R 755 /var/www/your_domain
Next, create a sample index.html page using nano or your favorite editor:
Inside, add the following sample HTML:
Save and close the file when you are finished.
sudo nano /etc/ap[ache2/sites-available/your_domain.conf
Paste in the following configuration block, which is similar to the default, but updated for our new directory and domain name:
Let’s enable the file:
$ sudo a2ensite your_domain.conf
$ sudo a2dissite 000-default.conf
$ sudo apache2ctl configtest
Restart Apache to implement your changes:
$ sudo systemctl restart apache2
Step 5 – Testing PHP Processing on your Web Server
Create the file at the web root you created in the previous step by running:
$ sudo nano /var/www/your_domain/info.php
This will open a blank file. Add the following text, which is valid PHP code, inside the file:
When you are done, save and close the file.
Now you can test whether your web server is able to correctly display content generated by this PHP script.
The address you will want to visit is:
The page that you come to shoud look something like this:
You succesfully installed LAMP on Ubuntu.
If you got any suggestions, questions or feedback, please feel free to contact us: firstname.lastname@example.org