If you want to set up a web server and look for some tips, this article is for you. Here are instructions for setting up a web server (LAMP = Linux + Apache + MySql + PHP) on OS Ubuntu 20.04, in connection with:

Apache 2
Mysql Sever 8

To start, go to the CLI (command-line user interface). You can open it with Ctrl + Alt + T combination. If you don't want to set up a local webserver, then you need to connect to your server via SSH.

Once you are on a proper instance in the terminal enter super user mode (root or sudo user). Run the following command:

sudo su

and enter the password to your account, if required.

Take the following steps to install the web server:

1. Update the available package information. Run this command:

sudo apt update

* In the examples, the commands that must be executed by the superuser beginning with the word sudo. You do not have to use sudo while in superuser mode.

2. Install the Apache 2 webserver.

sudo apt install apache2 gedit

3. Install the MySQL database server.

sudo apt install mysql-server

After the MySQL database server was installed, run the following command:

mysql -uroot

Also, enter these commands, one by one, to create a new user: 

CREATE USER 'phpmyadmin'@'localhost' IDENTIFIED BY '<New-Password-Here>';
GRANT ALL PRIVILEGES ON *.* TO 'phpmyadmin'@'localhost' WITH GRANT OPTION;

4. Install php8.2 and all necessary PHP libraries to work with Apache 2 and modern platforms, like Magento 2.

sudo add-apt-repository ppa:ondrej/php
sudo apt-get update

sudo apt install php8.2 libapache2-mod-php8.2 php8.2-curl php8.2-intl php8.2-zip php8.2-soap php8.2-xml php8.2-gd php8.2-mbstring php8.2-bcmath php8.2-common php8.2-xml php8.2-mysqli

5. Enable the apache mods to work with php8, rewrite, and restart the webserver.

sudo a2enmod php8.2
sudo a2enmod rewrite
sudo service apache2 restart

Follow the link and check if your local web server is working: (alternative http://localhost/). If everything is correct, the following page will open:

Перевірки роботи mysql сервера

6. Change the user based on which the webserver will run.

For this edit the following file /etc/apache2/envvars.

sudo gedit /etc/apache2/envvars 

find these strings there:

export APACHE_RUN_USER=www-data
export APACHE_RUN_GROUP=www-data

and replace them with:

export APACHE_RUN_USER=your_user
export APACHE_RUN_GROUP=your_group

where your_user — the name of your Ubuntu user (the one you want to edit files with), and your_group — group this user belongs to. Usually, the names of the user and the group are the same.

Save the file and reload the webserver.

sudo service apache2 restart

7. Change the owner of the web directory.

A web directory is a folder where your web application, such as Magento 2, is located. The goal is to give your user complete control over that folder. Run the following command:

chown -R your_user:your_group /var/www

8. Increase the memory limit (RAM) that is allowed for PHP.

By default, this value is 128 MB, which is insufficient to work e.g. with Magento 2.

Open the /etc/php/8.2/apache2/php.ini file for editing:

sudo gedit /etc/php/8.2/apache2/php.ini

find the following string

memory_limit = 128M

change it to

memory_limit = 1024M

save changes and reload the webserver once again.

sudo service apache2 restart

9. Change the virtual host settings.

Edit this file

sudo gedit /etc/apache2/sites-available/000-default.conf

and add the following code to the middle of the VirtualHost node:

<Directory /var/www/html>
Options Indexes FollowSymLinks MultiViews
AllowOverride all
Require all granted

Save changes and reload the web server

sudo service apache2 restart

10. Install MySQL

To manage MySQL databases, for example, to create new databases we recommend to install phpMyAdmin.