Original post from devanswers.co,
In this guide we will install a LAMP Stack (Apache, MySQL, PHP) on Ubuntu Server 20.04 / 20.10 and configure a web server.
Prerequisites
You should use a non-root user account with sudo privileges. Please see the Initial server setup for Ubuntu 20.04 / 20.10 guide for more details.
1. Install Apache
The first component of your LAMP Stack on Ubuntu 20.04 / 20.10 is Apache.
Begin by updating the package lists and installing Apache. Press y
and ENTER
when asked to continue.
sudo apt update && sudo apt install apache2
Installation may take a few minutes. Once installed, continue to Step 2 to configure the firewall on Ubuntu 20.04/ 20.10.
2. Configure Firewall
It is highly recommended that you configure a firewall to provide added security to your LAMP Stack on Ubuntu 20.04 / 20.10.
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. You may have already done this in the our Initial server setup for Ubuntu 20.04 / 20.10 guide, but it’s no harm adding it again just in case. 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
Status: active
To Action From
-- ------ ----
OpenSSH ALLOW Anywhere
Apache Full ALLOW Anywhere
OpenSSH (v6) ALLOW Anywhere (v6)
Apache Full (v6) ALLOW Anywhere (v6)
Above we can see the firewall is active and has two rules per service. v6
is short for IPv6.
3. Test Apache
To see if Apache installed correctly on Ubuntu 20.04 / 20.10, 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)
Drop-In: /lib/systemd/system/apache2.service.d
└─apache2-systemd.conf
Active: active (running) since Sat 2020-03-31 08:44:04 CEST; 15min ago
Main PID: 5727 (apache2)
Tasks: 55 (limit: 4915)
CGroup: /system.slice/apache2.service
├─5727 /usr/sbin/apache2 -k start
├─5728 /usr/sbin/apache2 -k start
└─5729 /usr/sbin/apache2 -k start
Mar 31 08:44:04 ubuntu2004 systemd[1]: Starting The Apache HTTP Server...
Mar 31 08:44:04 ubuntu2004 apachectl[5675]: AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1.
Mar 31 08:44:04 ubuntu2004 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 eth0 | 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 20.04 / 20.10 is MySQL.
Begin by updating the repositories and installing the MySQL package.
Press y
and ENTER
if prompted to install.
sudo apt update && sudo apt install mysql-server
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 Mon 2020-04-02 02:40:59 CEST; 2min 47s ago
Main PID: 18476 (mysqld)
Status: "Server is operational"
Tasks: 27 (limit: 4915)
CGroup: /system.slice/mysql.service
└─18476 /usr/sbin/mysqld
Apr 02 02:40:59 ubuntu2004 systemd[1]: Starting MySQL Community Server...
Apr 02 02:40:59 ubuntu2004 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 20.04 / 20.10.
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).
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. Disable Remote Root Login
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
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
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 20.04 / 20.10 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 15
Server version: 8.0.19-0ubuntu5 (Ubuntu)
Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
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>
To exit MySQL, type exit and press ENTER
.
exit
You have now successfully installed and configured MySQL for your LAMP Stack on Ubuntu 20.04 / 20.10!
6. Install PHP
The next and final component of your LAMP Stack on Ubuntu 20.04 / 20.10 is PHP.
Begin by updating the repository and 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.
Press y
and ENTER
when prompted to install the PHP package.
sudo apt update && sudo apt install php libapache2-mod-php php-mysql
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 7.4.3 (cli) (built: Mar 26 2020 20:24:23) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
with Zend OPcache v7.4.3, Copyright (c), by Zend Technologies
Great!
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 20.04 / 20.10 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 20.04 / 20.10 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 20.04 / 20.10 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/public_html
Create an index.html
file for testing.
sudo nano /var/www/mydomain.com/public_html/index.html
Enter the following:
<html>
<head>
<title>Welcome!</title>
</head>
<body>
<h1>Welcome to mydomain.com!</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/public_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
)
Test for errors.
apachectl configtest
You can ignore any error that mentions “Could not reliably determine the server’s fully qualified domain name”.
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
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 20.04 / 20.10 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 20.04 / 20.10 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.
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 20.04 / 20.10 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 20.04 / 20.10 LAMP server’s IP followed by the domain name you want to resolve.
x.x.x.x mytest1.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 mydomain.com in your browser.
9. Configure Apache (Optional)
Now that you have Apache serving web pages on your Ubuntu 20.04 / 20.10 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 20.04 / 20.10 LAMP web server is up and running, you may want to install phpMyAdmin so you can manage your MySQL server.
- Installing phpMyAdmin for Apache on Ubuntu 20.04 / 20.10
To set up a free SSL cert for your domain:
- Installing a Let’s Encrypt SSL Cert for Apache on Ubuntu 20.04 / 20.10
You may want to install an FTP server or configure SFTP for your doc root.
- Installing an FTP Server with vsftpd on Ubuntu 20.04 / 20.10
- How to Configure SFTP for a Web Server Document Root