Mirza's Documentation SiteMirza's Documentation Site
Home
Deploy on Virtual Machine
SSL Certificate Assignment
Install Metrics Management
Home
Deploy on Virtual Machine
SSL Certificate Assignment
Install Metrics Management
  • Deploy on Virtual Machine

    • Deploy Laravel
    • Deploy Wordpress MySQL and NGINX
    • Deploy NodeJS using Node Version Manager (NVM)
      • Static File
      • Upstream Port
    • Deploy Golang using goenv

Deploy on Virtual Machine

How to deploy some tech stack in virtual machine

Deploy Laravel

  1. Get in into root user
sudo su
  1. Update the ubuntu repository
sudo apt update -y
  1. Install NGINX Proxy
sudo apt install nginx -y
  1. Add Ondrej PPA Repository
sudo add-apt-repository ppa:ondrej/php
  1. Do the repository update again
sudo apt update
  1. Install PHP8.2
sudo apt install php8.2 -y
  1. Install PHP module
sudo apt install php8.2-{fpm,imap,ldap,xml,curl,mbstring,zip,mongodb,gd,dev}
  1. Remove Apache2 from the systemctl (because it will conflicted with NGINX)
sudo systemctl stop apache2
sudo systemctl disable apache2
  1. Install Composer
wget -O composer-setup.php https://getcomposer.org/installer
sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer
  1. Clone the repository in /var/www folder
cd /var/www
git clone https://gitlab.com/user/laravel-repo.git
  1. Install the vendor package
composer install
  1. Copy the .env.example into .env file and fill the .env file
cp .env.example .env
  1. Do the artisan things
php artisan jwt:secret
php artisan key:generate
  1. Seed database
php artisan db:seed
  1. Create file inside /etc/nginx/sites-available
vim /etc/nginx/sites-available/laravel-repo

Fill it with this value:

server {
	listen 80;
	index index.php index.html index.htm;
	error_log  /var/log/nginx/backend-error.log;
	access_log /var/log/nginx/backend-access.log;
	root /var/www/laravel-repo/public;

	location / {
    	try_files $uri $uri/ /index.php?$query_string;
    	gzip_static on;
	}

	location ~ \.php$ {
    	include snippets/fastcgi-php.conf;
    	fastcgi_pass unix:/var/run/php/php8.2-fpm.sock;
    	fastcgi_split_path_info ^(.+\.php)(/.+)$;
    	include fastcgi_params;
    	fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    	fastcgi_param PATH_INFO $fastcgi_path_info;
	}

	location ~ /.well-known {
    	allow all;
	}

	location = /.env {
    	deny all;
    	return 404;
	}
}
  1. Symlink it into /etc/nginx/sites-enabled
ln -s /etc/nginx/sites-available/laravel-repo /etc/nginx/sites-enabled/laravel-repo
  1. Unlink default file inside sites-enabled folder
unlink /etc/nginx/sites-enabled/default
  1. Test the nginx configuration
nginx -t
  1. Reload the nginx
nginx -s reload

Deploy Wordpress MySQL and NGINX

  1. Install common software
sudo apt-get update
sudo apt -y install software-properties-common
  1. Install NGINX
sudo apt install nginx -y
  1. Add PHP Repository
sudo add-apt-repository ppa:ondrej/php
  1. Install PHP and basic extension
sudo apt install php8.2 php8.2-fpm php8.2-common php8.2-gmp php8.2-curl php8.2-intl php8.2-mbstring php8.2-xmlrpc php8.2-gd php8.2-xml php8.2-cli php8.2-zip php8.2-mysql
  1. Purge Apache Web Server
sudo apt purge apache2
  1. Download and unzip wordpress latest
wget https://wordpress.org/latest.tar.gz
tar -xvzf latest.tar.gz
  1. Move to NGINX folder
sudo mv wordpress /var/www && cd /var/www/<folder-name>
  1. Install MySQL or MariaDB
sudo apt install mariadb-server # MariaDB
sudo apt install mysql-server # MySQL
  1. Secure install MySQL
