Set up LAMP (Linux, Apache, MySQL, PHP) in Ubuntu Linux
In this guide, we will explain how to install the Linux, Apache, MySQL, and PHP (LAMP) stack on an Ubuntu 22.04 server. You will set up a basic web application to test the stack's functionalities.
LAMP: A Comprehensive Open Source Web Development Platform
LAMP, an acronym for Linux, Apache, MySQL, and PHP, serves as a robust open-source framework for web development. Utilizing Linux as the operating system, Apache as the web server, MySQL as the relational database management system, and PHP. LAMP is the go-to choice for creating dynamic websites and web applications. Its popularity stems from its flexibility, making it a staple in the web development community.
Prerequisites
It works on all Linux distributions.
Operating system used: Ubuntu 22.04
Update && Upgrade the server
Installing Apache and Updating the Firewall
To update and upgrade the package lists from the repositories, run the following command:
Install Apache
Once the installation is complete, please modify your firewall settings as necessary.
Here is what each of these profiles means:
Apache: This profile opens only port 80 (normal, unencrypted web traffic).
Apache Full: This profile opens both port 80 and port 443
Apache secure: This profile opens only port 443 (TLS/SSL encrypted traffic.)
To only allow traffic on port 80, use the Apache profile:
Login to browser - http://your_server_IP
The default Ubuntu Apache web page serves both informational and testing purposes.
Install MySQL
To store and manage data for your website, you must install a database system. MySQL is a widely used database management system, often partnered with PHP environments.
When the installation is finished, it’s recommended that you run a security script that comes pre-installed with MySQL. This script will remove some insecure default settings and lock down access to your database system.
Start the interactive script by running:
This will ask if you want to configure the VALIDATE PASSWORD PLUGIN.
Answer Y for yes, or anything else to continue without enabling.
If you select "Yes," you'll need to choose a password validation level. Note that choosing the highest level 2
means your passwords must include numbers, uppercase and lowercase letters, and special characters. Otherwise, you'll encounter errors.
Run these commands -
After executing the commands, please enter your password:
Creating a MySQL Database
To create a database in MySQL, follow these steps:
Open your terminal or command prompt. Log into MySQL with the command mysql -u username -p
. Replace username
with your MySQL username. You'll be prompted to enter your password.
Once logged in, create a new database by executing CREATE DATABASE mydatabase;
. Replace mydatabase
with your desired database name.
To verify the database was created, use SHOW DATABASES;
. Your new database should appear in the list.
This process creates a new MySQL database ready for use.
To create a new database, run the following command from your MySQL console:
Now give this user permission over the example_database
database:
Now, review MySQL configurations.
Installing and testing PHP processing
Our setup includes Apache as the webserver to deliver content, MySQL for data storage and management, and PHP to process and render dynamic content to users.
After completing the installation, execute the command below to verify your PHP version:
Modifying Apache's DirectoryIndex Configuration
To adjust the file Apache uses by default when serving directories, edit the DirectoryIndex
directive in your Apache configuration file (httpd.conf or apache2.conf depending on your operating system). This change alters which file types Apache prioritizes when a directory is accessed.
It will look like this:
Add - index.php
After restarting the Apache web server, confirm the apache2 service status using the systemctl command.
Now that all your services are running create a PHP test script to confirm that Apache can handle and process requests for PHP files on the path /var/www/<DOMAIN_NAME>/
.
Add the following text, which is valid PHP code, inside the file by typing i
Press ESC :wq
to save
To ensure everything is set up correctly, visit your SERVER_PUBLIC_IP address in your web browser for a spot check.
Creating a Virtual Host for your Website
To create a directory for your_domain
, follow these steps:
Open your terminal.
Navigate to the location where you want to create the directory.
Enter the command
mkdir your_domain
and press Enter.
This creates a new directory named your_domain
in the specified location.
To change the ownership of the directory to your current system user, use the $USER
environment variable like this:
To create a new configuration file within Apache's sites-available directory, use your favorite command-line editor. We will use vi for this example:
To create a new blank file, insert the following base configuration and replace yourdomain.com
with your actual domain name:
Now, use a2ensite
to enable the new virtual host:
To disable the Apache default website—essential when not using a custom domain, as it would override your virtual host configuration—run the following command:
Ensure Your Configuration File is Error-Free: To verify that your configuration file is free of syntax errors, execute the command below:
Finally, reload Apache so these changes take effect :
Your new website is now active, but /var/www/<YOUR_DOMAIN>is still empty. Create an index.html
Include the following content in the file:
Save and close the file, then go to your browser and access your server's domain name or IP Address:
You can leave this file in place as a temporary landing page for your application until you set up a index.php
file to replace it. Once you do that, remember to remove or rename the index.html
file from your document root, as it would take precedence over a index.php
file by default.
Last updated