How to Host a Website on Linux VPS? A Complete Guide
VPS hosting is web hosting that provides a virtual private server within a shared physical server. It provides a dedicated environment with guaranteed resources such as CPU, RAM, and storage. Among the available VPS options, Linux is considered one of the popular choices owing to its stability, security, and personalization. Hosting websites on Linux VPS gives users extensive control over their servers. It’s also more scalable for increasing traffic, and prices are reasonable compared to the ones offered by a dedicated server.
In this tutorial, we'll explore the steps needed to host your website on a Linux VPS. These include choosing a good VPS provider, setting up the server environment, installing a web server, deploying website files, and managing domain and DNS settings to make your site accessible online.
#Why do you need a VPS provider?
A VPS provider gives you the infrastructure to host your website in a dedicated environment. Unlike shared hosting, VPS gives you full control, better performance, and flexibility when configuring the resources according to your needs. With faster website performance and better security comes higher rankings on search engines. Therefore, moving to VPS hosting can directly improve your SEO efforts. Furthermore, on shared hosting, email performance can be affected by other users sending spam or abusing resources. VPS hosting provides better control over your email system.
Providers like Cherry Servers offer scalable VPS plans with excellent support that lets you customize the server environment to your website's needs.
#Prerequisites
Before you begin to host your website on a Linux VPS, certain things need to be in place.
-
Register a domain name through which the visitors shall access your site.
-
Have a stable and speedy internet connection to handle your server.
-
Sign up with a reputable VPS provider such as Cherry Servers.
-
Choose a VPS with enough resources for your website with at least 1 GB of RAM for lightweight websites, a multi-core CPU for better performance, and enough storage to fit your website's files and database.
The above steps cannot be skipped and serve as the foundation of your website hosting system.
Deploy and scale your projects with Cherry Servers' cost-effective dedicated or virtual servers. Enjoy seamless scaling, pay-as-you-go pricing, and 24/7 expert support—all within a hassle-free cloud environment.
#How to host a website on Linux VPS?
Now that you have completed the prerequisites, you are now in a good position to execute this process. Follow the steps listed below to launch your website successfully.
#Step 1: Setting up your Linux VPS
The first thing you should do is set up the server environment. For this, you have to choose a distribution that suits your needs. The most common ones that are suitable for web hosting include Ubuntu, CentOS, and Debian. Ubuntu is highly recommended for beginners because it is easy to handle and has a great community behind it.
Next, access your VPS using SSH with credentials given by your VPS provider. You can now connect remotely to the terminal of your server, where you’ll perform all configurations.
Proceed to create a new user with limited privileges for security purposes by running the adduser
command, and then setting a secure password. Due to security concerns, one should not use the default root account for day-to-day operations.
Now, configure your server with a firewall to protect against unauthorized access. Use a simple tool like UFW to set up basic firewall rules. Allow the required ports to proper functionality - SSH for remote login on port 22, HTTP for the web at port 80, and HTTPS for secure web traffic on port 443.
Always make sure that your firewall matches any other specific security policies or procedures with regard to your server. You are now ready to do further configurations on your Linux VPS.
#Step 2: Web server setup
After setting up a Linux VPS, the next step is to install a web server that hosts your website. The two options that naturally come into mind are Apache and Nginx. Apache is very flexible and well-documented. Therefore, it is an excellent choice for a beginner. Nginx is known for its performance in handling high loads and is hence the best choice for scalable websites.
To install Apache on your Linux VPS, execute the following command in your terminal.
sudo apt-get update
sudo apt-get install apache2
For Nginx, execute this.
sudo apt update
sudo apt install nginx
Your web server should automatically start after installation. Just for confirmation, try accessing the IP address of your VPS via a web browser. If you've installed Apache, this should show you the default Apache2 Ubuntu page. If you've installed Nginx, it should show you a welcome page that confirms the successful installation.
You can ensure that when the system starts, the web server will launch automatically by executing the following command.
For Apache:
sudo systemctl enable apache2
For Nginx:
sudo systemctl enable nginx
With the web server up and running, you are one step closer to hosting your website.
#Step 3: Testing the web server
After installing the web server, you should test its functionality. Open a web browser and enter the public IP address of your server. Alternatively, use the following command to check the response.
curl -I localhost
However, if you're accessing from another machine, you can use the command below.
curl -I server-ip
You should see HTTP headers confirming the web server is running, such as 200 OK or 301 Moved permanently for Nginx. If everything is working correctly, you can proceed.
On the web browser, visit the server’s IP address or domain name and you should get the following welcome page:
#For Apache
#For Nginx
#Step 3: Setting up a domain name
You should now connect a domain name with your VPS. That way, visitors can access your website using a friendly name instead of your server's IP address.
First, purchase a domain name from a domain registrar such as GoDaddy, Namecheap, or Google Domains. Having bought a domain, you need to configure its DNS through your registrar's control panel. Create an 'A' record to point the domain name to the public IP of your VPS instance. Indicate your VPS's IP address in the 'A' record. For the root domain, leave it blank or use the hostname as the subdomain.
DNS updates might take several hours to propagate across the entire internet. Enter your domain name in a web browser to check the connection. If everything is configured properly, the default web server page should be displayed to you. You can also run a DNS lookup command from the terminal.
dig yourdomain.com
This will show whether the domain is correctly pointing to your VPS. If the correct IP is listed, your domain is connected successfully, and you're ready to move on to configuring your website.
#Step 4: Setting up your database
Most websites rely on databases for storing and managing content. Therefore, read carefully and set up the database accordingly. For websites, MySQL and MariaDB are likely the most popular open-source DBMSs. MariaDB is a fork of MySQL and is always advisable for use in production because it has many improvements in both performance and security. While both are great options, this article will focus on MySQL.
Now, let's set up the database on your Linux VPS. Start by updating your package list and installing the MySQL server.
sudo apt update
sudo apt install mysql-server -y
Once the installation is complete, MySQL will start automatically. You can verify this by checking the service status.
sudo systemctl status mysql
After confirming MySQL is running, log in to the MySQL shell as the root user.
sudo mysql -u root -p
Create a new database for your website with the following command.
CREATE DATABASE my_website_db;
Next, create a user and grant it the necessary permissions to access and manage the database.
CREATE USER 'myuser'@'localhost' IDENTIFIED BY 'securepassword';
GRANT ALL PRIVILEGES ON my_website_db.* TO 'myuser'@'localhost';
FLUSH PRIVILEGES;
EXIT;
For added security, run the mysql_secure_installation script.
sudo mysql_secure_installation
Be sure to set up a strong password validation policy.
Next, the script will guide you through securing your MySQL installation by removing insecure default settings, setting a root password, and more. Once complete, your MySQL database will be ready for use, ensuring secure and efficient data management for your website.
#Step 5: Uploading website files
Immediately after setting up the database, you need to upload your website files to your Linux VPS. The files can be transferred using secure methods like SCP, FTP, or SFTP. Command-line users favor SCP, whereas FTP or SFTP offers a simple interface for those who prefer graphical tools.
Before uploading, create a good directory structure on your server. The website files are normally held in the /var/www/
directory. You can create a folder for your website by executing the following.
sudo mkdir -p /var/www/mywebsite
Once the directory is set up, transfer your files using your chosen method. Make sure to upload HTML, CSS, JavaScript files, and any other assets to this folder.
After uploading, configure appropriate file permissions to ensure security. Use the chmod command to grant necessary access.
sudo chmod -R 755 /var/www/mywebsite
This ensures the right balance of accessibility and security for your website files.
#Step 6: Configuring your web server
To ensure your web server serves your website correctly, configure virtual hosts for Apache or server blocks for Nginx.
For Apache, create a virtual host file in the /etc/apache2/sites-available/
directory. Here’s how to set it up.
sudo nano /etc/apache2/sites-available/mywebsite.conf
Add the following configuration, replacing mywebsite.com with your domain.
<VirtualHost *:80>
ServerAdmin webmaster@mywebsite.com
ServerName mywebsite.com
DocumentRoot /var/www/mywebsite
</VirtualHost>
ErrorLog ${APACHE_LOG_DIR}/mywebsite.com-error.log
CustomLog ${APACHE_LOG_DIR}/mywebsite.com-access.log combined
Enable the site and restart Apache.
sudo a2ensite mywebsite.conf
sudo systemctl restart apache2
For Nginx, create a directory structure for your website files. In this example, I’ll create it in the /var/www
directory.
sudo nano /var/www/mywebsite/html
Make sure to upload HTML, CSS, JavaScript files, and any other assets to this folder.
Next, create a server block in /etc/nginx/sites-available/
.
sudo nano /etc/nginx/sites-available/mywebsite
Add the following lines of code:
server {
listen 80;
server_name mywebsite.com www.mywebsite;
root /var/www/mywebsite.com/public_html;
index index.html;
access_log /var/log/nginx/mywebsite.com.access.log;
error_log /var/log/nginx/mywebsite.com.error.log;
}
Next, create a symbolic link to enable the site. The symbolic link allows Nginx to recognize your configuration and include it in its active sites.
sudo ln -s /etc/nginx/sites-available/mywebsite
/etc/nginx/sites-enabled/
Once you've created the symbolic link, you need to restart Nginx for the changes to take effect.
sudo systemctl restart nginx
Test the configuration by accessing your domain. If everything is set up correctly, your website should be live.
#Step 7: Securing your website with SSL
How can you protect the data exchanged between the visitors and your server? SSL encryption is the answer. It protects data between your server and visitors, keeps their privacy safe, and even increases your SEO ranking. These days, if you're running an online store or any modern web app, an SSL certificate is more like a necessity. Fortunately, Let's Encrypt, a certificate authority, makes the setup procedure easier, and it's totally free! Additionally, installing the tool Certbot is all it takes to automate the SSL installation process.
For Apache:
sudo apt-get install certbot python3-certbot-apache
For Nginx:
sudo apt-get install certbot python3-certbot-nginx
Then, request an SSL certificate.
For Apache:
sudo certbot --apache
For Nginx:
sudo certbot --nginx
Follow the prompts to complete the installation.
Let’s Encrypt certificates are valid for 90 days, but Certbot can automatically renew them. Set up auto-renewal with this command.
sudo systemctl enable certbot.timer
With SSL configured, your website is secure with HTTPS.
#Step 8: Setting up backups and monitoring
To keep your website data secure, setting up periodic backups and server health checks is necessary. These automated backups facilitate your quick restore in case of data loss or failure of servers. You can schedule backups for your website files and database using tools such as rsync or tar. Set up a cron job to automate backups like this.
0 2 * * * /usr/bin/rsync -av /var/www/mywebsite /backup/directory/
This command will run the backup daily at 2 AM.
You have to take care of the health of your server, too. For that, you need to obtain real-time system resource usage statistics by using tools such as htop, top, nmon, while further monitoring can be done with Netdata, Monit, or Prometheus to track CPU, memory, and disk usage. Set up alerts to notify you so that you can fix a problem well before it affects your website's performance. Regular backups and monitoring ensure ongoing reliability.
#Troubleshooting common issues
When hosting a website on a Linux VPS, several common issues may arise.
-
Delays in DNS propagation: It may take anywhere from several minutes to 48 hours for changes to your domain's DNS records to propagate globally. During this time, your website may be intermittently unavailable. If the process takes too long, verify that your DNS settings are correctly configured and proactively resolve any issues using tools like dig or nslookup.
-
Web server errors: If your website isn’t loading or is showing error pages, check the web server’s log files for clues. Common errors include misconfigurations in virtual host or server block files, which can be resolved by reviewing and correcting your configuration settings.
-
Database connection issues: These are typically due to incorrect credentials or server settings. Ensure your MySQL credentials in the configuration file match those in your database, and verify that the database server is running.
#Conclusion
Linux VPS hosting allows flexibility, full control, and the ability to scale, which is why many webmasters choose it as the best option. With the steps discussed above, it should not be an issue to set up your VPS, install your web server, configure your domain, secure your site with SSL, and develop normal backups and monitoring to create a safe and reliable hosting environment. It is good to know how to handle issues like DNS delays, server errors, or problems with database connections to keep your website running smoothly. Proper configuration and maintenance of your Linux VPS ensure a reliable hosting environment for your website, with guaranteed security and performance.
Cloud VPS - Cheaper Each Month
Start with $9.99 and pay $0.5 less until your price reaches $6 / month.