A LAMP stack consists of Linux, Apache, MySQL/MariaDB, and PHP. This combination is widely used for hosting dynamic websites and web applications. In this guide, we’ll walk you through the steps to install and configure a LAMP web server on Ubuntu 18.04.
Prerequisites
- A server running Ubuntu 18.04.
- Root or sudo access to the server.
- Basic knowledge of the command line.
Step 1: Update the System
Before starting the installation, it’s a good practice to update your package index and upgrade the installed packages:
sudo apt update
sudo apt upgrade -y
Step 2: Install Apache
Apache is the web server component of the LAMP stack. To install it, run the following command:
sudo apt install apache2 -y
After the installation is complete, start the Apache service and enable it to run at boot:
sudo systemctl start apache2
sudo systemctl enable apache2
You can verify that Apache is running by entering your server’s IP address in a web browser. You should see the default Apache2 Ubuntu default page.
Step 3: Install MySQL/MariaDB
Next, you will install a database management system. We’ll use MariaDB, a popular fork of MySQL.
Install MariaDB with the following command:
sudo apt install mariadb-server -y
After the installation is complete, start the MariaDB service and enable it to run at boot:
sudo systemctl start mariadb
sudo systemctl enable mariadb
To secure your MariaDB installation, run:
sudo mysql_secure_installation
You will be prompted to answer a series of questions to secure your installation, such as setting a root password and removing anonymous users.
Step 4: Install PHP
Now, install PHP along with some commonly used PHP extensions:
sudo apt install php libapache2-mod-php php-mysql -y
Step 5: Configure Apache to Use PHP
By default, Apache serves HTML files. To ensure it processes PHP files, we will configure the dir.conf
file. Open it with:
sudo nano /etc/apache2/mods-enabled/dir.conf
You will see a section that looks like this:
<IfModule mod_dir.c>
DirectoryIndex index.html index.cgi index.pl index.php index.xhtml index.htm
</IfModule>
Make sure index.php
is listed first, so it looks like this:
<IfModule mod_dir.c>
DirectoryIndex index.php index.html index.cgi index.pl index.xhtml index.htm
</IfModule>
Save and exit the file (press CTRL + X
, then Y
, and Enter
).
Step 6: Restart Apache
To apply the changes, restart the Apache service:
sudo systemctl restart apache2
Step 7: Test PHP Processing
To confirm that PHP is working with Apache, create a test PHP file:
sudo nano /var/www/html/info.php
Add the following line to the file:
<?php
phpinfo();
?>
Save and exit the editor. Now, visit http://your_server_ip/info.php
in your web browser. You should see a page displaying your PHP configuration.
Step 8: Secure Your Server (Optional)
- Remove the PHP Info File: For security reasons, delete the info file after confirming that PHP is working:
sudo rm /var/www/html/info.php
- Install and Configure a Firewall: If you haven’t already, it’s a good idea to set up a firewall to control incoming and outgoing traffic. If UFW (Uncomplicated Firewall) is not installed, you can install it with:
sudo apt install ufw -y
Allow Apache through the firewall:
sudo ufw allow 'Apache Full'
sudo ufw enable
Conclusion
You have successfully installed and configured a LAMP stack on your Ubuntu 18.04 server. Your server is now ready to host dynamic websites and applications using Apache, MariaDB, and PHP. Be sure to regularly update your server and monitor its performance for optimal security and efficiency. For further customization and advanced configurations, consider exploring additional Apache and PHP settings.