---
slug: Creating Your Website Installing the LAMP Stack on Ubuntu 20.04
title: Creating Your Website Installing the LAMP Stack on Ubuntu 20.04
authors: [slorber, yangshun]
tags: [hola, docusaurus]
---
import CodeBlock from '@site/src/components/CodeBloack';
import ubuntu from '@site/static/img/small_apache_default.png'
import hello from '@site/static/img/landing_page.png'
import php from '@site/static/img/php7.4.13.jpg'
import todo from '@site/static/img/todo_list.png'
Introduction
In simple terms, a "LAMP" stack is a bundle of open-source software installed together to make a server capable of hosting dynamic websites and web apps built in PHP. "LAMP" stands for Linux (the operating system), Apache (the web server), MySQL (the database), and PHP (the programming language).
In this guide, we'll show you how to set up a LAMP stack on an Ubuntu 20.04 server.
What You Need Before Starting
Before we start, make sure you have an Ubuntu 20.04 server set up. You'll also need a user account with sudo privileges and a basic firewall. If you haven't done this yet, you can follow our guide for setting up your server.
Step 1 —Setting Up Apache and Securing Your Connection
Apache is one of the most popular web servers globally. It's widely used, well-supported, and has been a key part of the internet for a long time. That's why it's a fantastic option for hosting websites.
Let's get started by updating the package manager cache. If it's your first time using sudo in this session, you'll be asked for your password to confirm that you have the necessary privileges to manage system packages with apt.
Next, let's put Apache on your computer! Just type this command and hit enter:
After you type the command to install Apache, your computer might ask you a question. Just press "Y" on your keyboard and then hit enter.
Once Apache is finished installing, we need to make sure your computer's security settings allow it to work properly. We'll use something called UFW to do this. UFW has different settings for different types of programs and we'll pick the one for Apache.To see all the different settings UFW has, just type this command:
You'll see some information on your screen that looks like this:
Here's what each of these options means:
- Apache: This option allows regular web traffic on port 80, like opening the main door for visitors.
- Apache Full: It allows both regular web traffic (port 80) and secure traffic using TLS/SSL encryption (port 443).
- Apache Secure: This option only opens the special secure door for traffic using TLS/SSL encryption (port 443).
For now, it's a good idea to only allow connections through the main door (port 80). Since we just installed Apache and haven't set up a special security certificate yet, it's not ready for secure traffic (HTTPS).
To do this, we'll use the Apache profile:
You can make sure everything is working by checking with:
Now, your firewall is letting traffic through the main door (port 80).
To make sure everything is working, you can quickly check by opening your web browser and visiting your server's public IP address. If you're not sure what your public IP address is, there's a note in the next section that will help you find it.
When you visit your server's address in the web browser, you'll see a special page that comes with Ubuntu 20.04 and Apache. This page is just there to give you information and check if everything is set up correctly. It might look something like this:
If you can see this page, it means your web server is all set up and working properly. You've successfully installed it, and people can now visit your website through the firewall. Good job!
Finding Your Server's Public IP Address
If you're not sure about your server's public IP address (the special code to find your server on the internet), there are a couple of easy ways to check. This is the address you use when you want to connect to your server, for example using SSH.
One way is to use a tool called "iproute2." It's like a detective tool for your computer. You can find your IP address by typing this command:
When you type that command, your computer will show you two or three lines of information. All of them are correct addresses, but your computer might only be able to use one. You can try each one to see which works.
Another way is to use a tool called "curl." It's like sending a message to a special server and asking, "Hey, what's my IP address?" You can do this by typing a command:
No matter how you found your IP address, just copy and paste it into the address bar of your web browser. This way, you can check out the default Apache page and see if everything is working as expected.
Step 2 — Your Database: Installing MySQL
Now that your web server is ready, let's add the database system. This helps store and manage data for your website. MySQL is a popular choice for this, especially when you're using PHP.
Just like before, we'll use the "apt" tool to get and install MySQL. It's a simple command:
When your computer asks if you want to install MySQL, just type "Y" and press ENTER.
After the installation, it's a good idea to make things more secure. There's a special tool that comes with MySQL to help with this. It's like a virtual security assistant for your database. To use it, just type a command:
This will check if you want to set up something called the VALIDATE PASSWORD PLUGIN.
Note: Deciding to turn on this feature is up to you. If you enable it, MySQL will only accept passwords that meet certain criteria. However, it's okay to leave this validation turned off. Just make sure to always use strong and unique passwords for your database – that's important!
Type "Y" if you want to turn it on, or press any other key if you want to continue without turning it on.
If you say "yes," you'll need to choose how strict you want the password rules to be. If you pick level 2 for the strongest rules, keep in mind that your passwords must have numbers, both upper and lower case letters, special characters, or should not be common words. If this sounds a bit confusing or strict, you can choose a lower level for simpler rules.
= 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 decided to use the password check feature or not, your server will now ask you to choose and confirm a password for the MySQL root user. This is different from the system root. The database root user is like a super admin with full control over the database system.Even though MySQL usually doesn't need a password for the root user by default, it's safer to set a strong password here. We'll discuss why in a moment
If you turn on password validation, you'll see how strong your root password is. Your server will then ask if you want to keep that password. If you're happy with it, just type "Y" for "yes" when prompted.
For the remaining questions, just press "Y" and then press the ENTER key each time. This will remove some unnecessary users and databases, make sure remote logins are secure, and apply these changes to MySQL right away.
Once you're done, let's check if you can log in to the MySQL console. Just type:
This command will connect you to the MySQL server as the main admin user, called "root." This is automatically done using special permission ("sudo") when you run the command. You'll see some information displayed on your screen, which should look like this:
`} />
To leave the MySQL console, just type:
Even though you set a password during the setup, you might notice that you didn't have to enter it when logging in as the root user. This might seem unusual, but it's actually a security feature. The default way to log in as the main MySQL admin is a bit different – it uses the system's authentication instead of a password. This makes your database server more secure because only users with special permissions (like sudo privileges) can log in as the root MySQL user. In simple terms, you won't be using this root user directly for your website. The password you set is like a backup plan, just in case the default login method changes in the future.
To make things more secure, it's a good idea to create separate user accounts for each database, especially if you're planning to have more than one database on your server. This way, each database has its own special access, and it helps keep everything safer.
Note : As of now, the standard library used by PHP for MySQL doesn't work perfectly with the default way MySQL 8 checks passwords. So, when you're setting up users for your PHP applications, you'll need to configure them to use a specific method called "mysql_native_password." We'll show you how to do that later on.
Your MySQL server is all set up and safe. Now, let's move on to installing PHP, which is the last piece needed for the LAMP stack.
Step 3 - Adding PHP to Your Setup
Now that your server can show web pages and manage data, let's bring in PHP. This is what makes your website do cool things by processing code. To make it work smoothly, we'll install the "php" package. Also, we need "php-mysql" to let PHP talk to MySQL, and "libapache2-mod-php" to help Apache handle PHP files. Don't worry about these names; they'll be installed automatically.
Just type this command to get everything set up:
After everything is installed, you can check your PHP version by typing the following command:
Now that your LAMP stack is up and running, it's almost ready for your website. But before you test it with a PHP script, let's make sure everything is organized. We'll set up a special space in Apache called a "Virtual Host" to hold all your website's files and folders. We'll do that in the next step.
Step 4 - Organizing Your Website: Creating a Virtual Host
When you're running a website using the Apache web server, you can create something called virtual hosts. These are like individual compartments that hold all the settings for a specific website. They allow you to host more than one website on a single server. In this guide, we'll set up a pretend domain called your_domain. But remember, you should use your own domain name instead of "your_domain". Let's get started!
Apache on Ubuntu 20.04 comes with a default setup that serves web pages from a directory called /var/www/html. This works fine if you only have one website. However, if you want to host multiple websites, it can get messy. Instead of changing the default setup, we'll create a new directory for each website within the /var/www folder. This way, the /var/www/html directory will still be there to serve pages if no specific website is requested.
To do this for your_domain, follow these steps:
Next, we need to give you control over the directory we just created. To do this, we'll use a special code that references your username on the system. This ensures that you have the necessary permissions to manage the files and folders inside.
Next, we'll open a new file where we can configure our website settings. We'll do this in a specific directory where Apache keeps these configuration files. We'll use a basic text editor called nano for this task.
This will make a new empty file where we can put some basic settings for our website. Copy and paste the following simple configuration into this file.
ServerName your_domain
ServerAlias www.your_domain
ServerAdmin webmaster@localhost
DocumentRoot /var/www/your_domain
ErrorLog $ {APACHE_LOG_DIR}/error.log
CustomLog $ {APACHE_LOG_DIR}/access.log combined
`} />
Now, to save and close the file, if you're using the nano editor, press CTRL+X, then Y, and finally ENTER.
With this configuration, Apache knows to use the /var/www/your_domain folder as the place to find the files for your website. If you don't have a domain name yet or just want to test things out, you can ignore the ServerName and ServerAlias options for now.
Once you've saved and closed the file, you can activate the new virtual host by running the command:
If you're not using a custom domain name, you may want to turn off the default website that Apache comes with. This is important because otherwise, Apache's default setup will take precedence over your new virtual host.
To do this, you just need to type the following command:
To double-check that there are no mistakes in your configuration file, you can run the following command:
Finally, to apply these changes and make them active, you just need to reload Apache. You can do this by running the following command:
Your new website is up and running, but there's nothing in the web folder yet. To test if everything is working correctly, we'll create a simple web page. Navigate to the /var/www/your_domain directory, and create a new file called index.html. This file will contain the content of your webpage.
Inside the index.html file, add the following content:
your_domain website
Hello World!
This is the landing page of your_domain.