mysql_secure_installation
  1. Create wordpress database
mysql -u root
CREATE DATABASE <db-name>;
CREATE USER '<user-name>'@'localhost' IDENTIFIED BY '<password>';
GRANT ALL ON <db-name>.* TO '<user-name>'@'localhost' WITH GRANT OPTION;
FLUSH PRIVILEGES;
EXIT;
  1. Configure wp-config.php
sudo cp wp-config-sample.php wp-config.php
sudo vim wp-config.php
  1. Change owner wordpress folder
sudo chown -R www-data:www-data /var/www/<folder-name>
  1. Change mode wordpress folder
sudo find . -type d -exec chmod 755 {} \;
sudo find . -type f -exec chmod 644 {} \;

Deploy NodeJS using Node Version Manager (NVM)

  1. Curl installation shell from github
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
  1. Source the changed data in bash
source ~/.bashrc
  1. Check node version in terminal
nvm list-remote
  1. Install required node version
nvm install <node_version>

For example:

nvm install v16.13.0
  1. Check your node inside terminal
node -v
  1. Install package manager

If you use pnpm:

npm i -g pnpm

If you use yarn:

npm i -g yarn
  1. Install NGINX Proxy
sudo apt install nginx -y
  1. Use SuperUser to access root folder
sudo su
  1. Go to destination folder for your repository, and clone it
cd /var/www
git clone https://gitlab.com/user/node-repo.git
cd node-repo
  1. Install node_modules
pnpm install
  1. Build the code
pnpm run build

Static File

  1. Create file inside /etc/nginx/sites-available
vim /etc/nginx/sites-available/node-static-conf

Fill it with this value:

server {
    listen 80;
    root /var/www/node-repo/dist; # Build code static files are often found in the dist folder.
    index index.html;

    location / {
        try_files $uri $uri/ @rewrites;
        include  /etc/nginx/mime.types;

        proxy_set_header Host $host;
        proxy_set_header Referer $http_referer;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Host $server_name;
        proxy_set_header X-Forwarded-Proto $scheme;
    }

    location @rewrites {
        rewrite ^(.+)$ /index.html last;
    }

	location = /.env {
        deny all;
        return 404;
    }
}
  1. Symlink it into /etc/nginx/sites-enabled
ln -s /etc/nginx/sites-available/node-static-conf /etc/nginx/sites-enabled/node-static-conf
  1. Unlink default file inside sites-enabled folder
unlink /etc/nginx/sites-enabled/default
  1. Test the nginx configuration
nginx -t
  1. Reload the nginx
nginx -s reload

Upstream Port

  1. Install pm2 to run the app in background
npm i -g pm2
  1. Run the app using pm2
PORT=3000 NODE_PORT=3000 pm2 start pnpm --name "node-upstream:3000" -- start
  1. Check the app if it's already running
curl -X GET -I http://127.0.0.1:3000

The stdout should print this:

HTTP/1.1 200 OK
Access-Control-Allow-Origin: *
Content-Type: text/html
Cache-Control: no-cache
Etag: W/"63f-nngLMxQfvLPukzBwkH36P2K460M"
Date: Mon, 24 Jun 2024 15:52:06 GMT
Connection: keep-alive
Keep-Alive: timeout=5
Content-Length: 1599
  1. Create file inside /etc/nginx/sites-available
vim /etc/nginx/sites-available/node-upstream

Fill it with this value:

upstream node-upstream {
    server 127.0.0.1:3000;
}

server {
    listen 80;

    location / {
        proxy_pass http://node-upstream;
        proxy_set_header Host $host;
        proxy_set_header Referer $http_referer;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Host $server_name;
        proxy_set_header X-Forwarded-Proto $scheme;
    }

    location = /.env {
        deny all;
        return 404;
    }
}
  1. Symlink it into /etc/nginx/sites-enabled
ln -s /etc/nginx/sites-available/node-upstream /etc/nginx/sites-enabled/node-upstream
  1. Unlink default file inside sites-enabled folder
unlink /etc/nginx/sites-enabled/default
  1. Test the nginx configuration
