Back

How to install LAMP stack (Linux, Apache, MySQL, PHP)

How to install LAMP server (Linux, Apache, MySQL, PHP)

LAMP ("Linux-Apache-MySQL-PHP") is a popular open-source stack for hosting dynamic websites. Because it is free, highly adaptable, and extremely reliable, LAMP powers countless sites across the Internet.

In this tutorial you’ll learn how to install the LAMP stack on Ubuntu 22.04 Server.


1. Installing Apache and firewall

Update the package index:

sudo apt update

Install the Apache web server:

sudo apt install apache2

Enable and start Apache service so it launches automatically when the server boots:

sudo systemctl enable --now apache2

If you are using Ufw firewall, open ports 80 (HTTP) and 443 (HTTPS):

sudo ufw allow in "Apache"


Verify Apache is running by visiting your server’s IP address in a browser. For example, if the public IP is 12.34.56.78:

http://12.34.56.78/



2. Installing MySQL

To store your site’s data you’ll need a MySQL database. Install it with:

sudo apt install mysql-server

Because of an authentication quirk on Ubuntu servers, switch the root user to the native password method:

sudo mysql

Inside the MySQL prompt, run (replace password with your own strong password):

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';

After making changes, exit MySQL environment:

exit

Then launch the security script for configuring MySQL:

sudo mysql_secure_installation

The wizard first asks to enable the VALIDATE PASSWORD PLUGIN. You can choose Y (Yes) for additional security.

Choose a password-policy level (0 = LOW, 1 = MEDIUM, 2 = STRONG). Example:

There are three levels of password validation policy:

LOW    Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary file

Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 1

You’ll be prompted to set the MySQL root password. Because root has full control, pick something secure.

The plugin will rate your password and ask for confirmation. Enter "Y" (Yes):

Estimated strength of the password: 100
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y

For the remaining questions, press Y and Enter.


After finishing MySQL installation, check if you can connect to the database with a root user:

sudo mysql

Exit MySQL environment:

exit


3. Installing PHP

With Apache and MySQL ready, install PHP - the language that processes code and displays dynamic content. Add the php-mysql and libapache2-mod-php modules so PHP can talk to MySQL and Apache can handle PHP files:

 sudo apt install php libapache2-mod-php php-mysql

After installation is done, verify that it works by viewing the PHP version:

php -v


Additionally, to verify that Apache is integrated with PHP, create this file in the default web document root for testing:

sudo nano /var/www/html/info.php

And paste the following content:

testing
Save and close the file. If using nano editor: CTRL + X, then Y and Enter.

Then visit the site and verify that PHP is working:

http://12.34.56.78/info.php


4. Configuring virtual hosts

Virtual hosts let you serve multiple independent sites on the same Apache server.

Replace every instance of "yourdomain" with your actual domain name.

Please also make sure that your domain's DNS A record points to your server's IP address.


Create a directory for the site:

sudo mkdir /var/www/yourdomain
Give your current user ownership of the directory:

sudo chown -R $USER:$USER /var/www/yourdomain

Create a new virtual host file

sudo nano /etc/apache2/sites-available/yourdomain.conf

Paste the following text in the configuration file:


    ServerName yourdomain.com
    ServerAlias www.yourdomain.com
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/yourdomain
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

Save and close the file. If using nano editor: CTRL + X, then Y and Enter.

Enable the new virtual host:

sudo a2ensite yourdomain

Reload Apache service:

sudo systemctl reload apache2


Your site is now active, but the /var/www/yourdomain directory is empty.

Create a test index.html file:

nano /var/www/yourdomain/index.html

Add any text you like, which will be later displayed on the browser.

Save and close the file. If using nano editor: CTRL + X, then Y and Enter.


Open the site in a browser to view your test page:

http://yourdomain.com

In summary, to setup LAMP stack, you need to install Apache2, MySQL and PHP packages on a Linux server, and configure them accordingly. If you have any questions or run into issues, feel free to contact our live support or email [email protected].

Similar articles

How to run Grafana in Docker containerGrafana is a leading open-source tool for visualizing time series data. It’s widely used to visualize data from various...

Read

How to create a user account on Ubuntu serverUbuntu typically comes with a single main user who has root (administrative) privileges. In many cases, you’ll...

Read
Linux Tutorials