How to Install LAMP Stack on Ubuntu VPS

This knowledgebase article is going to show you how to install LAMP stack on Ubuntu 20.04 LTS. A software stack is a set 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 powers dynamic websites and web applications. Linux is the operating system; Apache is the web server; MariaDB/MySQL is the database server and PHP is the server-side scripting language responsible for generating dynamic web pages.

Step 1: Update Software Packages

Before we install the LAMP stack, it’s a good idea to update the repository and software packages. Run the following commands on your Ubuntu 20.04 OS.

sudo apt update

sudo apt upgrade

Step 2: Install Apache Web Server

Enter the following command to install Apache Web server. The apache2-utils package will install some useful utilities like Apache HTTP server benchmarking tool (ab).

sudo apt install -y apache2 apache2-utils

After it’s installed, Apache should be automatically started. Check its status with systemctl.

systemctl status apache2

Output:

● apache2.service - The Apache HTTP Server
Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
Active: active (running) since Sat 2020-04-11 11:31:31 CST; 2s ago
Docs: https://httpd.apache.org/docs/2.4/
Process: 53003 ExecStart=/usr/sbin/apachectl start (code=exited, status=0/SUCCESS)
Main PID: 53011 (apache2)
Tasks: 55 (limit: 19072)
Memory: 6.4M
CGroup: /system.slice/apache2.service
├─53011 /usr/sbin/apache2 -k start
├─53012 /usr/sbin/apache2 -k start
└─53013 /usr/sbin/apache2 -k start

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

sudo systemctl start apache2

It’s also a good idea to enable Apache to automatically start at system boot time.

sudo systemctl enable apache2

Check Apache version:

apache2 -v

Output:

Server version: Apache/2.4.41 (Ubuntu)
Server built: 2020-03-05T18:51:00

If you are using iptables firewall, then you need to run the following command to open TCP port 80.

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

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

sudo ufw allow http

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

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

By default, Apache uses the system hostname as its global ServerName. If the system hostname can’t be resolved in DNS, then you will probably see the following error after running sudo apache2ctl -t command.

AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1. Set the 'ServerName' directive globally to suppress this message

To solve this problem, we can set a global ServerName in Apache. Use the Nano command-line text editor to create a new configuration file.

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

Add the following line in this file.

ServerName localhost

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

sudo a2enconf servername.conf

Reload Apache for the change to take effect.

sudo systemctl reload apache2

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

Step 3: Install MariaDB Database Server

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

sudo apt install mariadb-server mariadb-client

After it’s installed, MariaDB server should be automatically started. Use systemctl to check its status.

systemctl status mariadb

Output:

● mariadb.service - MariaDB 10.3.22 database server
Loaded: loaded (/lib/systemd/system/mariadb.service; enabled; vendor preset: enabled)
Active: active (running) since Fri 2020-04-10 14:19:16 UTC; 18s ago
Docs: man:mysqld(8)
https://mariadb.com/kb/en/library/systemd/
Main PID: 9161 (mysqld)
Status: "Taking your SQL requests now..."
Tasks: 31 (limit: 9451)
Memory: 64.7M
CGroup: /system.slice/mariadb.service
└─9161 /usr/sbin/mysqld

If it’s not running, start it with this command:

sudo systemctl start mariadb

To enable MariaDB to automatically start at boot time, run

sudo systemctl enable mariadb

Now run the post-installation security script.

sudo mysql_secure_installation

When it asks you to enter MariaDB root password, press Enter key as the root password isn’t set yet. Then enter to set the root password for MariaDB server.

<image1>

Next, you can press Enter to answer all remaining questions, which will remove anonymous user, disable remote root login and remove test database. This step is a basic requirement for MariaDB database security. (Notice that Y is capitalized, which means it is the default answer. )

<image2>

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

sudo mariadb -u root

To exit, run

exit;

Check MariaDB server version information.

mariadb --version

As you can see, we have installed MariaDB 10.3.22.

mariadb Ver 15.1 Distrib 10.3.22-MariaDB, for debian-linux-gnu (x86_64) using readline 5.2

Step 4: Install PHP7.4 or PHP8.1

Installing PHP7.4

Enter the following command to install PHP7.4 and some common PHP modules.

sudo apt install php7.4 php-pear php7.4-curl php7.4-dev php7.4-gd php7.4-mbstring php7.4-zip php7.4-mysql php7.4-xml php7.4-fpm libapache2-mod-php7.4 php7.4-imagick php7.4-tidy php7.4-xmlrpc php7.4-intl php7.4-mysql php-common php7.4-cli php7.4-common php7.4-json php7.4-opcache php7.4-readline php7.4-bcmath php7.4-mbstring php7.4-xml php7.4-http php7.4-curl php7.4-zip php7.4-gd

Enable the Apache php7.4 module then restart Apache Web server.

sudo a2enmod php7.4

sudo systemctl restart apache2

Installing PHP8.1

Enter the following command to install PHP8.1 and some common PHP modules.

sudo apt install php-pear php8.1-curl php8.1-dev php8.1-gd php8.1-mbstring php8.1-zip php8.1-mysql php8.1-xml php8.1-fpm libapache2-mod-php8.1 php8.1-imagick php8.1-tidy php8.1-xmlrpc php8.1-intl php8.1-mysql php-common php8.1-cli php8.1-common php8.1-opcache php8.1-readline php8.1-bcmath php8.1-mbstring php8.1-xml php8.1-http php8.1-curl php8.1-zip php8.1-gd

Enable the Apache php7.4 module then restart Apache Web server.

sudo a2enmod php8.1

sudo systemctl restart apache2

Congratulations! You have successfully installed LAMP stack (Apache, MariaDB and PHP7.4) on Ubuntu.

  • 1 gebruikers vonden dit artikel nuttig
Was dit antwoord nuttig?

Gerelateerde artikelen

How To Install LAMP Stack on CentOS VPS

This Knowledgebase article details steps on how to install LAMP stack on CentOS & RHEL OS...

How to Install LEMP Stack on Ubuntu VPS

This knowledgebase article details how to install LEMP stack (Nginx, MariaDB, and PHP7.4) on...

Install WordPress on Ubuntu with LAMP

This knowledgebase will detail how to install WordPress on Ubuntu flavours with Apache, MariaDB...

How to Set up WordPress Multisite on Ubuntu Server using LAMP

This knowledgebase will show you how to set up WordPress Multisite with Apache Web server. It’s...

How to Run PHP-FPM with Apache in Ubuntu

There are basically two ways to run PHP code with Apache web server: Apache PHP module...