
How to Install LAMP Stack on Ubuntu 20.04: A Complete Guide
Setting up a robust web server is a fundamental skill for developers, system administrators, and tech enthusiasts. One of the most popular and time-tested solutions is the LAMP stack—a powerful open-source combination of Linux, Apache, MySQL, and PHP. This combination provides a stable and high-performance platform for hosting everything from simple blogs to complex web applications.
This guide will walk you through the entire process of installing and configuring a fully functional LAMP stack on an Ubuntu 20.04 server. We’ll cover each component step-by-step, ensuring you have a secure and ready-to-use web environment.
Before we begin, you should have access to an Ubuntu 20.04 server and a non-root user account with sudo
privileges.
Step 1: Install the Apache Web Server
The first layer of our stack is Apache, the web server software responsible for serving web pages to visitors. The installation is straightforward using Ubuntu’s package manager.
First, update your package list to ensure you’re getting the latest versions:
sudo apt update
Next, install the Apache2 package:
sudo apt install apache2
Once the installation is complete, you need to adjust your firewall settings to allow web traffic. Ubuntu’s UFW firewall comes with pre-configured profiles for popular applications.
You can see the available profiles with this command:
sudo ufw app list
You’ll see a profile called ‘Apache Full’, which allows traffic on both port 80 (HTTP) and port 443 (HTTPS). To enable it, run:
sudo ufw allow 'Apache Full'
To verify that Apache is running correctly, open your web browser and navigate to your server’s IP address (http://your_server_ip
). You should see the default Ubuntu 20.04 Apache welcome page. This confirms that your web server is active and accessible.
Step 2: Install the MySQL Database Server
With your web server online, it’s time to install the database system. MySQL is a powerful relational database management system used to store and manage your site’s data.
Install the MySQL server package with:
sudo apt install mysql-server
After the installation finishes, it is highly recommended to run a security script that comes with MySQL. This script will remove insecure default settings and lock down access to your database system.
Start the interactive script by running:
sudo mysql_secure_installation
This script will guide you through several options:
- Validate Password Component: This is optional but recommended. Enabling it enforces strong password policies for your database users.
- Set Root Password: You will be prompted to create a new, secure password for the MySQL root user.
- Remove anonymous users: Yes. This is a critical security measure.
- Disallow root login remotely: Yes. The root user should only be able to connect from the local machine.
- Remove test database and access to it: Yes. The test database is for development purposes and should be removed from a production environment.
- Reload privilege tables now: Yes. This applies all the changes you just made.
Completing this script significantly improves the security of your database.
Step 3: Install PHP
PHP is the scripting language that will process dynamic content, interact with your database, and power your web applications (like WordPress, Drupal, etc.). We need to install PHP itself, a module to link it with Apache, and another module to allow it to communicate with MySQL.
Install these packages with the following command:
sudo apt install php libapache2-mod-php php-mysql
This command installs the core PHP files, the Apache module that allows Apache to handle PHP files, and the specific module for MySQL connectivity.
Once installed, you can check your PHP version with:
php -v
Step 4: Configure Apache to Prioritize PHP Files
By default, Apache will look for an index.html
file when a user requests a directory. We need to tell it to prioritize index.php
files to ensure our PHP applications are served correctly.
To do this, edit the dir.conf
file:
sudo nano /etc/apache2/mods-enabled/dir.conf
The file will look something like this:
<IfModule mod_dir.c>
DirectoryIndex index.html index.cgi index.pl index.php index.xhtml index.htm
</IfModule>
Move index.php
to the first position after DirectoryIndex
, like this:
<IfModule mod_dir.c>
DirectoryIndex index.php index.html index.cgi index.pl index.xhtml index.htm
</IfModule>
Save and close the file (in nano
, press CTRL+X
, then Y
, then ENTER
).
For the changes to take effect, you must restart Apache:
sudo systemctl restart apache2
Step 5: Test PHP Processing
Your LAMP stack should now be fully configured. The final step is to create a test PHP file to confirm that Apache can correctly process and serve PHP content.
Create a new file in your web root directory:
sudo nano /var/www/html/info.php
Add the following PHP code to the file. This code calls a function that displays detailed information about your server’s PHP configuration:
<?php
phpinfo();
?>
Save and close the file.
Now, open your web browser and navigate to http://your_server_ip/info.php
. You should see a page generated by PHP with extensive details about your server environment, PHP version, and active modules.
Important Security Tip: This page contains sensitive information about your server setup and should not be left accessible to the public. After confirming that PHP is working, it is crucial to remove this file:
sudo rm /var/www/html/info.php
Conclusion
Congratulations! You have successfully installed and configured a complete LAMP stack on your Ubuntu 20.04 server. You now have a powerful and flexible platform ready to host dynamic websites and applications. From here, your next steps could include installing a Content Management System (CMS) like WordPress, securing your site with an SSL certificate from Let’s Encrypt, or uploading your own application files.
Source: https://kifarunix.com/install-lamp-stack-on-ubuntu-20-04/