This where the steps I took to deploy one of my apps to Vultr on an Ubuntu server. It also includes the installation of the SSL certificate
If you don't have an SSH key in your local machine, you'll need to create one by runninig:
# You will be prompted to set the file name and a password
ssh-keygen -C "[email protected]"
# Use that public key inside of the server setup page
cat /path/to/your/file.pubOnce the server is up and running, you can connect via SSH to it using the root user and password provided by the Hosting setup page:
ssh root@IP_ADDRESSIf you get prompted for the authenticity of the SSH handshake, just type yes.
If your server didn't do this already, update the system packages by running:
sudo apt update
sudo apt upgradeIf you get prompted by some server config, you can press Enter twice to continue.
It's best practice to user a sudo user instead of the root one for security reasons. To create the new user, use the following commands:
adduser USER_NAME # Create the user
usermod -aG sudo USER_NAME # Add sudo permissions
id USER_NAME # Check if the user was created successfullyNow, manually add the SSH key to the new user. Run:
cd /home/USER_NAME
mkdir .ssh
chmod 700 /home/USER_NAME/.ssh
cd .ssh
sudo nano authorized_keys
sudo chown -R USER_NAME:USER_NAME /home/USER_NAME # Make that user the owner of their directoryInside the authorized_keys paste the same public SSH key from your machine just as you did on the first step. Then exit the editor with CTRL + X, then press Y and Enter to save.
Now you should be able to log in as that user:
exit # First exit the server from the root user
ssh USER_NAME@IP_ADDRESSFinally, to disable root login, edit the sshd_config file:
# /etc/ssh/sshd_config
PermitRootLogin no
PasswordAuthentication noReload the sshd service to apply the changes:
sudo systemctl reload sshdTo install the latest Node.js version using NVM, run the following:
# Install NVM
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.5/install.sh | bash
source ~/.bashrc
# Use NVM to install the latest Node.js version
nvm install --ltsIf you need to use your Github account with SSH for app deployment, you must do the following on the server:
ssh-keygen -t rsa
eval `ssh-agent -s` && ssh-add ~/.ssh/KEY_NAMENow you can use the public key as a Deploy Key inside of your Github project and clone it via:
git clone REPO_URL local_dir_nameThe best process manager for node is PM2, which can be installed globally using:
npm i -g pm2
pm2 startup ubuntu # Set it up for auto-startup. Follow the steps from this commandIt's also recommended to use pm2-logrotate to not store an infinite amount of logs. To install it, run the following:
pm2 install pm2-logrotate
# (Recommended) Only keep 5 log files per process at the same time. Default is 30
pm2 set pm2-logrotate:retain 5For firewall protection, enable the UFW with:
sudo ufw enable
sudo ufw status
sudo ufw allow ssh # Port 22 is for SSHIt's highly recommended to add this package to prevent brute force attacks when someone tries to log into our server. The easiest way to set it up is by running the following commands:
sudo apt install fail2ban
sudo systemctl enable fail2ban
sudo systemctl restart fail2banAfter that, the configuration file will be located at /etc/fail2ban/jail.conf. Based on this article.
To install the NGINX web server, run the following:
sudo apt install nginx
sudo systemctl enable nginx
sudo ufw allow 'Nginx Full'You can now create the configuration files for all of your websites inside of the /etc/nginx/sites-available directory. To make those sites live, run the following:
# Create link to the config file on enabled sites
sudo ln -s /etc/nginx/sites-available/YOUR_CONFIG_FILE /etc/nginx/sites-enabled/
# Test if there are no issues and apply changes
sudo nginx -t
sudo systemctl restart nginxTo install a basic SSL certificates on your NGINX sites, we can use Certbot which also auto-renews them:
sudo apt install certbot python3-certbot-nginx
sudo certbot --nginx -d example.com -d www.example.com
sudo systemctl enable certbot.timer # Enable auto-renewalsIf your app requires Redis, you can install it as follows:
curl -fsSL https://packages.redis.io/gpg | sudo gpg --dearmor -o /usr/share/keyrings/redis-archive-keyring.gpg
echo "deb [signed-by=/usr/share/keyrings/redis-archive-keyring.gpg] https://packages.redis.io/deb $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/redis.list
sudo apt-get update
sudo apt-get install redisInside the /etc/redis/redis.conf file, search and replace the supervised option:
# /etc/redis/redis.conf
supervised systemdAnd just restart the redis-server service to apply the changes:
sudo systemctl restart redis-server
sudo systemctl enable redis-serverTo secure the Redis Databse, you must set a password at requirepass foobared inside /etc/redis/redis.conf. Make sure to use an extremely secure password, since Redis is able to check up to 1M passwords per second if faced with bruteforce.
# ...
requirepass USE_AN_EXTREMELY_SECURE_PASSWORD
# ...We recommend using a random string by running the following command:
openssl rand 60 | openssl base64 -AOnce the password is set, just restart the redis service and you're good to go.
sudo systemctl restart redis
# Test that auth works
redis-cli
set key1 10 # This should throw a NOAUTH error
auth your_redis_password
set key1 10 # This should return OKThe connection string should look something like this:
redis://default:YOUR_REDIS_PASSWORD@localhost