CI/CD with Github Actions
How to Point Domain and Deploy NextJS Project using Github on Nginx Remote Server or VPS
- Get Access to Remote Server via SSH
- Verify that all required softwares are installed
- Install Software (If required)
- Install PM2 (If required)
- Add PM2 Process on Startup
- Verify Nginx is Active and Running
- Verify Web Server Ports are Open and Allowed through Firewall
- Exit from Remote Server
- Login to Your Domain Provider Website
- Navigate to Manage DNS
- Add Following Records:
Type | Host/Name | Value |
---|---|---|
A | @ | Your Remote Server IP |
A | www | Your Remote Server IP |
AAAA | @ | Your Remote Server IPv6 |
AAAA | www | Your Remote Server IPv6 |
- Copy Project from Local Machine to Remote Server or VPS. There are two ways to do it:-
-
Using Command Prompt
- On Local Windows Machine Make Your Project Folder a Zip File using any of the software e.g. winzip
- Open Command Prompt
- Copy Zip File from Local Windows Machine to Linux Remote Server
- Copied Successfully
- Get Access to Remote Server via SSH
- Unzip the Copied Project Zip File
-
Using Github
- Open Project on VS Code then add .gitignore file (If needed)
- Push your Poject to Your Github Account as Private Repo
- Make Connection between Remote Server and Github Repo via SSH Key
- Generate SSH Keys
- If Permission Denied then Own .ssh then try again to Generate SSH Keys
- Open Public SSH Keys then copy the key
- Go to Your Github Repo
- Click on Settings Tab
- Click on Deploy Keys option from sidebar
- Click on Add Deploy Key Button and Paste Remote Server's Copied SSH Public Key then Click on Add Key
- Clone Project from your github Repo using SSH Path It requires to setup SSH Key on Github
-
- Create Virtual Host File
- Write following Code in Virtual Host File
- Enable Virtual Host or Create Symbolic Link of Virtual Host File
- Check Configuration is Correct or Not
- Install Dependencies
- Create Production Build
- Create pm2 config File inside project folder
- Write below code in ecosystem.config.js file
- Restart Nginx
- Start NextJS Application using pm2
- Save PM2 Process
- Check PM2 Status
- Now you can make some changes in your project local development VS Code and Pull it on Remote Server (Only if you have used Github)
- Pull the changes from github repo
- Create Production Build
- Reload using PM2
How to Automate NextJS Project Deployment using Github Action
- On Your Local Machine, Open Your Project using VS Code or any Editor
- Create A Folder named .scripts inside your root project folder e.g. miniblog/.scripts
- Inside .scripts folder Create A file with .sh extension e.g. miniblog/.scripts/deploy.sh
- Write below script inside the created .sh file
- Go inside .scripts Folder then Set File Permission for .sh File
- Create Directory Path named .github/workflows inside your root project folder e.g. miniblog/.github/workflows
- Inside workflows folder Create A file with .yml extension e.g. miniblog/.github/workflows/deploy.yml
- Write below script inside the created .yml file
- Go to Your Github Repo Click on Settings
- Click on Secrets and Variables from the Sidebar then choose Actions
- On Secret Tab, Click on New Repository Secret
- Add Four Secrets HOST, PORT, USERNAME and SSHKEY as below
- You can get Server User Name by loging into your server via ssh then run below command
- Generate SSH Key for Github Action by Login into Remote Server then run below Command
- Open Newly Created Public SSH Keys then copy the key
- Open authorized_keys File which is inside .ssh/authroized_keys then paste the copied key in a new line
- Open Newly Created Private SSH Keys then copy the key, we will use this key to add New Repository Secret On Github Repo
- Commit and Push the change to Your Github Repo
- Get Access to Remote Server via SSH
- Pull the changes from github just once this time.
- Your Deployment should become automate.
- On Local Machine make some changes in Your Project then Commit and Push to Github Repo It will automatically deployed on Live Server
- You can track your action from Github Actions Tab
- If you get any File Permission error in the action then you have to change file permission accordingly.
- All Done
How to Enable HTTPS in Your Domain Hosted on Linux Remote Server or VPS
Let's Encrypt is a non-profit certificate authority run by Internet Security Research Group that provides X.509 certificates for Transport Layer Security encryption at no charge.
Certbot is a free, open source software tool for automatically using Let’s Encrypt certificates on manually-administrated websites to enable HTTPS.
- To Access Remote Server via SSH
- Install Certbot and it’s Nginx plugin
- Verify Web Server Ports are Open and Allowed through Firewall
- Obtain an SSL certificate
- Check Status of Certbot
- Dry Run SSL Renewal