nginx -t
  1. Reload the nginx
nginx -s reload

Deploy Golang using goenv

  1. Check out goenv where you want it installed. Usually it's in $HOME/.goenv
git clone https://github.com/go-nv/goenv.git ~/.goenv
  1. Define environment variable GOENV_ROOT to point to the path where goenv repo is cloned and add $GOENV_ROOT/bin to your $PATH for access to the goenv command-line utility
echo 'export GOENV_ROOT="$HOME/.goenv"' >> ~/.bashrc
echo 'export PATH="$GOENV_ROOT/bin:$PATH"' >> ~/.bashrc
  1. Add goenv init to your shell to enable shims, management of GOPATH and GOROOT and auto-completion. Please make sure eval "$(goenv init -)" is placed toward the end of the shell configuration file since it manipulates PATH during the initialization
echo 'eval "$(goenv init -)"' >> ~/.bashrc
  1. If you want goenv to manage GOPATH and GOROOT (recommended), add GOPATH and GOROOT to your shell after eval "$(goenv init -)"
echo 'export PATH="$GOROOT/bin:$PATH"' >> ~/.bashrc
echo 'export PATH="$PATH:$GOPATH/bin"' >> ~/.bashrc
  1. Restart your shell so the path changes take effect
exec $SHELL
or
source ~/.bashrc
  1. Install Go versions into $GOENV_ROOT/versions
goenv install v1.22.2
  1. Set goenv global version
goenv global v1.22.2
  1. Install NGINX proxy
sudo apt install nginx -y
  1. Use SuperUser to access root folder
sudo su
  1. Go to destination folder for your repository, and clone it
cd /var/www
git clone https://gitlab.com/user/go-repo.git
cd go-repo
  1. Install go modules
go mod download && go mod tidy
  1. Copy the env.example file into .env
cp .env.example .env
  1. Build golang app into binary executable file
go build -o <output-file> <main.go-file-path>

Example:

go build -o app.sh cmd/main.go
  1. Create file service for running golang app in background
cd /etc/systemd/system
vim golang-app.service

Fill the file with this config:

[Unit]
Description=golang-app

[Service]
Type=simple
User=root
Group=root
LimitNOFILE=1024
Restart=on-failure
RestartSec=10
startLimitIntervalSec=60
WorkingDirectory=/var/www/go-repo
ExecStart=/var/www/go-repo/app.sh

[Install]
WantedBy=multi-user.target
  1. Restart the daemon and check the service status
systemctl daemon-reload
systemctl status golang-app.service
  1. Start the app and enable the service (to make the app autostart)
systemctl start golang-app.service
systemctl enable golang-app.service
  1. Check the app if it's already running
curl -X GET -I http://127.0.0.1:8000

The stdout should print this:

HTTP/1.1 200 OK
Access-Control-Allow-Origin: *
Content-Type: text/html
Cache-Control: no-cache
Etag: W/"63f-nngLMxQfvLPukzBwkH36P2K460M"
Date: Mon, 24 Jun 2024 15:52:06 GMT
Connection: keep-alive
Keep-Alive: timeout=5
Content-Length: 1599
  1. Create file inside /etc/nginx/sites-available
vim /etc/nginx/sites-available/golang-app

Fill it with this value:

upstream golang-app {
    server 127.0.0.1:8000;
}

server {
    listen 80;

    location / {
        proxy_pass http://golang-app;
        proxy_set_header Host $host;
        proxy_set_header Referer $http_referer;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Host $server_name;
        proxy_set_header X-Forwarded-Proto $scheme;
    }

    location = /.env {
        deny all;
        return 404;
    }
}
  1. Symlink it into /etc/nginx/sites-enabled
ln -s /etc/nginx/sites-available/golang-app /etc/nginx/sites-enabled/golang-app
  1. Unlink default file inside sites-enabled folder
unlink /etc/nginx/sites-enabled/default
  1. Test the nginx configuration
nginx -t
  1. Reload the nginx
nginx -s reload
Last Updated: 7/20/2024, 6:57:22 AM
Contributors: azrimadit