How to set up a VPS Web Server
This is more or less just a guideline to remind myself how I go about setting up a web server in a way I'll most likely remember it. This is not limited to a VPS, but I am using a VPS so we will proceed with that in mind.
First start off by getting yourself a vps. I use https://lowendbox.com/ to find myself a cheap vps. Sign up, buy your VPS subscription and launch your VPS at the provider's web portal. They may have a console you can use, but I will be using putty to connect to the server.
Next, set up putty to connect to your newly purchased vps. This can be easily installed on your windows machine by going to https://www.putty.org/. Alternatively, you can manually SSH instead if you prefer.
Enter the IP granted by the VPS provider, logging in as root and entering the password you have set with the VPS provider. If a root password is not provided, look in the VPS provider's web portal to see an option to update the root password.
Begin by verifying what OS you're running. Typically it'll be CentOS/Almalinux.
You can do this by either using cat /etc/os-release or hostnamectl.
The reason for this is because based on the version, you either use yum or apt-get/apt.
Next, make sure we are up to date on everything by entering
sudo yum update
Let's go ahead and install apache by entering
sudo yum install httpd
Once installed, make sure it's enabled and started if not automatically
sudo yum install httpd
Once installed, make sure it's enabled and started if not automatically
sudo systemctl enable httpd.service
sudo systemctl start httpd.service
If you're on a vps, chances are these already come preinstalled.
We will install nano which is a text editor since I like using it over Vi which is natively available.
sudo yum install nano
Optional:
We can install mariadb, php, phpmyadmin if we want here. That would make it a LAMP stack. We're not looking to do that at this time so let's finish out the web server.
Once it's all installed, apache will be up and running and you can view the test page.
We can create our website now. I generally do this step separately as it's a different can of worms.
Once complete, we can make a new directory to put our website in.
mkdir /var/www/html/example.com
Don't forget to create a new config file for the new website.
sudo nano /etc/httpd/conf.d/example.com.conf
and inside of it
<VirtualHost *:80>
ServerName example.com
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html/example.com
ErrorLog /var/www/html/example.com/logs/error.log
CustomLog /var/www/html/example.com/logs/access.log combined
</VirtualHost>
We may need to create the directory for the logs as well.
mkdir /var/www/html/example.com/logs
Let's restart apache to make sure everything is running right.
systemctl restart httpd.service
We should be able to reach our site now.
We can also go ahead and get ourselves a domain to set 'A Record' so we can get an SSL certificate.
Here are the commands for obtain the ssl cert from certbot
sudo yum install epel-release
sudo yum install certbot python3-certbot-apache mod_ssl
sudo certbot --apache -d example.com
sudo certbot renew --dry-run
open crontab by typing
sudo crontab -e
inside put the following, adjusting as you'd like for your needs on how frequently to renew.
42 0,12 * * * certbot renew
If you're unable to access the site after setting up ssh, make sure the firewall is open.
firewall-cmd --add-service=https --permanent
Comments