Back-end Server Inside Virtual Machine
A Tutorial on Setting Up Back-end Server In Virtual Machine
Want some music while reading? 🎵📻
The Story Behind This Tutorial
The inspiration to write this tutorial comes from a project work for a Software Project course at the university. In this course, we worked on a full-stack project that required the back-end to be setup inside of a virtual machine. The virtual machine was created using CSC Cloud. I took the responsibility of creating and maintaining the back-end server for the team. This taught me how to utilize my Linux, bash scripting and DBMS skills. I learned a lot after this, therefore, I think it’s great to write a tutorial now.
Introduction
In this tutorial, we will see how to set up a remote back-end server and use MariaDB inside the virtual machine for the server. The result will be a working remote back-end that is always up and running which is quite the same as a back-end which that is deployed to Heroku.
This tutorial assumes that you already have a virtual machine up and running.
SSH Tunneling
SSH tunneling could also be called SSH port forwarding which is a way of transporting data over an encrypted SSH connection. This methods allow you to connect a local port to a remote port securely.
There are tools that help you with this purpose such as Putty, SolarWinds Solar-PuTTY, etc. However, you could also do this with Git Bash only using ssh command. Please note that the following command is tested in Windows not MacOS, the command might need to be changed a little bit to work with MacOS.
In Git Bash, execute this code:
ssh <vm-username>@<vm-ip-address> -L <port-number>:localhost:3306
You might have a question here why 3306 but not another port. The answer is that we’re going to use MariaDB and MySQL in this case, and MySQL default port is 3306.
After using this command, if you use Windows, your VM is working and you type in the correct username and IP address, you should now be inside of the VM.
When the VM is created the first time, there is a high chance that MariaDB isn’t installed in there. There is also a high possibility that the Node used inside the VM version is lower than 12.x.x. So we should install both MariaDB and upgrade Node.
Upgrade Node
Just to make sure that the Node is upgraded, we should first uninstall it from the system. The commands for this are below:
sudo apt-get purge nodejs
curl -sL https://deb.nodesource.com/setup_12.x | sudo -E bash -
sudo apt-get install -y nodejs
The first line is for uninstalling NodeJS. The second line fetches NodeJS version 12.x.x from the server. You could, of course, install the latest version here but I choose 12.x.x because it’s the lowest version that allows us to run the back-end without babel. And then the third line is for installing the NodeJS into your machine.
MariaDB Database
Install MariaDB
To install MariaDB in Linux, we simple run:
sudo apt update
sudo apt install mariadb-server
Create MariaDB User
By default, we have a root user but we shouldn’t use this user to interact with our database because of security issues. We should first create a user to interact with the DB later. To create a new user, we first run:
sudo mariadb
This allows us to use MariaDB shell. Then after we’re inside, execute:
CREATE USER 'admin'@'localhost' IDENTIFIED BY 'password';
This command creates a new user with username admin and the password is password. Of course, when you create the user for yourself, please change the username and password accordingly. After that, we should grant privileges to the new user. Otherwise, this user can’t interact with the DB.
If you want your user to be a superuser that can use any DB and do everything he/she wants, we can use the below command:
GRANT ALL PRIVILEGES ON *.* TO 'admin'@'localhost' IDENTIFIED BY 'password' WITH GRANT OPTION;
But if you want the user have only the power over a specific DB, run this command instead:
GRANT ALL PRIVILEGES ON 'newdatabase'.* TO 'admin'@'localhost';
newdatabase is your database’s name. After granting privileges, we should run this one next:
FLUSH PRIVILEGES;
Now, you could check the newly added privileges by running this command:
SHOW GRANTS FOR 'admin'@'localhost';
After this, you could see the grant you just added for the admin user. And our database is ready to be used now.
Start the Services
Just to make sure that our MariaDB and MySQL services are running, we should run the following commands:
sudo systemctl start mariadb.service
sudo service mysql start
You could try logging in using the new user
mysql admin -u admin -p
This command will prompt for password and you could type the password to login.
Database Configuration
This upcoming part assumes that your back-end is coded in NodeJS. Our project used Knex to help with database querying. Knex requires a database configuration object, here is a part of it:
|
|
You could see here that we either use the environment variables or specific variables for each value. The main values that we want to focus on are:
- host
- user
- password
- database
In our case, the host will remain the same which is localhost because we will clone the back-end and use it inside the VM.
You could choose whichever free port that is available to use except 3306 because MySQL is already using that one. In the example, we use 3308 and because of this, when we use the ssh command above, the
The user in our case is admin because we just created it above, the password is password and the database is newdatabase.
Starting The Project
So we will now moving on to the step where we start our back-end server and keep it always working. If you’re inside MariaDB, type exit to get out of the shell. You should be now back in the VM’s terminal.
First, if you have a repository in Github, you could clone it here. Otherwise, you could use software like WinSCP to copy files from your local machine to the VM as well. If you project uses environment variables, you should re-create the .env file here.
Next, just to make sure that mysql service is running we should again execute the command:
sudo service mysql start
There are many ways to run a back-end server in the background but I choose to use screen because it’s simple and not complicated. We can start by running:
screen
cd project
npm run start
The first command starts a new screen. After that, we cd into our project folder and start the server. If your server doesn’t throw any error, you have successfully started the server inside the VM. You could now close the terminal and test your server using the URL
http://<your_VM_IP_address>/api/...
If everything is done correctly, the URL should work and you have a working back-end that runs always.
Later on, if you want to come back to that screen and check your server, you should first use the ssh command and then type:
screen -ls
screen -r <screen-id>
Extras
There will be situations where MySQL stops working. It could be because of many reasons such as out of memory, etc. There are many ways to prevent or check if this happens and restarts the service. For my case, I created a bash script to automatically restart the server every time it stops and then add a cron job for this to work on schedule so that I don’t have to manually restart it myself. I leave the details for the audience to explore!
Conclusion
This practice helps me build up my Linux and scripting skills a lot. Before this, I thought the only way to have a remote server up and running is using Heroku but this was way cooler! 😁. I hope you could successfully set up the back-end after following this tutorial. Thanks for reading and see you again in another post!