The first time I needed to run a PHP application on a server, I spent an embarrassing amount of time figuring out what a "stack" even meant. LAMP kept showing up in tutorials without anyone explaining what the four letters actually referred to. Linux, Apache, MySQL, PHP — four things that need to be installed and configured together for most PHP-based web apps to work.
Once I understood that, the rest made sense. I've set up LAMP stacks more times than I can count since then — on client servers, personal projects, WordPress installations. The process is pretty predictable once you've done it a few times.
This guide walks through the full installation on Ubuntu 22.04: Apache, MySQL with a proper security setup, PHP with the extensions most apps need, and a working test page to confirm everything's talking to each other.
I set this up on Tencent Cloud Lighthouse running Ubuntu 22.04 LTS. Worth noting: if your goal is specifically a WordPress or PHP-based CMS site, Lighthouse offers pre-built application images (WordPress, LAMP, LNMP) that skip the entire manual stack setup — you get a configured environment in under 10 minutes. I'm going through the manual installation here because understanding the stack is useful, but for production WordPress hosting the application image is the faster path. For other PHP apps, the clean Ubuntu image and manual setup in this guide gives you full control.
Key Takeaways
- Use the Lighthouse LAMP application image to skip the manual stack installation
- Run
mysql_secure_installationimmediately after MySQL installation- Create per-application MySQL users — never use the root account in app code
.htaccessfiles requireAllowOverride Allin the Apache virtual host config- Test PHP is working: create
/var/www/html/info.phpwith<?php phpinfo(); ?>
| Component | What it does |
|---|---|
| Linux | The operating system (Ubuntu 22.04 in our case) |
| Apache | The web server — receives HTTP requests and serves files |
| MySQL | The relational database |
| PHP | The server-side scripting language |
Good fit for LAMP:
.htaccess support (Apache handles this natively)⚡ Shortcut for WordPress users: If your goal is to run WordPress or a standard PHP CMS, Tencent Cloud Lighthouse offers a pre-built LAMP application image (and a separate WordPress image). Selecting it at instance creation gives you a fully configured Apache + MySQL + PHP environment in under 10 minutes — skip this entire manual installation guide. The manual guide below is for custom PHP applications where you want precise control over the configuration.
If you're running Node.js, Python, or Go apps, a LEMP stack (Nginx instead of Apache) or just Nginx with your runtime is often a better fit. But for PHP applications, LAMP is the standard and well-supported choice.
sudo apt update
sudo apt install -y apache2
# Start Apache and enable on boot
sudo systemctl start apache2
sudo systemctl enable apache2
# Allow HTTP and HTTPS through the firewall
sudo ufw allow 'Apache Full'
# Verify
sudo systemctl status apache2
Visit http://YOUR_SERVER_IP — you should see the Apache default page.
sudo apt install -y mysql-server
sudo systemctl start mysql
sudo systemctl enable mysql
This removes some defaults that are fine for development but not for production:
sudo mysql_secure_installation
You'll be asked several questions:
| Question | Recommended answer |
|---|---|
| Set up VALIDATE PASSWORD component? | Y (choose strength level 1 or 2 for production) |
| Remove anonymous users? | Y |
| Disallow root login remotely? | Y |
| Remove test database? | Y |
| Reload privilege tables? | Y |
sudo systemctl status mysql
# Connect to MySQL
sudo mysql
# You'll see the MySQL prompt: mysql>
# Exit with: exit
Install PHP and the modules commonly needed for web applications:
sudo apt install -y php libapache2-mod-php php-mysql
# Common additional modules (install based on what your app needs)
sudo apt install -y \
php-curl \
php-gd \
php-mbstring \
php-xml \
php-xmlrpc \
php-zip \
php-intl
Restart Apache to load the PHP module:
sudo systemctl restart apache2
Check the PHP version:
php -v
# Should output: PHP 8.1.x or similar
Create a PHP info file to verify PHP is being processed by Apache:
sudo nano /var/www/html/info.php
<?php
phpinfo();
?>
Visit http://YOUR_SERVER_IP/info.php in your browser. You should see a detailed PHP information page showing version, loaded modules, and configuration.
Remove this file after testing — it exposes server information publicly:
sudo rm /var/www/html/info.php
Never run your application as the MySQL root user. Create a dedicated database and user:
sudo mysql
-- Create a database
CREATE DATABASE myapp_db;
-- Create a user with a strong password
CREATE USER 'myapp_user'@'localhost' IDENTIFIED BY 'use_a_strong_password_here';
-- Grant permissions on the database
GRANT ALL PRIVILEGES ON myapp_db.* TO 'myapp_user'@'localhost';
-- Apply the changes
FLUSH PRIVILEGES;
-- Verify
SHOW DATABASES;
EXIT;
Test the new user logs in:
mysql -u myapp_user -p
# Enter the password you set
# Should connect successfully
Virtual hosts let you run multiple websites on one server, each with its own domain.
# Create a directory for your site
sudo mkdir -p /var/www/mysite/public_html
sudo chown -R www-data:www-data /var/www/mysite
sudo chmod -R 755 /var/www/mysite
# Create a basic index page
sudo nano /var/www/mysite/public_html/index.html
<html><body><h1>mysite.com is working.</h1></body></html>
Create the virtual host config:
sudo nano /etc/apache2/sites-available/mysite.conf
<VirtualHost *:80>
ServerName mysite.com
ServerAlias www.mysite.com
ServerAdmin webmaster@mysite.com
DocumentRoot /var/www/mysite/public_html
ErrorLog ${APACHE_LOG_DIR}/mysite_error.log
CustomLog ${APACHE_LOG_DIR}/mysite_access.log combined
<Directory /var/www/mysite/public_html>
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
# Enable the virtual host
sudo a2ensite mysite.conf
# Disable the default site (optional)
sudo a2dissite 000-default.conf
# Enable the rewrite module (needed for most PHP apps and .htaccess)
sudo a2enmod rewrite
# Test config
sudo apache2ctl configtest
# Reload Apache
sudo systemctl reload apache2
sudo apt install -y certbot python3-certbot-apache
sudo certbot --apache -d mysite.com -d www.mysite.com
Certbot handles the Apache configuration automatically — it creates an HTTPS virtual host and sets up an HTTP redirect. Certificates auto-renew every 90 days.
Test renewal:
sudo certbot renew --dry-run
phpMyAdmin gives you a browser-based interface for managing MySQL databases. It's convenient for development but adds a security consideration in production — protect it with a strong password and consider restricting access by IP.
sudo apt install -y phpmyadmin
# During installation:
# - Select "apache2" as the web server
# - Configure database for phpmyadmin with dbconfig-common: Yes
# - Set an application password
Enable the phpMyAdmin config:
sudo phpenmod mbstring
sudo systemctl restart apache2
Access it at http://YOUR_SERVER_IP/phpmyadmin.
Security note: Consider restricting phpMyAdmin access to your IP in the Apache config, or set up HTTP Basic Auth in front of it.
The most common issue after a fresh LAMP install: you visit a .php file and see the raw PHP code instead of the output.
This usually means libapache2-mod-php isn't loaded, or Apache wasn't restarted after installing PHP.
# Check if PHP module is enabled
apache2ctl -M | grep php
# Should show something like: php8.1_module (shared)
# If not, enable it
sudo a2enmod php8.1
sudo systemctl restart apache2
Also check that DirectoryIndex includes index.php. In /etc/apache2/mods-enabled/dir.conf:
DirectoryIndex index.php index.html index.cgi index.pl index.xhtml index.htm
# Apache
sudo systemctl start|stop|restart|reload apache2
sudo apache2ctl configtest # Test config syntax
sudo a2ensite SITE_NAME # Enable a virtual host
sudo a2dissite SITE_NAME # Disable a virtual host
sudo a2enmod MODULE_NAME # Enable an Apache module
tail -f /var/log/apache2/error.log # Watch error logs
# MySQL
sudo systemctl start|stop|restart mysql
sudo mysql -u root # Connect as root
mysql -u USER -p DATABASE # Connect as user to specific DB
mysqldump -u USER -p DB > backup.sql # Dump database to file
# PHP
php -v # Check PHP version
php -m # List loaded modules
php -i | grep "Loaded Configuration" # Find php.ini location
| Issue | Likely Cause | Fix |
|---|---|---|
| Connection refused | Service not running or wrong port | Check systemctl status SERVICE and verify firewall rules |
| Permission denied | Wrong file ownership or permissions | Check file ownership with ls -la and use chown/chmod to fix |
| 502 Bad Gateway | Backend service not running | Restart the backend service; check logs with journalctl -u SERVICE |
| SSL certificate error | Certificate expired or domain mismatch | Run sudo certbot renew and verify domain DNS points to server IP |
| Service not starting | Config error or missing dependency | Check logs with journalctl -u SERVICE -n 50 for specific error |
| Out of disk space | Logs or data accumulation | Run df -h to identify usage; clean logs or attach CBS storage |
| High memory usage | Too many processes or memory leak | Check with htop; consider upgrading instance plan if consistently high |
| Firewall blocking traffic | Port not open in UFW or Lighthouse console | Open port in Lighthouse console firewall AND sudo ufw allow PORT |
Does Lighthouse have a pre-built LAMP image?
Yes — select the LAMP application image to get Apache + MySQL + PHP pre-configured. No manual installation needed.
Should I use LAMP or LEMP?
For new PHP projects, LEMP (Nginx) uses less memory and handles concurrency better. LAMP with Apache is better if you need .htaccess support.
How do I check the installed PHP version?
Run php -v. List all extensions with php -m.
How do I create a MySQL database for an application?
Log in with sudo mysql, then: CREATE DATABASE myapp; CREATE USER 'user'@'localhost' IDENTIFIED BY 'password'; GRANT ALL PRIVILEGES ON myapp.* TO 'user'@'localhost'; FLUSH PRIVILEGES;
How do I enable .htaccess in Apache?
Add AllowOverride All to your site's <Directory> block, then sudo a2enmod rewrite && sudo systemctl restart apache2.
Get your cloud server running today:
👉 Tencent Cloud Lighthouse — Ubuntu VPS with full root access
👉 View current pricing and promotions
👉 Explore all active deals and offers