Created
May 23, 2021 04:20
-
-
Save ianomad/de16680baa03c73238cddbfaa8720b78 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -eu | |
# ---------------- variables -------------------- | |
sandbox_domain="demo.sb.devsbox.io" | |
email="[email protected]" | |
nginx_conf_content=$(cat << EOM | |
# redirect http to https | |
server { | |
listen 80 default_server; | |
listen [::]:80 default_server; | |
server_name _; | |
return 301 https://\$host\$request_uri; | |
} | |
server { | |
listen 8080; | |
listen [::]:8080; | |
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; | |
} | |
} | |
EOM | |
) | |
# ---------------- functions -------------------- | |
nginx_setup() { | |
# for amazon linux - sudo amazon-linux-extras install nginx1 -y | |
sudo apt update -y && sudo apt upgrade -y | |
sudo apt install nginx -y | |
echo $nginx_conf_content | sudo tee -a /etc/nginx/sites-available/devsbox | |
sudo ln -s /etc/nginx/sites-available/code-server /etc/nginx/sites-enabled/devsbox | |
sudo systemctl restart nginx | |
} | |
codeserver_setup() { | |
# the simplest command to install code-server | |
curl -fsSL https://code-server.dev/install.sh | sh | |
# enable it at the boot time | |
sudo systemctl enable --now code-server@$USER | |
echo 'waiting for 10 seconds for code server to start' | |
sleep 10 | |
# set the password | |
sudo sed -i.bak 's/password: .*/password: demo/' ~/.config/code-server/config.yaml | |
# restart the box to make the changes take effect | |
sudo systemctl restart code-server@$USER | |
} | |
https_setup() { | |
# certbot is a free ssl certificate | |
sudo apt install -y nginx certbot python3-certbot-nginx | |
# this will append configurations to the nginx file (/etc/nginx/sites-enabled/devsbox) | |
sudo certbot --non-interactive --redirect --agree-tos --nginx -d $sandbox_domain -m $email | |
} | |
# ---------------- run -------------------- | |
# setup codeserver | |
codeserver_setup | |
# proxy nginx to code server | |
nginx_setup | |
# enable https | |
https_setup |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment