This writeup will show how to set up apache web server and deploy nodejs applicaion using PM2 process manger. #
The project is a simple applicaion created using React for frontend and nodejs for the backend using mysql database engine.
Login to the VPS #
We will use SSH to access the VPS. To login access your terminal and type ssh root@vpsip
ssh [email protected]
You will be promted to accept the ssh fingerprint if you are connectin for the fist time, type yes and press enter.
Next you will need to provide the root password to complete the login.
Once logged in we can update our VPS to get it ready for the installations
yum -y update
Installations #
Installing apache web server #
yum install httpd
Installing NVM for node installation #
To download NVM script for installation run the below command then source .bash_profile
curl https://raw.githubusercontent.com/creationix/nvm/v0.13.1/install.sh | bash
source ~/.bash_profile
#### Installing node
We can now install node using nvm, run the command below to install latest node version
nvm install --lts
Installing PM2 #
With node installed we can install pm2 for managing the node processes
npm install pm2@latest -g
Installing mysql #
wget https://dev.mysql.com/get/mysql80-community-release-el7-1.noarch.rpm
rpm -ivh mysql80-community-release-el7-1.noarch.rpm
yum install mysql-server
Installing git #
We will need to get the files on server, using git we will clone code from our repository.
yum install git
Setup #
Apache setup #
We will set up the virtual env where our site is to be uploaded. We need to set up our virtual host file, we will create inside Virtual Host Directive using editor of choice.
cd /etc/httpd/conf.d
touch yourdomain.com.conf
vim yourdomain.com.conf
you will add the content below to the .conf file
<VirtualHost *:80>
ServerName yourdomain.com
ServerAlias www.yourdomain.com
DocumentRoot /var/www/yourdomain/html
DirectoryIndex index.html
ErrorLog logs/yourdomain.com-error_log
CustomLog logs/yourdomain.com-acess_log common
<Location /api>
ProxyPass http://localhost:4000
ProxyPassReverse http://localhost:4000
</Location>
</VirtualHost>
- *:80 : site will be accessible from any IP
- ServerName: Used by apache to determine the virtual host block to use matching the http header of packet request
- ServerAlias: Any other domain that can be used to match virtual host block
- DocumentRoot: The directory you want to serve whenever client request yourdomain.com
- DirectoryIndex: The main file that apache should use in the directory servered.
- ErrorLog, CustomLog: Tells apache where the error logs should go.
- Location: used to tell apache to server a different content when a request is made to yourdomain.com/api
- ProxyPass, ProxyPassReverse: Enables proxying capabilities in apache
We can test our Apache configuration first before we restart it to make the changes take effect
httpd -t
systemctl restart httpd
We will need to create the specified DocumentRoot and the DirectoryIndex,the index will be created from build command.
mkdir /var/www/yourdomain/html
Mysql configuration and Database setup #
We will start msql and create the database and tables that we will use
Securing Mysql. #
We need to change the default password to one that we can use to make connection to the database.Start the sql service and get the temporary password generated during installation using the commands below respectively. Take note of the password
systemctl start mysqld
sudo grep ‘temporary password’ /var/log/mysqld.log
using the script below to change the default less secure options that were set during installation such as removing annonymous users, disabling remote login. Reply with Y for the prompts. Using the temporary password recovered earlier we will upadte the password to a new one.
sudo mysql_secure_installation
Once the script is done we can login using our updated password and create out database and the tables.
To login to mysql run the command below.
mysql -u root -p
Create the database and table(S) you will use. Here the table created was named posts.
mysql> CREATE DATABASE yourdbname;
mysql> USE DATABASE yourdbname;
mysql> CREATE TABLE youtablename;
mysql> CREATE TABLE `posts` ( `id` int NOT NULL AUTO_INCREMENT, `title` varchar(105) NOT NULL, `body` text NOT NULL, `created_at` date NOT NULL, PRIMARY KEY (`id`), UNIQUE KEY `id_UNIQUE` (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=5
DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
mysql> show tables;
mysql> INSERT INTO `blog`.`posts` (`title`, `body`, `created_at`) VALUES ("First post" , "Body of First post", "2022-10-2");
mysql> select * from posts ;
mysql> exit ;
Add website files and configure #
We will use git to clone our repository. We will clone the site in the DocumentRoot that we defined in our VirtualHost configuration.
cd /var/www/yourdomain/html
git clone https://github.com/lischy/cPanel.git .
Once we have the files , we can now edit our .ENV file, install dependencies and create our build for static assests
We will change directory to setup our backend first and install our dependencies
cd server
npm install
we will edit our .env to adjust our variables, you will need to add the following environment variables to your .env file
NODE_ENV= development
PORT = 400
DB_HOST ='localhost'
DB_USER ='root'
DB_NAME ='blog'
DB_PASSWORD='yourdatabasepasswordset'
We will now start our node process for the backend/api using pm2 to ensure the process is always running.
pm2 start --name api app.js
For more infomation on pm2 please check Documentation
you can now check if the api will give any response. visit yourdomain.com/api to test
Since the build for the fronted was done locally you can move the files to the document root using the following command
cd ../dist
cp -r dist/* /var/www/yourdomain/html/
If you have not done build locally you can follow the following steps to do the build then move the files to the DocumentRoot specified for your virtual host
cd yourfrontendfolder
npm install
npm run build
cp -r yourfrontendfolder/* /var/www/yourdomain/html/
At this point you can test your deployment, with the non https version before proceeding to enable https
Enabling HTTPS #
To install ssl and enable HTTPS please reference the ssl guide here. Take note that the document root will differ and for the updates that you will need to do on your virtual host file configuration.