Skip to content

Instantly share code, notes, and snippets.

@rahul-yr
Last active July 26, 2022 15:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rahul-yr/f52845e7393e6896719297abc94dd0b2 to your computer and use it in GitHub Desktop.
Save rahul-yr/f52845e7393e6896719297abc94dd0b2 to your computer and use it in GitHub Desktop.
This is a code-server installation script on GCP VM. It has already configured with Ngnix, Cert Bot and Google Domains. You just need to update the input variables on the top section

This is a script to install and deploy code-server to your domain.

  • This enables you to access the vscode over the internet in a single click

Prerequisites

  • Domain Name (Suggested Google Domains)
  • GCP or Azure or AWS Account

Tech

  • Web server : Nginx
  • SSL : Let's encrypt
  • DNS Provider : Google Domains ( You are free to use any dns provider of your choice. But you need to update the script accordingly.)
  • Google Cloud Compute instance

But for Google Domains you don't need to update any code.

Update the variables below to match your environment

DOMAIN_NAME="temp.rahuldev.in" SUPPORT_EMAIL="temp@rahuldev.in" VSCODE_LOGIN_PASSWORD="admin" USING_GOOGLE_DNS=true USERNAME="Google dns UserName" PASSWORD="Google dns Password"

#!/bin/sh
# Update the variables below to match your environment
DOMAIN_NAME=""
SUPPORT_EMAIL=""
VSCODE_LOGIN_PASSWORD="admin"
USING_GOOGLE_DNS=true
USERNAME=""
PASSWORD=""
# script starts here
# update packages
echo "+ Updating packages..."
sudo apt update
# install code-server from offical repository
echo "+ Installing code-server..."
curl -fsSL https://code-server.dev/install.sh | sh
# create a startup service
echo "+ Creating a startup service..."
sudo systemctl enable --now code-server@$USER
# wait for the service to start
echo "+ Waiting for the service to start..."
sleep 5
# create a script to update the password
echo "+ Updating a script to add the new password..."
sudo sed -i '/password:/d' ~/.config/code-server/config.yaml
sudo echo 'password: '$VSCODE_LOGIN_PASSWORD >> ~/.config/code-server/config.yaml
# restart code-server
echo "+ Restarting code-server..."
sudo systemctl restart code-server@$USER
# install the required packages
echo "+ Installing the required packages for ssl..."
sudo apt install -y nginx certbot python3-certbot-nginx
# create a nginx config file
echo "+ Creating a nginx config file..."
sudo touch /etc/nginx/sites-available/code-server && sudo chmod 777 /etc/nginx/sites-available/code-server
sudo echo '
server {
listen 80;
listen [::]:80;
server_name '${DOMAIN_NAME}';
location / {
proxy_pass http://localhost:8080/;
proxy_set_header Host $host;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection upgrade;
proxy_set_header Accept-Encoding gzip;
}
}
' > /etc/nginx/sites-available/code-server
# link nginx file
sudo ln -sf /etc/nginx/sites-available/code-server /etc/nginx/sites-enabled/code-server
# test the nginx configuration
echo "+ Testing the nginx configuration..."
sudo nginx -t
# using google dns
if [ $USING_GOOGLE_DNS = true ]; then
# create a script to update the dynamic dns
echo "+ Creating a script to update the dynamic dns using http method..."
sudo touch /home/update-dynamic-dns.sh && sudo chmod 777 /home/update-dynamic-dns.sh
sudo echo '#!/bin/sh
# Resolve current public IP
IP=$(curl -s "https://domains.google.com/checkip")
# Update Google DNS Record
URL="https://'${USERNAME}':'${PASSWORD}'@domains.google.com/nic/update?hostname='${DOMAIN_NAME}'&myip=${IP}"
curl -s $URL
' > /home/update-dynamic-dns.sh
# create a systemd service to update the dynamic dns
echo "+ Creating a systemd service to update the dynamic dns..."
sudo touch /etc/systemd/system/update-dynamic-dns.service && sudo chmod 777 /etc/systemd/system/update-dynamic-dns.service
sudo echo "
[Unit]
Description=Update dynamic dns in Google Domains
After=network.target
[Service]
ExecStart=/bin/bash /home/update-dynamic-dns.sh
Restart=always
RestartSec=300s
[Install]
WantedBy=default.target" > /etc/systemd/system/update-dynamic-dns.service
# start the service
echo "+ Starting the service..."
sudo systemctl enable --now update-dynamic-dns.service
fi
# add let's encrpt certificate
echo "+ Adding let's encrypt certificate..."
sudo certbot --non-interactive --redirect --agree-tos --nginx -d ${DOMAIN_NAME} -m ${SUPPORT_EMAIL}
# restart nginx
echo "+ Restarting nginx..."
sudo systemctl restart nginx
# visit the site
echo "+ Congratulations! You have successfully installed code-server with Nginx."
echo "+ Also you have successfully installed Let's Encrypt certificate."
echo "+ As well as successfully scheduled the dynamic dns service."
echo "+ Visit the site at https://${DOMAIN_NAME}"
echo "##############################################################################"
echo ""
echo "Need help? Available on"
echo "Mail : rahul.mso@outlook.com"
echo "Github : https://github.com/rahul-yr"
echo "Linkedin : https://www.linkedin.com/in/rahul-reddy-y/"
echo ""
echo "##############################################################################"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment