Skip to content

Instantly share code, notes, and snippets.

@kylefmohr
Last active March 18, 2024 05:11
Show Gist options
  • Save kylefmohr/3c54a33d4e179e9b55ca71a6eec89fc5 to your computer and use it in GitHub Desktop.
Save kylefmohr/3c54a33d4e179e9b55ca71a6eec89fc5 to your computer and use it in GitHub Desktop.
Script to install cdr/code-server v4.19.1, point it do a domain, and install LetsEncrypt
#!/bin/bash
#Tested working on Ubuntu 20.04 and 22.04!
VERSION='4.22.1'
echo "This script will install code-server v$VERSION, enable it as a service, and expose it to the internet with a LetEncrypt Certificate."
echo ""
echo "By this point, you should already have a domain name pointed to the IP address of this server!"
read -p "Press ENTER to continue or CTRL+C to quit"
echo "Which domain name do you want to use? Including subdomain if applicable: "
read domainname
echo "Please enter the password you'd like to use for code-server: "
read -s password
echo "Which user should the code-server service run as? Preferrably not root..."
read user
echo "Enter an email address for LetsEncrypt: "
read email
if [ ! -d "/home/$user/" ]
then
echo "User $user doesn't exist, creating"
sudo adduser $user --gecos "" --disabled-password
sudo usermod -aG sudo $user
echo "$user:$password" | sudo chpasswd
fi
sudo apt install nginx -y
mkdir ~/code-server
cd ~/code-server
echo "Downloading code-server"
wget https://github.com/coder/code-server/releases/download/v$VERSION/code-server-$VERSION-linux-amd64.tar.gz
echo "Unpacking code-server files"
tar -xzvf code-server-$VERSION-linux-amd64.tar.gz > /dev/null 2>&1
cp -r code-server-$VERSION-linux-amd64/ code-server
sudo cp -r code-server/ /usr/lib/code-server
ln -s /usr/lib/code-server/code-server /usr/bin/code-server
sudo mkdir /var/lib/code-server
echo "[Unit]
Description=code-server
After=nginx.service
[Service]
Type=simple
User=$user
Environment=PASSWORD=$password
Environment=EXTENSIONS_GALLERY='{\"serviceUrl\": \"https://marketplace.visualstudio.com/_apis/public/gallery\"}'
ExecStart=/usr/lib/code-server/bin/code-server --bind-addr 127.0.0.1:8080 --user-data-dir /var/lib/code-server --auth password
Restart=always
[Install]
WantedBy=multi-user.target" > /lib/systemd/system/code-server.service
sudo systemctl start code-server
#need to sleep 1 second to give service a sec to start up. then we check the status to ensure it's working
sleep 1s
sudo systemctl enable code-server
echo "server {
listen 80;
listen [::]:80;
server_name $domainname;
location / {
proxy_pass http://localhost:8080/;
proxy_set_header Upgrade \$http_upgrade;
proxy_set_header Connection upgrade;
proxy_set_header Accept-Encoding gzip;
proxy_set_header Host \$host;
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 \$host;
proxy_set_header X-Forwarded-Proto \$scheme;
}
}" > /etc/nginx/sites-available/code-server.conf
sudo ln -s /etc/nginx/sites-available/code-server.conf /etc/nginx/sites-enabled/code-server.conf
sudo nginx -t
sudo systemctl restart nginx
echo "Installing certbot software"
#if ubuntu18.04, use these instead
#sudo add-apt-repository ppa:certbot/certbot > /dev/null 2>&1
#sudo apt install python-certbot-nginx -y > /dev/null 2>&1
#if ubuntu20.04/22.04 (recommended), leave this next line uncommented
sudo apt install python3-certbot-nginx -y > /dev/null 2>&1
sudo certbot --nginx --agree-tos --redirect --no-eff-email -m $email -d $domainname
echo "Setting permissions on code-server folders"
sudo chown -R $user /usr/lib/code-server
sudo chown -R $user /var/lib/code-server
sudo apt install -y python3-venv python3-pip build-essential > /dev/null 2>&1
echo "That should do it! Head to https://$domainname to try it out"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment