---
slug: Ubuntu 18.04 LAMP Stack Setup Step-by-Step Guide
title: Ubuntu 18.04 LAMP Stack Setup Step-by-Step Guide
authors: [slorber, yangshun]
tags: [hola, docusaurus]
---
import CodeBlock from '@site/src/components/CodeBloack';
import ubuntu from '@site/static/img/small_apache_default.png';
import lamp from '@site/static/img/LampStack.jpg';
Introduction
Think of a 'LAMP' stack like a group of helpful computer tools that work together to create awesome websites. Let's meet the team:
- L for Linux: This is like the computer's main system.
- A for Apache: Meet the web server, it helps people see your website.
- M for MySQL: Imagine this as a smart storage space where your website keeps important things.
- P for PHP: This is the creative part that makes your website do cool and interesting stuff.
Now, we're going to set up this friendly team on a computer using Ubuntu 18.04. Ready to get started?
What You Need Before We Begin
Before we start, make sure you have a special kind of computer called an 'Ubuntu 18.04 server.' Also, you'll need a special account on that computer that can do important things, like installing stuff. We'll guide you on how to set this up in an easy way. Let's get started!
Step 1: Installing Apache and Setting Up the Firewall
Apache is a common web server used to host websites. It works great with PHP to make dynamic websites. Let's get it installed and make sure our computer's security is set up.
Start by making sure your computer has the latest updates by typing:
If it's your first time using 'sudo' today, it might ask for your usual computer password to make sure you're allowed to do special things.
After updating, let's get Apache installed. Just type:
After typing the command, your computer will tell you about some things it wants to add. It will also say how much space it needs. Just press 'Y' and then 'ENTER' to say 'Yes,' and it will start adding everything.
Making Sure Your Firewall Welcomes Web Traffic
Now, let's make sure the security system on your computer is ready to let in web visitors. If you followed our setup guide before, we want to tell it to allow people to see our website.
Check if the firewall knows about Apache by typing:
If you check the details of the Apache Full profile, you'll find that it allows visitors to come in through ports 80 and 443:
To let people visit your website, type the following to allow incoming traffic for both regular websites and secure websites:
To make sure everything is working, open your web browser and visit your computer's special address. If you're not sure what it is, we'll show you how to find it in the next part.
When you open the web browser, you might see a page that comes with Ubuntu 18.04 and Apache. Don't worry, it's just there to show things are working fine. It should look a bit like this:
If this page shows up, it means your web server is set up properly and can be reached through the firewall.
Finding Your Server's Public IP Address
If you're not sure about your server's special address that everyone can use to connect to it, don't worry. We'll show you a few easy ways to find it. This address is usually what you use to talk to your server through SSH.
One way to find it is by using some simple commands. Let's start with this one:
After typing the command, you'll get a couple of lines. Each line is a correct address, but your computer might only use one of them. You can try each one to see which works.
Another way is to use a special tool called 'curl' to ask an external friend what address it sees for your server. It's like getting a second opinion. Let's try this:
No matter how you found your special address, just type it into the top bar of your web browser. This will show you the default Apache page and confirm everything is working fine.
Now that your web server is ready, let's add MySQL. It's like a smart organizer for your website's information. It stores and helps manage all the data your site needs.
Just like before, we'll use 'apt' to get and install MySQL. Ready?
Note: No need to run 'sudo apt update' again before this command. You already did it earlier when installing Apache. Your computer's list of available software is already updated and ready to go.
After typing the command, it will show you a list of things it's going to add and how much space they'll take up. Just type 'Y' to keep going.
Once it's finished installing, let's run a security script that's already included with MySQL. This script helps keep your database safe by fixing some risky settings and making sure only authorized users can access it.
To start the security script, type:
It will ask if you want to set up a password-checking tool. This tool helps make sure your passwords are strong and secure.
Deciding to turn on this feature is a personal choice. If you enable it, MySQL will reject passwords that aren't very strong, which is good for security. However, it might cause problems if you use certain software, like phpMyAdmin. If you're not sure, it's okay to leave this off. Just make sure to always use strong and unique passwords for your database.
If you say 'yes,' it will ask you to pick how strict you want the password rules. If you choose the strongest level (entering '2'), it will give you errors if your password doesn't have numbers, both uppercase and lowercase letters, and special characters. So, choose carefully based on how strong you want your passwords to be.
= 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
`} />
Whether you chose the password-checking tool or not, your computer will now ask you to pick and confirm a password for the main user of your database (MySQL root user). This user has full control over everything in the database.Even though this user doesn't always need a password, it's a good idea to give one for extra safety. We'll discuss this in a bit.
If you turned on password checking, it will tell you how strong your password is. Then, it will ask if you want to change it. If you like your password, just type 'N' for 'no' and keep it.
For the other questions, just press 'Y' and then press the ENTER key. This will do a few things like removing some users and a test database, and making sure remote logins aren't allowed for the main user.
Once you're done, let's check if you can get into the MySQL console by typing:
This will let you into the MySQL server as the main user, which is called 'root.' We're using 'sudo' to do this. You should see something like this after typing the command:
`} />
Even though you set a password during setup, you didn't need to type it here. That's because the way your computer checks passwords is a bit different for the main user.This might seem odd, but it actually makes things safer. Only users with special access, like you using the console or certain applications, can log in as the main MySQL user. In everyday terms, this means you won't use this user for your regular website. Having a password is still a good idea, just in case things change in the future.
For better security, it's a good idea to create separate user accounts with limited access for each database you have, especially if you'll have more than one database on your server. You can learn how to do this in our guide: 'Creating a New User and Setting Permissions in MySQL'. It has step-by-step instructions to help you set up MySQL users and manage who can access your databases.
Now that your MySQL server is set up and safe, let's move on to installing PHP, the last part of the LAMP stack.
Step 3: Adding PHP
PHP is like the brain that makes your website do things. It processes code, connects to your databases, and shows dynamic content to visitors.
Let's use 'apt' again to add PHP. We need three packages: 'php' for PHP itself, 'libapache2-mod-php' to connect PHP with Apache, and 'php-mysql' so PHP can talk to MySQL. Type this command to get them all:
PHP should install smoothly. We'll check it shortly to make sure everything's working.
Changing How Apache Shows Directory Contents (Optional)
Sometimes, you might want Apache to prioritize certain files when someone visits a directory on your website. By default, Apache looks for an 'index.html' file first. But if you want it to look for 'index.php' files first, we can change that.
To do this, we'll edit a configuration file called 'dir.conf.' We'll use a simple text editor called 'nano.
You'll see something like this:
DirectoryIndex index.html index.cgi index.pl index.php index.xhtml index.htm
`} />
Now, take the highlighted 'index.php' and move it to the top of the list, like this:
DirectoryIndex index.php index.html index.cgi index.pl index.xhtml index.htm
`} />
When you're done, press CTRL+X to save and close the file. Confirm by typing 'Y' and pressing ENTER.
Then, restart the Apache web server so it can apply your changes. Just use this command:
To see if Apache is running okay, you can use the following command:
To exit this status display, simply press the 'Q' key.
Adding Extra Features to PHP (Optional)
If you want to do more with PHP, you can add extra modules. To see what's available, use this command:
You can use the arrow keys to move up and down, and press 'Q' to exit.
These results show extra things you can add to PHP. Each one has a brief explanation.
If you want to know more about what a module does, you can search online for information about it. Or, you can see a detailed description of the package by typing:
You'll see a bunch of information. Look for the 'Description' field; it gives a detailed explanation of what each module does.
For instance, to understand the php-cli module, you can type
Among other details, you'll see something like this:
If you find a package you want to install after researching, you can use the 'apt install' command just like before.
For example, if you want to install php-cli, you would type:
If you need to install multiple modules, you can list them all after the 'apt install' command, separated by spaces, like this:
Now that your LAMP stack is set up, it's a good idea to create an Apache virtual host to store your server's configuration details. This helps in managing your website.
Step 4 — Setting Up a Virtual Host for Multiple Domains on Apache
Let's create a virtual host to manage configuration details and host multiple domains on your Apache web server. In this guide, we'll use a domain called 'your_domain,' but make sure to replace it with your actual domain name. If you're not sure how to set up a domain with Cloudtopiaa, check our Introduction to Cloudtopiaa DNS guide
By default, Ubuntu 18.04 comes with one server block that serves content from /var/www/html. While this is fine for a single site, managing multiple sites can get messy. To keep things neat, let’s make a new space for your_domain within /var/www, leaving /var/www/html as the default if no specific site is requested.
Create the directory for your_domain like this:
Now, let’s make sure the directory is owned by the current user. This is done with the $USER variable, which represents the user currently logged in:
To verify the permissions of your web root directory, you can use the following command:
Then, let's create a simple index.html page using the nano text editor or any other editor you prefer:
Inside the file, paste the following example HTML code:
Welcome to Your_domain!
Success! The your_domain server block is working!