I recently created a discord bot for a personal use written in Python. Due to the version of python hosted on the server I had some issues. Decided to fiddle a little too much and we’ve ended up with a dead web server. Turns out Ubuntu is very much dependant on python and you should proceed with caution 🙂
All Good, any I had to start from scratch as naively I had no backups but it’s all a learning process.
Anyway, these are the steps I took to recreate the webserver.
ssh in as root
adduser user (replace user with your username)
usermod -aG sudo user
Logout
From Terminal from your own machine run the following commands (replace id_rsa with actual path, user and host:
ssh-key
cat ~/.ssh/id_rsa.pub | ssh user@host "mkdir -p ~/.ssh && touch ~/.ssh/authorized_keys && chmod -R go= ~/.ssh && cat >> ~/.ssh/authorized_keys"
Disable root Login
sudo nano /etc/ssh/sshd_config
PermitRootLogin no
sudo systemctl restart ssh
My VPS comes with a pre configured user that I don’t want
sudo deluser ubuntu
Firewall Setup
sudo apt install ufw
sudo ufw status (Check If it's running, enable it isn't)
sudo ufw enable
As we’re running a web server
sudo ufw allow http
sudo ufw allow https
SSH Access (You don’t want to lose the ability to connect to your server!)
sudo ufw allow OpenSSH
sudo ufw allow 22
Installing web server, we’ll be using docker and docker compose, uninstall all conflicting packages
for pkg in docker.io docker-doc docker-compose docker-compose-v2 podman-docker containerd runc; do sudo apt-get remove $pkg; done
Add Docker’s official GPG key:
sudo apt-get update
sudo apt-get install ca-certificates curl
sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc
Add the repository to Apt sources:
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \
$(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update
Install latest Docker package
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
Verify the installation
sudo docker run hello-world
Docker Compose Install (We’re using linuxserver.io Webserver Swag)
sudo apt install docker-compose
Verify Installation
docker compose --version
Configure linuxserver.io and compose
sudo groupadd docker
add user to docker group
sudo usermod -aG docker user
Logout and log back in and make directories
sudo mkdir /opt/webserver_swag
sudo chown mike:mike /opt/webserver_swag
Note down user id
id
Create yaml file either on Server or SCP the file over, this will go in /opt/webserver_swag Example config, replace PUID and PGID with the id’s you noted down and create a password, for MYSQL and fill in URL.
version: "2"
services:
mariadb:
image: linuxserver/mariadb
container_name: mariadb
environment:
- PUID=1001
- PGID=1001
- MYSQL_ROOT_PASSWORD=password
- TZ=Europe/London
- MYSQL_DATABASE=WP_database
- MYSQL_USER=WP_dbuser
- MYSQL_PASSWORD=password
volumes:
- /opt/webserver_swag/config/mariadb:/config
restart: unless-stopped
swag:
image: linuxserver/swag
container_name: swag
cap_add:
- NET_ADMIN
environment:
- PUID=id
- PGID=id
- TZ=Europe/London
- URL=URL
- SUBDOMAINS=www
- VALIDATION=http
volumes:
- /opt/webserver_swag/config:/config
ports:
- 443:443
- 80:80 #optional
depends_on:
- mariadb
restart: unless-stopped
I’m using WordPress so I’m going to install that.
wget https://wordpress.org/latest.tar.gz
tar xvf latest.tar.gz -C /opt/webserver_swag/config/www/
rm latest.tar.gz
Change root directory of compose file
cd /opt/webserver_swag/config/nginx/site_confs
nano default.conf
change root to /config/www/wordpress;
Save and Exit
docker compose up -d --force-recreate
Navigate to site and follow the prompts
From the start to finish with researching etc. This process took about an hour, if I had this, I would have saved a lot of time.
Some useful resources I used along the way:
https://www.youtube.com/watch?v=7GTYB8RVYBc
https://docs.docker.com/engine/install/ubuntu/