Image

(Last change: 23.03.2023)
This guide shows you how to install the LAMP stack on Ubuntu 20.04 LTS. A software stack is a collection of software tools bundled together. LAMP stands for Linux, Apache, MariaDB/MySQL and PHP, all of which are open source and free to use. It is the most common software stack that supports dynamic websites and web applications. Linux is an operating system; Apache is a web server; MariaDB/MySQL is a database server and PHP is a server-side scripting language responsible for creating dynamic web pages. In this guide, we are installing as root, if you have a normal one, use the sudo command.


The preconfigured server configuration is available on the order form.

1. Update software packages

Before we install the LAMP stack, it is recommended to update the repository and software packages. Run the following commands on Ubuntu 20.04.

apt update
apt upgrade

2. Install Apache Web Server

Enter the following command to install the Apache web server. The apache2-utils package will install some useful utilities such as the HTTP server testing tool Apache (ab).

apt install -y apache2 apache2-utils


Once installed, Apache should automatically start. Check its status with systemctl.

systemctl status apache2

If it's not running, use systemctl to start it.

systemctl start apache2

It is also recommended to enable Apache to start automatically at system boot time.

systemctl enable apache2

Now enter the public IP address of your Ubuntu 20.04 server in your browser's address bar. You should see the inscription "It works!" Web page, which means that the Apache web server is working properly. If you are installing LAMP on your local Ubuntu 20.04 machine, enter 127.0.0.1 or localhost in your browser's address bar.

If the connection is refused or cannot be established, the firewall may be blocking incoming requests on TCP port 80. If you are using the iptables firewall, you need to run the following command to open TCP port 80.

iptables -I INPUT -p tcp --dport 80 -j ACCEPT

If you are using a UFW firewall, run this command to open TCP port 80.

ufw allow http

Now we need to set www-data (the Apache user) as the owner of the document root (otherwise known as the web root). By default, it is owned by the root user.

chown www-data:www-data /var/www/html/ -R

By default, Apache uses the system hostname as its global server name. If the system hostname cannot be resolved in DNS, you will probably see the following error after running the command #apache2ctl -t.

AH00558: apache2: could not reliably resolve the FQDN of the server using 127.0.0.1. Set the 'ServerName' directive globally to suppress this message.


To solve this problem, we can set the global server name in Apache. Use the Nano command line text editor to create a new configuration file. Add the following line to this file:

nano /etc/apache2/conf-available/servername.conf

ServerName localhost

Save and close the file. To save the file in the Nano text editor, press Ctrl+O, then press Enter to confirm. To exit, press Ctrl+X. Then include this configuration file.

a2enconf servername.conf

Restart Apache for the changes to take effect.

systemctl restart apache2

Now if you run the #apache2ctl -t command again, you won't see the above error message.

3. Install the MariaDB database server

MariaDB is a replacement for MySQL. It is developed by former members of the MySQL team who are concerned that Oracle might turn MySQL into a closed source product. Enter the following command to install MariaDB on Ubuntu 20.04.

apt install mariadb-server mariadb-client

After installation, the MariaDB server should automatically start. Use systemctl to check its status.

systemctl status mariadb

If it is not running, start it with this command:

systemctl start mariadb

To allow MariaDB to start automatically at boot time, run

systemctl enable mariadb

Now run the security script after installation.

mysql_secure_installation

When you are asked to enter the MariaDB root password, press the Enter key as the root password has not yet been set. Then type y to set the root password for the MariaDB server.

You can then press Enter to answer any remaining questions, which will remove the anonymous user, disable remote root login, and delete the test database. This step is the main requirement to secure the MariaDB database. (Note that Y is capitalized, which means this is the default answer.)

By default, the MariaDB package on Ubuntu uses unix_socket for user authentication, which basically means you can use the OS username and password to login to the MariaDB console. So you can run the following command to login without providing the MariaDB root password.

mariadb -u root

Run to exit

exit;

4. Install PHP7.4

At the time of this writing, PHP7.4 is the latest stable version of PHP and has a marginal performance advantage over PHP7.3. Enter the following command to install PHP7.4 and some common PHP modules.

apt install php7.4 libapache2-mod-php7.4 php7.4-mysql php-common php7.4-cli php7.4-common php7.4-json php7.4-opcache php7.4-readline

Enable the Apache php7.4 module, then restart the Apache web server.

a2enmod php7.4
systemctl restart apache2

To test PHP scripts with the Apache server, we need to create an info.php file in the document's root directory.

nano /var/www/html/info.php

<?php phpinfo(); ?>


To save the file in the Nano text editor, press Ctrl+O, then press Enter to confirm. To exit, press Ctrl+X. Now in the browser address bar enter server-ip-address/info.php. Replace server-ip-address with your actual IP address. If you are following this guide on your local machine, enter 127.0.0.1/info.php or localhost/info.php.

You should see your server's PHP information. This means that PHP scripts can work properly with the Apache web server.

There are two main ways to run PHP code on the Apache web server:

  • Apache PHP Module
  • PHP-FPM

In the steps above, the Apache PHP7.4 module is used to process PHP code, which is usually fine. But in some cases, you need to run PHP code using PHP-FPM instead. Here's how to do it:

Disable the Apache PHP7.4 module.

a2dismod php7.4

Install PHP-FPM.

apt install php7.4-fpm

Enable the proxy_fcgi and setenvif module.

a2enmod proxy_fcgi setenvif

Include the config file /etc/apache2/conf-available/php7.4-fpm.conf

a2enconf php7.4-fpm

Restart Apache for the changes to take effect.

systemctl restart apache2

Now, if you refresh the info.php page in your browser, you will find that the server API has changed from Apache 2.0 handler to FPM/FastCGI, which means Apache web server will pass PHP requests to PHP-FPM.

LAMP (Apache, MariaDB and PHP7.4) installed successfully on Ubuntu 20.04. All these steps have already been completed in the LAMP Ubuntu 20.04 preinstalled configuration for VPS servers in the order form.




No Comments Yet