The acronym LAMP stands for Linux, Apache, MySQL, and PHP, all of which are open-source software that, when stacked together, create a powerful web server. You should consider the LAMP stack if you’re planning to deploy a website or want to develop one and need a test environment.
You’re in luck! Because in this post, we’ll learn how to install a LAMP stack Ubuntu server step-by-step.
To follow this tutorial, you must have an Ubuntu server and a user account with sudo privileges. This post will be using an Ubuntu 22.04 server.
Step 1: Setup the Firewall
Assuming that your server has the UFW firewall enabled and active, this step will walk you through allowing the web server ports 80 and 443.
- Connect to your server via SSH or login to the desktop environment (if available).
- Next, update the local package cache to ensure you’re installing the latest packages later:
sudo apt update
- Run the below commands to add ports 80 and 443 to the allow list:
sudo ufw allow 80 sudo ufw allow 443
- Lastly, confirm the firewall rules are present and active:
sudo ufw status numbered
Step 2: Install Apache2
So the ‘L’ in LAMP stands for Linux. And since you’re running Ubuntu, you’ve good the that part. Next in line is ‘A’, Apache, specifically Apache2. Follow these instructions to install it.
- Run this command to install Apache2 via APT.
sudo apt install -y apache2
As you can see, the command installs additional packages that Apache needs.
- Next, confirm the apache2 service is enabled and active:
sudo systemctl status apache2
- Now, let’s make sure that the Apache web server is accessible. Launch a browser and open the URL http://SERVERIPADDRESS/.
If all is well, you should see the default home page, as shown below.
Step 3: Install MySQL
Now that the web server is up and running, we’ll need a database management system where you can store, organize, and your website’s contents. This is where the ‘M’ in LAMP comes in: MySQL.
- Run the below command to install the MySQL server:
sudo apt install mysql-server
As you can see, this command will also install the required additional packages.
- After the MySQL server installation, let’s ensure that the MySQL root account has a password:
# Connect to MySQL sudo mysql # Set the root user password to Dk03^M$i7xB9. ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'Dk03^M$i7xB9'; # Exit MySQL exit
- Next, run the mysql_secure_installation script. This command will prompt you for the password specified in the previous step:
- The script then prompts you to set up the VALIDATE PASSWORD COMPONENT. This component will make passwords adhere to stricter validation guidelines, essentially banning weak and blank passwords.
- For the rest of the questions, answer Y.
- And you’ve completed the MySQL installation.
Step 4: Install PHP
So you now have a web server (Apache2) and a database system for content management (MySQL). You need the ‘P’ in LAMP that handles code and displays dynamic content: PHP.
- Run the below command to install the php, libapache2-mod-php, php-mysql. The libapache2-mod-php package allows PHP integration with Apache, and php-mysql facilitates the connection between PHP and MySQL.
sudo apt install php libapache2-mod-php php-mysql
- After the installation, confirm that PHP is present on the machine:
- Restart the Apache service so that it picks up the current changes:
sudo systemctl restart apache2
- Next, let’s test whether PHP works. We’ll create a PHP page that displays the local system information. Run this command to open a new file called /var/www/html/systeminfo.php in the text editor.
sudo nano /var/www/html/systeminfo.php
- Copy the code below, paste it into the editor, and save the file:
<pre> <strong>Hostname:</strong> <?php system("hostname"); ?> <br /> <strong>IP Address:</strong> <?php system("hostname -I"); ?> <br /> <strong>System Information:</strong> <?php system("hostnamectl | grep 'Operating System\|Architecture\|Kernel'"); ?> <br /> <strong>Uptime:</strong> <?php system("uptime"); ?> <br /> <strong>Memory Usage (MB):</strong> <?php system("free -m"); ?> <br /> <strong>Disk Usage:</strong> <?php system("df -h"); ?> </pre>
The file should look like the one below.
- After saving the file, open the server URL http://SERVERIPADDRESS/systeminfo.php in your browser and see the following information on the webpage.
- We’ve now confirmed that PHP is installed and working as expected.
In this post, you learned how to install and configure each component of the LAMP stack, such as Apache, MySQL, and PHP.
That’s it! You’ve successfully installed and tested the LAMP stack Ubuntu server and are on your way to starting your web development project.