Introduction to the LAMP Stack
LAMP Stack is a group of open-source software commonly installed together so that a server can host websites and dynamic web applications written in PHP. The name is an acronym for Linux, the web server Apache, the database MySQL, and the server-side processing language PHP.
If you are new and want to deploy a server quickly, you can use a one-click installation service from a provider VPS. However, installing it yourself will help you understand the system configuration and give you more control during operation.
In this guide, you will set up the LAMP Stack on an Ubuntu 22.04 server. The Apache, MySQL, and PHP installation commands still use Ubuntu's official software repositories; the specific versions may vary depending on the Ubuntu release and when the commands are run (according to the Ubuntu Apache documentation and Ubuntu MySQL documentation).
Prerequisites
To complete this guide, you need an Ubuntu 22.04 server with a non- root root user account sudowith sudo privileges, along with a basic firewall. You can refer to the initial Ubuntu Server setup guide.
Log in via SSH using an account with sudo privileges sudo and update the package lists before you begin:
sudo apt update
sudo apt upgrade
Step 1 — Install Apache and Update the Firewall
Apache is one of the most popular web servers on Linux, with extensive documentation and broad support. Ubuntu provides the apache2 package in the official software repository (according to ubuntu.com).
Install Apache with APT:
sudo apt install apache2
When prompted for confirmation, enter Y Y ENTERand press Enter. Then, check the service status:
sudo systemctl status apache2
If you are using UFW, list the available application profiles:
sudo ufw app list
Output
Available applications:
Apache
Apache Full
Apache Secure
OpenSSH
Meaning of the UFW Profiles
- Apache: opens only port
80for HTTP. - Apache Full: opens ports
80and443for HTTP and HTTPS. - Apache Secure: opens only port
443for HTTPS.
If you have not configured a TLS certificate yet, you can allow only HTTP for initial testing:
sudo ufw allow in "Apache"
sudo ufw status
Once the website has a TLS certificate and needs to serve HTTPS, use the Apache Full configuration or open the HTTPS port according to your network design. Do not enable additional services unless necessary.
Open the server's public IP address in a browser:
http://your_server_ip
If you see the default Apache page, the web server has been installed and is accessible through the firewall.

How to Find the Server's Public IP Address
Typically, this is the address used to connect via SSH. You can check the local network addresses with:
ip addr
To see the public address identified by an external service, you can use:
curl https://icanhazip.com
If the server is behind NAT or uses a cloud network, also check the Public IP address in the provider's control panel. Not every address shown in ip addr can be accessed directly from the Internet.
Step 2 — Install MySQL
MySQL handles data storage and management for the website. Install the MySQL server with:
sudo apt install mysql-server
The service usually starts automatically after installation. Check its status with:
sudo systemctl status mysql
If the service is not running, restart it with:
sudo systemctl restart mysql
On Ubuntu, the default local MySQL account root usually authenticates through the system socket. Therefore, you can access MySQL with:
sudo mysql
Ubuntu currently recommends creating a separate account with appropriate privileges instead of using the root account for applications (according to the Ubuntu MySQL configuration documentation). Exit the MySQL interface:
exit;
On some Ubuntu versions or MySQL packages, you can also run the interactive security checker:
sudo mysql_secure_installation
The questions and options may vary by version. Read each prompt carefully; you should generally remove anonymous accounts, restrict root remote logins, and remove the test database if the system offers these options. Do not mechanically apply answers from older versions.
Create a separate MySQL database and account
The example below creates a separate database and user for the application. Replace the sample values with your own names and a strong password:
sudo mysql
CREATE DATABASE app_db CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'app_user'@'localhost' IDENTIFIED BY 'ThayBangMatKhauManh';
GRANT ALL PRIVILEGES ON app_db.* TO 'app_user'@'localhost';
FLUSH PRIVILEGES;
EXIT;
Ubuntu recommends using the modern password authentication method provided by the MySQL version. Use the legacy method only when the application genuinely requires it and you have checked compatibility (according to the Ubuntu MySQL account documentation).
The MySQL server is now installed and basically configured. Next, install PHP.
Step 3 — Install PHP
Apache delivers content, MySQL stores data, and PHP processes dynamic code. To enable Apache to run PHP and PHP to connect to MySQL, install the required packages:
sudo apt install php libapache2-mod-php php-mysql
Ubuntu provides the php, the PHP module for Apache, and php-mysql in the official documentation (according to the Ubuntu PHP installation documentation).
Check the actual PHP version installed from the Ubuntu repositories:
php -v
Do not hard-code a result such as PHP 8.1.2 in the guide, because the version depends on the Ubuntu release, update repositories, and installation date. As of August 23, 2026, the PHP 8.5 branch is actively supported; PHP 8.4 is still within its support period, while PHP 8.2 receives only security fixes until December 31, 2026 (according to PHP.net). Prioritize the version provided by your Ubuntu repositories and check your application's compatibility.
Restart Apache to ensure that the new module and configuration are loaded:
sudo systemctl restart apache2
Step 4 — Create a Virtual Host for the Website
Apache allows you to create multiple Virtual Hosts to host multiple domains on the same server. In this example, replace your_domain with your actual domain name and ensure that the DNS record points to the server's IP address.
You can also view the guide to pointing a domain to a server.
Instead of using the default directory directly, /var/www/htmlcreate a separate directory for the website:
sudo mkdir -p /var/www/your_domain
sudo chown -R "$USER":"$USER" /var/www/your_domain
Create the Virtual Host configuration file:
sudo nano /etc/apache2/sites-available/your_domain.conf
Add the following basic configuration:
<VirtualHost *:80>
ServerName your_domain
ServerAlias www.your_domain
ServerAdmin webmaster@localhost
DocumentRoot /var/www/your_domain
<Directory /var/www/your_domain>
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/your_domain_error.log
CustomLog ${APACHE_LOG_DIR}/your_domain_access.log combined
</VirtualHost>
Save the file and exit nano with CTRL+X, press Y and ENTER. Enable only AllowOverride All if you need to use the .htaccessfile; otherwise, you can omit the Directory block or use a more restrictive configuration.
Enable the Virtual Host, check the syntax, and reload Apache:
sudo a2ensite your_domain.conf
sudo apache2ctl configtest
sudo systemctl reload apache2
If you do not need Apache's default page, you can disable the default site:
sudo a2dissite 000-default.conf
sudo apache2ctl configtest
sudo systemctl reload apache2
If you do not yet have a domain name, keep the default site or create a separate test configuration. When using a real domain name, check DNS and open the correct ports on the firewall before accessing it.
Step 5 — Test PHP with Apache
Create a PHP test file in the website directory:
nano /var/www/your_domain/info.php
<?php
phpinfo();
Open http://your_domain/info.php in a browser. If the PHP information page appears, Apache has successfully processed the PHP file.
After testing, delete this file because it displays extensive configuration information that could help attackers:
rm /var/www/your_domain/info.php
Step 6 — Next Steps
- Configure HTTPS with a valid TLS certificate before putting the website into production.
- Open only the ports required on UFW, and do not allow MySQL access from the Internet unless the application requires it.
- Use a separate MySQL account for each application, with the minimum permissions required.
- Set up database backups and periodically test your ability to restore them.
- Update Ubuntu, Apache, MySQL, and PHP according to your maintenance procedures.
You have completed the core components of the LAMP Stack on Ubuntu: Apache serves the website, MySQL manages data, and PHP processes dynamic content.

