Web server on Virtual Machine

In this tutorial, I will show you step by step how to prepare your own web server on a virtual machine. To do this, you’ll need Oracle VM VirtualBox and the Ubuntu Server distribution. At least I work with such a pair of tools and it works well. Before we get down to the business, I would like to point out that in this article you wouldn’t find information on web server security.

I strongly advise you do not to use the knowledge presented here to configure a web server to support websites and applications available on the web. This server is great for testing applications in the development environment.

VM configuration and UBUNTU SERWER instalation

Let’s start with configuring the virtual machine. More about installation Linux Server on Virtual Box you can find in this article: Virtual Linux Server. I assigned her 4 GB of RAM, 4 processor cores, and 10 GB of HDD memory. It has been working great so far. Before installing Linux, you need to set the connection in Virtual Box settings to Bridged network card on our machine in the Settings / Network tab in Oracle VM. I tried configuring with NAT, but there are communication difficulties when it comes to the Apache2 server and so far, I have not found a solution.

Logo ubuntu server on which we build a web server.

From https://ubuntu.com/download/server you can download the latest Ubuntu Server image. It’s worth choosing the LTS (Long-Term Support) version. After downloading the image, install it in a virtual machine and run it. Installation is not complicated and does not require special attention to any of its elements.

After starting the server, it is worth running the following commands:

sudo apt-get update
sudo apt-get upgrade

to update installed packages.

Install Apache 2 and PHP for base web server

After updating the packages, install the apache2 and php server with the command:

sudo apt-get install apache2 php

After installing these packages, you should think about whether you want to transfer via FTP your pages on the server? It is not a necessary element for work. You just have to decide whether we will send files using the shared space by Oracle VM or via FTP. I prefer using FTP and therefore I need to install the FTP package and reconfigure apache2.

Install the FTP package with the command:

apt-get install vsftpd

After installation, you need to make a few changes to the /etc/vsftpd.conf file:

We remove # at the beginning of the lines containing the following code:

write_enable=YES
lacal_umask=022
chroot_local_user=YES
utf8_filesystem=YES

and we add the following lines of code:

allow_writeable_chroot=YES
pasv_min_port=40000
pasv_max_port=40100

save the file and after leaving, reset the ftp server with the command:

sudo systemctl restart vsfpd

From now on, we can use the FTP client by logging into the server’s IP address and entering the user’s login and password. We can extract the server IP with the ifconfig command.

Because when logging in to the ftp server, we get access only to the user’s home directory, it is good to change the apache2 server settings so that you don’t need to copy files with the page on the server. The default location for www files is / var / www but can be changed by editing two files: /etc/apache2/apache2.conf i apache2/sites-enabled/000-default.conf

In file apache2.conf we replace the link to the page in the code line <Directory /var/www> to <Directory /home/username/www/> where username is the username.

In file 000-default.conf we change the path to the directory in the code line DocumentRoot /var/www/ na DocumentRoot /home/username/www

After making the changes, restart the apache server

Service apache2 restart

If everything went correctly after dropping the html file into the home / www directory, the browser should be able to read this file after entering the server’s ip address.

MariaDB installation for SQL services on web server

Theoretically, when installing Ubuntu Server, we don’t have a database installed, but it is worth checking it by entering the MySQL or MariaDB command. If the server reports an error, we can go ahead and install it. If not, we have the database server installed and this step can be skipped.

Mariadb Logo which we installed on the web server

The installation should start from entering the website https://downloads.mariadb.org/mariadb/repositories/ and choose your Linux distro, distribution version, and database version you are interested in. After making a selection at the bottom of the page, we will get a list of commands that must be entered in the terminal on the server to install the database server.

After executing these commands, we are left to install the customer service package. This can be done with the command:

apt install mariadb-client-core-10.1

After installation, we can log in to the server using a command mysql (if logged in as root) or mysql -u root -p if we are on other than count than root.

If the installation was successful, information about the server will be displayed. We leave the server with the exit command.

The next step is to secure the database. For “home” use it’s not a required step, but in my opinion it is still worth setting up at least two accounts (root and user), if only to avoid accidentally breaking the database.

To secure the database, run the command

mysql_secure_installation

and follow the appearing commands:

  1. we can change the password to login to the root account;
  2. remove anonymous users;
  3. disable logging to the database through external connection;
  4. delete the test database;
  5. reload the permission table.

Done, now you can get to the database from the root account with the mysql command or from another account by entering the command:

mysql -u root -p

If you want to run phpmyAdmin, i.e. graphical database support, you need to install additional packages.

apt-get install phpmyadmin php-mbstring php-gettext

during installation we will get a question about the web server, choose apache2. When asked about the phpmyadmin package configuration, we answer no.

After the installation is complete, we still need to update the apache2 server configuration:

open file /etc/apache2/apache2.conf and write in he end of file:

Include /etc/phpmyadmin/apache.conf

save the file and restart the apache2 server after exiting to the shell

systemctl restart apache2

Now we can get to the phpmyadmin panel from the linklocalhost/phpmyadmin

The login and password are the same as for MariaDB

After completing the entire path, you have a set up environment for testing web applications. My congratulations 😊

Krzysztof NyrekWeb server on Virtual Machine