Installing a LAMP Stack on Ubuntu Server
Practical guide to installing Apache, MySQL or MariaDB, PHP and SSL on Ubuntu Server for WordPress, PHP websites and web applications.
Introduction
A LAMP stack is one of the most common server setups for hosting PHP websites. LAMP stands for Linux, Apache, MySQL or MariaDB, and PHP. On an Ubuntu VPS, this stack can run WordPress, custom PHP applications, landing pages, dashboards and many CMS-based websites.
Installing LAMP is only the first part. A usable production setup also needs firewall rules, virtual hosts, correct file permissions, database security, SSL certificates, backups, updates and monitoring.
Quick answer
To install a LAMP stack on Ubuntu, update the server, install Apache, install MySQL or MariaDB, install PHP and required extensions, configure an Apache virtual host, point DNS to the VPS, enable SSL, test PHP, secure the database, configure backups and monitor website status.
What is LAMP?
A LAMP stack is a web hosting environment made of four parts:
Linux
- The operating system. In this guide, Ubuntu Server.
Apache
- The web server that receives browser requests and serves websites.
MySQL or MariaDB
- The database server used by WordPress, CMS platforms and PHP applications.
PHP
- The programming language/runtime used by WordPress and many web applications.
LAMP is popular because it is stable, widely documented and compatible with many PHP-based websites.
When to use LAMP
A LAMP stack is a good choice when you need to host PHP-based websites or applications.
Good use cases:
- WordPress websites
- WooCommerce stores
- PHP applications
- Laravel apps
- simple business websites
- landing pages
- CMS platforms
- client websites
- staging environments
- development servers
LAMP may not be ideal when:
- your app uses Node.js only
- you need Nginx-specific reverse proxy setup
- you prefer a managed control panel
- you do not want server administration
- you need containerized deployment
For beginners, LAMP is a practical first manual web stack because it is common and well supported.
Before installation
Before installing LAMP, prepare the VPS.
Check:
- Ubuntu Server is supported
- SSH access works
- server packages are updated
- firewall allows SSH
- domain DNS access is available
- server IP address is known
- backup plan exists
- root or sudo user access is available
- expected PHP version is known
- website files and database requirements are known
Do not move production websites before the stack is installed, secured, backed up and tested.
Install Apache
Apache is the web server that will serve your website.
sudo apt update
sudo apt upgrade -y
sudo apt install apache2 -y
systemctl status apache2
sudo ufw allow 'Apache Full'
sudo ufw status
http://your-server-ip
If Apache is installed correctly, the default Ubuntu Apache page should load from the server IP.
Install MySQL or MariaDB
WordPress and many PHP applications need a database. On Ubuntu, you can use MySQL or MariaDB depending on your preference and compatibility requirements.
sudo apt install mysql-server -y
systemctl status mysql
sudo mysql_secure_installation
sudo apt install mariadb-server mariadb-client -y
sudo mysql_secure_installation
Use strong database passwords and avoid using the root database user for website applications.
Install PHP
PHP runs the application code for WordPress and many CMS platforms.
sudo apt install php libapache2-mod-php php-mysql php-cli php-curl php-gd php-mbstring php-xml php-zip php-intl -y
php -v
sudo systemctl restart apache2
Some applications require specific PHP versions or extensions. Always check the application requirements before deployment.
Create an Apache virtual host
A virtual host tells Apache which domain should serve which website directory.
Example structure:
- domain: example.com
- web root: /var/www/example.com/public_html
sudo mkdir -p /var/www/example.com/public_html
sudo chown -R www-data:www-data /var/www/example.com
sudo chmod -R 755 /var/www/example.com
echo "<h1>example.com is working</h1>" | sudo tee /var/www/example.com/public_html/index.html
sudo nano /etc/apache2/sites-available/example.com.conf
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/example.com/public_html
<Directory /var/www/example.com/public_html>
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/example.com-error.log
CustomLog ${APACHE_LOG_DIR}/example.com-access.log combined
</VirtualHost>
sudo a2ensite example.com.conf
sudo a2enmod rewrite
sudo apache2ctl configtest
sudo systemctl reload apache2
Replace example.com with your real domain. Check Apache config before reloading.
DNS setup
To make the domain load from the VPS, point DNS to the server.
Common records:
- A record for example.com → VPS IPv4 address
- AAAA record for IPv6 if used
- CNAME for www → example.com
- MX records should remain unchanged if email is hosted elsewhere
If only the website is moving to this VPS, do not change MX, SPF, DKIM or DMARC records unless email is also moving.
Check DNS before pointing your domain
Use DNS Lookup to confirm A, AAAA and CNAME records before SSL issuance and website testing.
Enable SSL with Certbot
After DNS points to the VPS and Apache serves the domain, install SSL.
sudo apt install certbot python3-certbot-apache -y
sudo certbot --apache -d example.com -d www.example.com
sudo certbot renew --dry-run
sudo systemctl reload apache2
SSL will only issue correctly if DNS points to the server and ports 80/443 are reachable.
Test PHP
Create a temporary PHP info file to confirm PHP is working.
echo "<?php phpinfo(); ?>" | sudo tee /var/www/example.com/public_html/info.php
https://example.com/info.php
sudo rm /var/www/example.com/public_html/info.php
Do not leave phpinfo files publicly accessible. They expose server configuration details.
File permissions
File permissions affect both security and website functionality.
General guidance:
- website files should not be globally writable
- web root should be owned consistently
- uploads/cache directories may need write access
- avoid chmod 777
- use separate users for deployments where possible
- keep sensitive config files protected
Wrong permissions can cause 403 errors, failed uploads, broken updates or security risks.
Backups and updates
A LAMP stack needs regular maintenance.
Back up:
- website files
- databases
- Apache virtual host configs
- SSL notes/configuration
- application environment files
- uploaded media
Maintain:
- Ubuntu security updates
- Apache updates
- PHP updates
- database updates
- application/CMS updates
- plugin/theme updates if WordPress is used
Store backups off-server. A backup only on the same VPS may be lost if the server fails.
Monitoring
Monitor the stack after setup.
Check:
- website status
- Apache service
- database service
- SSL expiry
- disk usage
- CPU and memory
- error logs
- database backups
- response time
- HTTP status codes
A LAMP stack can appear healthy until disk fills, database stops or Apache/PHP errors start. Monitoring helps catch issues earlier.
Common problems
Apache not running
HighThe web server is stopped or failed to start.
Next step: Check systemctl status apache2 and Apache error logs.
Firewall blocks HTTP/HTTPS
HighThe server works locally but cannot be reached publicly.
Next step: Allow ports 80 and 443 through the firewall.
DNS points to wrong IP
MediumThe domain does not reach the VPS.
Next step: Check A/AAAA records and DNS propagation.
PHP code downloads instead of running
HighPHP module or PHP-FPM integration is not configured correctly.
Next step: Install PHP Apache module or correct PHP handler.
Database connection fails
HighThe application cannot connect to MySQL/MariaDB.
Next step: Check database name, user, password, host and service status.
Apache virtual host not enabled
MediumApache serves default page or wrong site.
Next step: Enable the site config and reload Apache.
SSL certificate fails to issue
MediumDNS, firewall or domain validation is not ready.
Next step: Confirm DNS points to server and ports 80/443 are open.
Permissions cause 403 errors
MediumApache cannot read the website files.
Next step: Review ownership and directory permissions.
phpinfo left public
LowServer configuration details are exposed.
Next step: Delete info.php after testing.
No database backups
HighData can be lost after failure or mistake.
Next step: Set automated database backups.
How to install LAMP safely
-
Step 1: Update Ubuntu
Apply package updates before installing services.
-
Step 2: Install Apache
Install and test the web server.
-
Step 3: Install MySQL or MariaDB
Install database server and run secure installation.
-
Step 4: Install PHP and extensions
Install PHP plus extensions required by your application.
-
Step 5: Create virtual host
Configure Apache for your domain and web root.
-
Step 6: Point DNS
Set A/AAAA/CNAME records to the VPS.
-
Step 7: Enable SSL
Use Certbot or another SSL method after DNS works.
-
Step 8: Test PHP and remove test files
Confirm PHP works, then delete phpinfo files.
-
Step 9: Set backups
Back up files, databases and configuration.
-
Step 10: Monitor services
Track website status, SSL, Apache, database, disk and logs.
Useful LAMP commands
Check Apache:
systemctl status apache2
Restart Apache:
sudo systemctl restart apache2
Test Apache config:
sudo apache2ctl configtest
Check enabled sites:
ls /etc/apache2/sites-enabled/
Check Apache error log:
sudo tail -100 /var/log/apache2/error.log
Check MySQL:
systemctl status mysql
Log into MySQL:
sudo mysql
Check PHP version:
php -v
List PHP modules:
php -m
Check disk:
df -h
Check listening ports:
sudo ss -tulpn
Commands are illustrative and may vary by Ubuntu version and installed packages.
WordPress on LAMP
WordPress works well on LAMP, but it needs proper PHP extensions, database setup and file permissions.
Check:
- PHP version supported by WordPress
- php-mysql installed
- upload size limits
- memory limit
- rewrite module enabled
- SSL configured
- permalinks working
- database user with limited permissions
- backups for files and database
- caching plugin or server cache if needed
For WordPress, enable Apache rewrite module and allow .htaccess overrides in the virtual host if using permalink rules.
Frequently asked questions
What does LAMP mean?
LAMP means Linux, Apache, MySQL or MariaDB, and PHP.
Is LAMP good for WordPress?
Yes. LAMP is one of the most common stacks for WordPress hosting.
Do I need Apache or Nginx?
This guide uses Apache. Nginx is also popular, but configuration is different.
Should I use MySQL or MariaDB?
Both can work for many PHP applications. Check your application requirements.
Why does my domain show the default Apache page?
The virtual host may not be configured, enabled or matched to the domain.
Why did SSL fail?
DNS may not point to the server yet, ports 80/443 may be blocked, or the domain may not resolve correctly.
Can I host multiple websites on one LAMP server?
Yes. Create separate virtual hosts and web roots for each domain.
Related tools
Use these free tools to verify your configuration after applying changes.
Related guides
Browse all Hosting & VPS guides →Need help applying this fix?
Send us your domain, report link or issue details. CheckDomainHealth will review the request and route it to the right technical team if hands-on support is needed.
Was this guide helpful?
Your feedback helps us improve our guides for everyone.
Thanks for your feedback!