Prerequisites
You should use a non-root user account with sudo privileges. Please see the Initial server setup for Ubuntu 22.04 guide for more details.
1. Install Apache
Begin by updating the package lists and installing Apache on Ubuntu 22.04.
sudo apt update && sudo apt install apache2
Press y
and ENTER
when asked to continue. Installation may take a few minutes. Once installed, continue to Step 2 to configure the firewall on Ubuntu 22.04.
2. Configure Firewall
It is highly recommended that you configure a firewall to provide added security to your LAMP Stack on Ubuntu 22.04.
We’ll start by adding a firewall rule for SSH because if you are configuring your server remotely, you don’t want to get locked out when enabling the firewall. If the rule already exists, the command will just skip it.
sudo ufw allow OpenSSH
If you get an error “ERROR: could find a profile matching openSSH”, this probably means you are not configuring the server remotely and can ignore it.
Now we can add the firewall rules for Apache.
sudo ufw allow in "Apache Full"
Now enable the firewall if it isn’t already.
sudo ufw enable
Press y
if you see a message “Command may disrupt existing ssh connections”.
If the firewall was activated correctly, you should see “Firewall is active and enabled on system startup“.
You can also check the current firewall status with:
sudo ufw status
Below we can see the firewall is active and has two rules per service. v6
is short for IPv6.
Status: active
To Action From
-- ------ ----
OpenSSH ALLOW Anywhere
Apache Full ALLOW Anywhere
OpenSSH (v6) ALLOW Anywhere (v6)
Apache Full (v6) ALLOW Anywhere (v6)
3. Test Apache
To see if Apache installed correctly on Ubuntu 22.04, we can check the current service status.
sudo service apache2 status
If it is up and running, you should see a green active state.
● apache2.service - The Apache HTTP Server
Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
Active: active (running) since Thu 2022-05-19 13:34:28 UTC; 2min 43s ago
Docs: https://httpd.apache.org/docs/2.4/
Process: 684 ExecStart=/usr/sbin/apachectl start (code=exited, status=0/SUCCESS)
Main PID: 771 (apache2)
Tasks: 55 (limit: 1119)
Memory: 7.5M
CPU: 45ms
CGroup: /system.slice/apache2.service
├─771 /usr/sbin/apache2 -k start
├─780 /usr/sbin/apache2 -k start
└─781 /usr/sbin/apache2 -k start
May 19 13:34:27 devanswers systemd[1]: Starting The Apache HTTP Server...
May 19 13:34:28 devanswers apachectl[749]: AH00558: apache2: Could not reliably determine the server's f>
May 19 13:34:28 devanswers systemd[1]: Started The Apache HTTP Server.
If you get the above error about a fully qualified domain name, you can ignore it.
You may need to press q
to exit the server status if using an SSH client.
Now that the Apache service is up and running, you should be able to view the test Apache web page through your web browser.
Enter the IP address of your server in the address bar and hit ENTER
. If you don’t know your IP, you can find out with the following command.
ip addr show ens3 | grep inet | awk '{ print $2; }' | sed 's/\/.*$//'
You’re all set! You can find this Apache default welcome page in the folder /var/www/html
. To edit this file:
sudo nano /var/www/html/index.html
Press CTRL
+ X
to exit the nano text editor.
If you have a domain name, you can point it to the IP address of your server without further configuration and Apache should load this default page. However, if you want to host multiple domains on your server or set up SSL certs with Let’s Encrypt, you should set up a virtual host in Step 8 at the end of this guide.
4. Install MySQL
The next component of your LAMP Stack on Ubuntu 22.04 is MySQL.
Begin by updating the repositories and installing the MySQL package.
sudo apt install mysql-server
Press y
and ENTER
if prompted to install.
Once the package installer has finished, we can check to see if the MySQL service is running.
sudo service mysql status
If running, you will see a green Active status like below.
● mysql.service - MySQL Community Server
Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
Active: active (running) since Thu 2022-05-19 13:45:14 UTC; 5s ago
Process: 2396 ExecStartPre=/usr/share/mysql/mysql-systemd-start pre (code=exited, status=0/SUCCESS)
Main PID: 2404 (mysqld)
Status: "Server is operational"
Tasks: 38 (limit: 1119)
Memory: 355.0M
CPU: 721ms
CGroup: /system.slice/mysql.service
└─2404 /usr/sbin/mysqld
May 19 13:45:13 devanswers systemd[1]: Starting MySQL Community Server...
May 19 13:45:14 devanswers systemd[1]: Started MySQL Community Server.
You may need to press q
to exit the service status if using an SSH client.
5. Configure MySQL Security
You should now run mysql_secure_installation
to configure security for your MySQL server on Ubuntu 22.04.
sudo mysql_secure_installation
5.1. Validate Password Component (Optional)
You will be asked if you want to set up the Validate Password Plugin. It’s not really necessary unless you want to enforce strict password policies for some reason.
Securing the MySQL server deployment.
Connecting to MySQL using a blank password.
VALIDATE PASSWORD COMPONENT can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD component?
Press y|Y for Yes, any other key for No:
Press ENTER
if you don’t want to set up the validate password plugin.
5.2. Create Root Password
If you haven’t created a root password for MySQL yet, you must create one now.
Please set the password for root here.
New password:
Re-enter new password:
Generate a strong password and enter it. Note that when you enter passwords in Linux, nothing will show as you are typing (no stars or dots).
Update May 2022: If you see this error “… Failed! Error: SET PASSWORD has no significance for user ‘root’@’localhost’ as the authentication method used doesn’t store authentication data in the MySQL server. Please consider using ALTER USER instead if you want to change authentication parameters.” – please read this article and return here later.
5.3. Remove Anonymous Users?
You will next be asked to remove anonymous users.
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.
Remove anonymous users? (Press y|Y for Yes, any other key for No) :
Press y
and ENTER
to remove anonymous users.
5.4. Disallow root login remotely?
Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.
Disallow root login remotely? (Press y|Y for Yes, any other key for No) :
Press y
and ENTER
to disallow root login remotely.
5.5. Remove test database and access to it?
By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.
Remove test database and access to it? (Press y|Y for Yes, any other key for No) :
Press y
and ENTER
to remove the test database.
5.6. Reload privilege tables now?
Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.
Reload privilege tables now? (Press y|Y for Yes, any other key for No) :
Press y
and ENTER
to reload the privilege tables.
All done! 😀
5.7. Test MySQL Service
The MySQL component of your LAMP Stack on Ubuntu 22.04 is now ready.
You should log into the MySQL server to ensure it is working correctly. Because you are running this command as sudo
, it will automatically log into MySQL using the MySQL root account. If you are prompted for a password, enter your Linux account password, not the MySQL one.
sudo mysql
Output:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 16
Server version: 8.0.31-0ubuntu0.22.04.1 (Ubuntu)
Copyright (c) 2000, 2022, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
If you instead get an error “ERROR 1045 (28000): Access denied for user ‘root’@’localhost’ (using password: NO)” try running the command mysql -u root -p
and then enter your MySQL root password manually.
To exit MySQL, type exit and press ENTER
.
exit
You have now successfully installed and configured MySQL for your LAMP Stack on Ubuntu 22.04.
6. Install PHP
The next and final component of your LAMP Stack on Ubuntu 22.04 is PHP.
Begin by installing the PHP package. We will also install two more packages libapache2-mod-php
and php-mysql
, which will allow PHP to communicate with the MySQL database.
sudo apt install php libapache2-mod-php php-mysql
Press y
and ENTER
when prompted to install the PHP package.
7. Test PHP
7.1. Test PHP in Command Line
Once the package has finished installing, we can test PHP in the command line.
php -version
If PHP installed correctly, you should see something similar below:
PHP 8.1.2 (cli) (built: Apr 7 2022 17:46:26) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.2, Copyright (c) Zend Technologies
with Zend OPcache v8.1.2, Copyright (c), by Zend Technologies
7.2. Test PHP for Apache
Now, let’s test PHP for Apache.
Create a new file called info.php
in your document root directory.
sudo nano /var/www/html/info.php
Once nano
editor has opened, enter the following PHP code.
<?php
phpinfo();
Save and exit (press CTRL
+ X
, press Y
and then press ENTER
)
We can now load this file in the browser by going to http://your_ip/info.php
or http://example.com/info.php
.
If you don’t know you IP, you can find out with:
ip addr show eth0 | grep inet | awk '{ print $2; }' | sed 's/\/.*$//'
Below we can see the PHP info page is working correctly.
Once you’ve confirmed PHP is working correctly, it’s important to delete info.php
as it contains information that could be useful to hackers.
sudo rm /var/www/html/info.php
Note: If you plan on uploading files larger than 2MBs through WordPress or similar, you will need to alter the PHP config file and set the max upload size. See: PHP / Apache: set max file upload and post size.
Your LAMP Stack on Ubuntu 22.04 is now complete! If you would like to set up virtual hosts or set up additional configuration for Apache, read on.
8. Configure a Virtual Host (Optional)
If you want to host multiple domains on your Ubuntu 22.04 LAMP server or set up SSL certs with Let’s Encrypt, you will need to set up a virtual host.
If you do not have a domain name, you can instead trick your OS (Linux/Mac/Windows) to resolve a domain to your Ubuntu 22.04 server’s IP address. More on this in section 8.4. below.
8.1. Create Directory and index.html
Create a new directory in /var/www/
for your domain. Just replace mydomain.com with your own.
sudo mkdir -p /var/www/mydomain.com/html
Create an index.html
to show a basic HTML test page.
sudo nano /var/www/mydomain.com/html/index.html
Enter the following:
<html>
<head>
<title>Welcome!</title>
</head>
<body>
<h1>Welcome to my virtual host!</h2>
</body>
</html>
Save and exit (press CTRL
+ X
, press Y
and then press ENTER
)
8.2. Create Virtual Host
Create a virtual host configuration file replacing mydomain.com with your own.
sudo nano /etc/apache2/sites-available/mydomain.com.conf
Enter the following replacing mydomain.com with your own.
<VirtualHost *:80>
ServerAdmin webmaster@mydomain.com
ServerName mydomain.com
ServerAlias www.mydomain.com
DocumentRoot /var/www/mydomain.com/html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Save and exit (press CTRL
+ X
, press Y
and then press ENTER
)
Enable your virtual host replacing mydomain.com with your own.
sudo a2ensite mydomain.com.conf
Disable the default Apache website, otherwise it will override your virtual host.
sudo a2dissite 000-default
Test for errors.
sudo apache2ctl configtest
You can ignore any error that mentions “Could not reliably determine the server’s fully qualified domain name”.
Restart Apache.
sudo systemctl reload apache2
8.3. Test Virtual Host
If you don’t have a registered domain name, you can skip to Step 8.4 below to trick your OS into resolving a domain name of your choice to your Ubuntu 22.04 LAMP server.
You will now need to go to your domain registrar’s DNS settings and point the A record for your domain to the IP of your Ubuntu 22.04 LAMP server. Once the A record has propagated (may take 24-48 hours), you should be able to see your test page in the browser.
If you don’t know you IP, you can find out with:
ip addr show eth0 | grep inet | awk '{ print $2; }' | sed 's/\/.*$//'
To add more domains, simply repeat Step 8 above.
8.4. Edit Hosts File (optional)
If you do not have any domains registered and instead just want to load mydomain.com in your browser as a test, you can edit the hosts file in your OS to point these domains to your Ubuntu 22.04 LAMP server.
To edit the hosts file in Linux or Mac, run sudo nano /etc/hosts
in terminal. In Windows, follow this guide to edit hosts. Once the hosts files is open, enter your Ubuntu 22.04 LAMP server’s IP followed by the domain name you want to resolve.
x.x.x.x mydomain.com
Replace x.x.x.x
with your web server’s IP.
If you don’t know your web server’s IP, you can find out with:
ip addr show eth0 | grep inet | awk '{ print $2; }' | sed 's/\/.*$//'
Once you’ve saved your hosts file, you should be able to access devanswe.rs in your browser.
9. Configure Apache (Optional)
Now that you have Apache serving web pages on your Ubuntu 22.04 LAMP server, there may be some additional configuration settings that will be useful to you.
9.1. Disable Directory Listing and Enable AllowOverride
Apache by default will list contents of your directories without indexes (index.html, index.php). This is a security risk because it could allow hackers to browse your web server looking for scripts.
You will also find that .htaccess
will be ignored by default in Apache. If you need .htaccess, you can enable it by altering the Apache configuration file.
Open the config file.
sudo nano /etc/apache2/apache2.conf
Press CTRL
+ W
and search for <Directory /var/www/>
.
The block should look something like this:
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
Delete Indexes
to stop Apache listing directories and change AllowOverride
to All
to enable .htaccess
. It should now look like this:
<Directory /var/www/>
Options FollowSymLinks
AllowOverride All
Require all granted
</Directory>
Save and exit (press CTRL
+ X
, press Y
and then press ENTER
)
Restart Apache.
sudo systemctl restart apache2
9.2. Enable mod_rewrite
If you want to later configure some rules in .htaccess
, you will most likely need to enable mod_rewrite
.
sudo a2enmod rewrite
Restart Apache.
sudo systemctl restart apache2
What Next?
Now that your Ubuntu 22.04 LAMP web server is up and running, you may want to install phpMyAdmin so you can manage your MySQL server.
To set up a free SSL cert for your domain:
You may want to install an FTP server or configure SFTP for your doc root.
- Installing an FTP Server with vsftpd on Ubuntu 22.04
- How to Configure SFTP for a Web Server Document Root
We also have several other articles relating to the day-to-day management of your Ubuntu 22.04 LAMP server.
- Where are the Apache Error and Access Logs Located?
- How To Back Up Your Web Server Doc Root with Tar and Cron
- How to Back Up MySQL Databases and Automate with Cron
- How to Update Ubuntu 22.04
- What to do when you see *** System Restart Required ***
- Better System Monitoring with htop
Let me know if this helped. Follow me on Twitter, Facebook and YouTube, or 🍊 buy me a smoothie.
Thanks, very concise and helped me get a server running in like 30 minutes. Font choice is questionable.
Many thanks for the comment – glad it helped. Not a fan of OpenSans then? What sort of typeface had you in mind?
SUPERB article. I’ve used this on a few Ubuntu servers now, and it’s just right!
Big thanks <3