How to Set Up a LAMP Server (Apache, PHP, MySQL) on Ubuntu: A Beginner's Guide
About the author
John Simiyu, known as Simiyu the Software Developer, is an experienced full-time full-stack software developer with proven, robust skills in web development using PHP, Bootstrap, JavaScript, HTML, MySQL and CSS, Python for automation, and Kotlin and Java for native Android development. He has over 5 years of experience building websites, business systems and mobile apps for clients. He also runs Scolemax Solutions, a Kenyan hosting business, which is mentioned in this article.
What is a LAMP server?
LAMP is the combination of software behind a huge number of websites:
- - Linux, the operating system
- Apache, the web server that sends pages to visitors
- MySQL, the database that stores your data
- PHP, the language that builds your pages and talks to the database
If you have built a PHP and MySQL project on shared hosting, all of this was already installed for you. On your own VPS, you install and configure it yourself. That gives you control, and it teaches you how websites really run.
Before you start
You need:
- - An Ubuntu server, ideally a fresh one you can practise on
- A user account that can use sudo
- A connection by SSH
- A basic comfort with the Linux commands from my previous article
A note on Scolemax plans, based on what the homepage lists today: the Dev Containers come with stacks pre-loaded, including PHP with Apache, so if you chose one of those, much of this setup may already be done and you can skip to the testing steps. The KVM plans, Ryzen KVM from KES 1,050 per month and KVM Rootservers from KES 980 per month, give you a full server where you build the stack yourself. Please confirm current plans and which operating system images are available on scolemax.com before you buy.
Practise on a test server first, not on a live business website.
Step 1: Update the server
Always begin with fresh package lists and updates:
sudo apt update
sudo apt upgrade -yIf the server asks to restart services, accept the default. If an update says a reboot is needed, restart with sudo reboot and reconnect after a minute.
Step 2: Install Apache
sudo apt install apache2 -y
Check that it is running:
sudo systemctl status apache2
You should see "active (running)". Press q to leave the status screen.
Now open your browser and visit your server's IP address. If you see the Apache default page, Apache is working. If the page does not load, the cause is usually a firewall blocking the port, which we handle in Step 7.
Step 3: Install MySQL
sudo apt install mysql-server -yCheck that it is running:
sudo systemctl status mysql
Then run the security script:
sudo mysql_secure_installation
It will ask a few questions, such as whether to set up password validation, remove anonymous users, disallow remote root login and remove the test database. For a beginner's server, the safe answer to removing anonymous users, disallowing remote root login and removing the test database is yes. Read each question and choose carefully instead of pressing Enter without looking.
Step 4: Install PHP
sudo apt install php libapache2-mod-php php-mysql -yThese packages give you PHP itself, the Apache connection and the MySQL driver. Most PHP projects also need a few extra modules. A useful starting set is:
sudo apt install php-mbstring php-xml php-curl php-zip php-gd -yCheck the PHP version:
php -v
The version you see depends on your Ubuntu release. Make a note of it, because some projects need a specific version.
Restart Apache so it loads PHP:
sudo systemctl restart apache2Step 5: Test PHP
Create a test file:
echo "<?php phpinfo();" | sudo tee /var/www/html/info.phpVisit http://your-server-ip/info.php in your browser. You should see a page full of PHP settings.
Important: delete this file straight away after testing:
sudo rm /var/www/html/info.phpThat page reveals details about your server that attackers like to know, so never leave it on a live site.
Step 6: Create a database and a database user
Log in to MySQL:
sudo mysqlThen run these commands, replacing the names and the password with your own:
CREATE DATABASE myproject CHARACTER SET utf8mb4;
CREATE USER 'myproject_user'@'localhost' IDENTIFIED BY 'Use-A-Strong-Password-Here';
GRANT ALL PRIVILEGES ON myproject.* TO 'myproject_user'@'localhost';
FLUSH PRIVILEGES;
EXIT;Why create a separate user? Your website should not connect as the MySQL root user. If your site is ever compromised, a limited user can only reach its own database, not everything on the server.
Test the new user:
mysql -u myproject_user -p
Enter the password. If you get a MySQL prompt, it works. Type EXIT; to leave.
Step 7: Open the firewall safely
If you use UFW, allow SSH first and then the web ports:
sudo ufw allow OpenSSH
sudo ufw allow "Apache Full"
sudo ufw enable
sudo ufw statusAlways allow SSH before you enable the firewall. If you forget, you can lock yourself out of your own server. If your provider has its own network firewall as well, check that ports 80 and 443 are allowed there too.
Step 8: Create a virtual host for your website
A virtual host lets one server hold more than one website. Even for a single site, it is good practice.
Create the folder for your site:
sudo mkdir -p /var/www/mysite/public_html
sudo chown -R $USER:www-data /var/www/mysite
sudo chmod -R 755 /var/www/mysiteCreate a test page:
echo "<h1>My site is working</h1>" > /var/www/mysite/public_html/index.htmlCreate the Apache configuration file:
sudo nano /etc/apache2/sites-available/mysite.confPaste this, replacing example.co.ke with your own domain:
<VirtualHost *:80>
ServerName example.co.ke
ServerAlias www.example.co.ke
DocumentRoot /var/www/mysite/public_html
<Directory /var/www/mysite/public_html>
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/mysite-error.log
CustomLog ${APACHE_LOG_DIR}/mysite-access.log combined
</VirtualHost>Save with Ctrl+O and exit with Ctrl+X. Then enable the site, turn on URL rewriting, check the configuration and reload:
sudo a2ensite mysite.conf
sudo a2enmod rewrite
sudo apache2ctl configtest
sudo systemctl reload apache2If configtest says "Syntax OK", you are good. If it shows an error, read the line number it mentions and fix that line.
Optionally, disable the default site so it does not interfere:
sudo a2dissite 000-default.conf
sudo systemctl reload apache2Step 9: Point your domain to the server
A website needs a domain that points to your server's IP address. In your domain's DNS settings, create an A record for your domain and one for www, both pointing to your server's IP. DNS changes can take from a few minutes to several hours to spread.
If you register your domain with Scolemax, the homepage lists a .co.ke domain at KES 600 for both registration and renewal, along with DNS management. You can also use a domain from any other registrar.
Step 10: Test PHP with your database
Create a file called test-db.php in your site folder:
<?php
$pdo = new PDO(
'mysql:host=localhost;dbname=myproject;charset=utf8mb4',
'myproject_user',
'Use-A-Strong-Password-Here'
);
echo 'Database connection works.';Visit it in your browser. If you see "Database connection works", Apache, PHP and MySQL are all talking to each other. Then delete the test file, because it contains a password:
rm /var/www/mysite/public_html/test-db.php
In real projects, keep database credentials in a configuration file outside the public folder, and never in a public repository.
Step 11: Add HTTPS
Visitors expect the padlock, and browsers warn about sites without it. Let's Encrypt provides free certificates, and the tool Certbot can install and renew them automatically. I cover this in a separate article in this series, so once your domain points to your server, read that next.
Quick checklist
- - Server updated
- Apache installed and running
- MySQL installed and secured
- PHP installed and tested, with the test file deleted
- A separate database and user created
- Firewall allows SSH, HTTP and HTTPS
- Virtual host created and enabled
- Domain pointing to the server
- Database connection tested
- HTTPS added
Troubleshooting guide
- - The page will not load: check the firewall, check that Apache is running and confirm you are using the right IP address
- You see the PHP code instead of the page: PHP is not connected to Apache, so install libapache2-mod-php and restart Apache
- A 403 Forbidden error: check folder permissions and the Require all granted line
- A 500 Internal Server Error: read the Apache error log with sudo tail -n 50 /var/log/apache2/error.log
- Cannot connect to the database: recheck the database name, username, password and host
- Changes to the configuration have no effect: run apache2ctl configtest and reload Apache
The log files tell you what went wrong in almost every case, so make reading them a habit.
Common mistakes to avoid
- - Leaving info.php or test files on the server
- Using the MySQL root user for your website
- Enabling the firewall before allowing SSH
- Using weak or default passwords
- Setting folder permissions to 777
- Skipping updates
- Installing everything on a live business server without practising first
- Forgetting your own backups
Bonus points before you go
- - Bonus 1: Take a note of every command you run and why. It becomes your own server setup guide.
- Bonus 2: Once the setup works, make a backup or snapshot if your plan allows it, so you can return to a clean working state.
- Bonus 3: Do not install things you do not need. Each extra service is something else to update and secure.
- Bonus 4: Check the error logs after your first week, even if nothing seems wrong.
- Bonus 5: Many people use a control panel to avoid all of this. Doing it by hand at least once teaches you how the pieces fit, which makes you a much better developer.
Final thoughts
A LAMP server looks like a lot of steps, but each one is simple, and you now understand how Apache, PHP and MySQL work together. Build it once on a practice server, and the next one will take you minutes.
If you want to practise on a real server, you can see the Scolemax plans at https://scolemax.com. If you want a server set up, secured and ready for your project, or help moving an existing website, contact me on WhatsApp at 0739334082 or email consult@johnsimiyuweb.site.
Next, read: How to Secure a New VPS: Firewall, SSH Keys and Fail2ban


