Theme editor

Guide Linux Ubuntu LAMP Stack (Apache, MySQL, PHP) Installation Guide (2025)

  • Thread starter Thread starter CL4Y
  • Start date Start date
  • Views 140

CL4Y

Keyboard Ninja
Administrator
Thread owner

🚀 Ubuntu LAMP Stack (Apache, MySQL, PHP) Installation Guide (2025)

By following the steps below, you can quickly install the LAMP stack (Linux, Apache, MySQL, PHP) on your Ubuntu-based server and start running your web applications. LAMP Stack consists of a Linux OS, Apache web server, MySQL database, and PHP scripting language—making it an ideal setup for dynamic websites and applications. This guide is based on Ubuntu 22.04, but also works on Ubuntu 18.04 and newer.



🛠️ Step 1: Update the Server

Before installing the LAMP Stack, ensure you have a clean server with Ubuntu OS installed. As this installation is irreversible, it is highly recommended to use an empty or fresh server. Make sure your server is fully updated using the commands below, then proceed with the LAMP Stack installation.

Bash:
apt update -y && apt upgrade -y && apt autoremove -y && apt -y install wget && apt -y install nano && apt -y install curl
jetto-lamp-stack-setup-1.gif

⚠️ After running the commands, make sure to answer Yes (yes) to all prompts on screen.



🛠️ Step 2: Install Apache Web Server & Configure the Firewall

Apache is the first component of LAMP and serves your website content to users. It's open-source and easily available from Ubuntu’s repositories.

Use the command below to install Apache:
Bash:
sudo apt install apache2
Apache starts automatically after installation. You can confirm it by entering your server’s IP address in a browser and viewing the default Apache welcome page.

jetto-lamp-stack-setup-2.gif

⚠️ After running the commands, make sure to answer Yes (yes) to all prompts on screen.

🔐 Firewall Configuration

By default, Ubuntu uses UFW (Uncomplicated Firewall). To allow HTTP (port 80) and HTTPS (port 443) traffic for Apache, use the following:

Bash:
sudo ufw allow 'Apache Full'
sudo ufw enable
After this step, Apache will be accessible through both HTTP and HTTPS.

jetto-lamp-stack-setup-3.gif


🌍 Test Apache Installation

Once installation and firewall setup are complete, open your server IP address in a browser to verify Apache is working.

Try accessing:

http://your_server_ip

jetto-lamp-stack-setup-4.webp




🛠️ Step 3: Install MySQL Database Server

Most web applications require a database. In LAMP, this is handled by MySQL. Installing MySQL on Ubuntu is straightforward.

Use the command below to install MySQL:
Bash:
sudo apt install mysql-server

jetto-lamp-stack-setup-5.gif

⚠️ After running the commands, make sure to answer Yes (yes) to all prompts on screen.

Once installation is complete, MySQL will start automatically. Check its status with:
Bash:
sudo systemctl status mysql

jetto-lamp-stack-setup-6.webp


🔐 Secure MySQL Configuration

After installing MySQL, it's recommended to run the built-in secure installation script:

Bash:
sudo mysql_secure_installation

jetto-lamp-stack-setup-7.gif

⚠️ This script will help you set a root password, remove anonymous users, delete test databases, and disable remote root login. Follow the prompts carefully.

✅ To verify a successful installation, log in to MySQL shell:
Bash:
sudo mysql

If you can access the MySQL shell, everything is working correctly. To exit:
Bash:
exit

jetto-lamp-stack-setup-8.gif




🛠️ Step 4: Install PHP & Integrate with Apache

PHP allows your website to become dynamic and interact with databases. In this step, you’ll install PHP and the required modules to work with Apache and MySQL.

Use the following command:
Bash:
sudo apt install php libapache2-mod-php php-mysql

jetto-lamp-stack-setup-9.gif

⚠️ After running the commands, make sure to answer Yes (yes) to all prompts on screen.

This command installs:
  • php: Core PHP interpreter
  • libapache2-mod-php: Lets Apache interpret PHP files
  • php-mysql: Enables PHP to communicate with MySQL

⚙️ Optional: Set PHP as Default File Type

Apache determines the default index file from the dir.conf file.

To prioritize PHP over HTML:
Bash:
sudo nano /etc/apache2/mods-enabled/dir.conf

Find this line:
Bash:
DirectoryIndex index.html index.cgi index.pl index.php index.xhtml index.htm

Change it to:
Bash:
DirectoryIndex index.php index.html index.cgi index.pl index.xhtml index.htm
Save and exit (CTRL+X, then Y, then ENTER).

Restart Apache to apply:
Bash:
sudo systemctl restart apache2

jetto-lamp-stack-setup-10.gif


✅ To test PHP:
Bash:
echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/info.php

jetto-lamp-stack-setup-11.gif


Visit http://your_server_ip/info.php — if you see the PHP Info page, PHP is working correctly.

⚠️ Note: For security, delete the test file after confirming:
Bash:
sudo rm /var/www/html/info.php



🛠️ Step 5: Create a Virtual Host (Apache Virtual Server)

To host multiple websites on one server, set up Apache Virtual Hosts. In this example, we’ll configure a host for lamp.jetto.net.

🧱 1. Create the Directory Structure

Create a separate root directory for each site:
Bash:
sudo mkdir -p /var/www/lamp.jetto.net/public_html

Set correct ownership and permissions:
Bash:
sudo chown -R $USER:$USER /var/www/lamp.jetto.net/public_html

jetto-lamp-stack-setup-12.gif


🧾 2. Create the Virtual Host Config File

Navigate to Apache config directory and create the file:
Bash:
sudo nano /etc/apache2/sites-available/lamp.jetto.net.conf

Paste the following:
Bash:
<VirtualHost *:80>
ServerAdmin [email protected]

ServerName lamp.jetto.net
ServerAlias www.lamp.jetto.net

DocumentRoot /var/www/lamp.jetto.net/public_html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

jetto-lamp-stack-setup-13.gif


3. Enable the Configuration

Enable the site with:
Code:
sudo a2ensite lamp.jetto.net.conf
sudo systemctl reload apache2

jetto-lamp-stack-setup-14.gif


4. Disable the Default Apache Site

To prevent default Apache page showing at http://your_server_ip, disable the default site:
Bash:
sudo a2dissite 000-default.conf
sudo systemctl reload apache2

jetto-lamp-stack-setup-15.gif


5. Add index.html to Test the Website

To test the virtual host, create an index.html page.

Navigate to the site's directory:
Bash:
cd /var/www/lamp.jetto.net/public_html

Create an index file:
Bash:
nano index.html

Add the content: "website is working", then save and exit.

jetto-lamp-stack-setup-16.gif

⚠️ Your basic LAMP setup is now complete. We will explore more advanced topics in future guides.
 
Thread owner
Our basic installation guide is now complete. We will cover specialized software installations in our other guides. 😊
 
Back
Top