VPS

How to Set up a Linux VPS: a Step-by-step User Guide

Learn how to set up a Linux VPS with this comprehensive step-by-step user guide. Ensure a secure, reliable, and scalable server environment with ease.

is*hosting team 25 Jun 2024 8 min reading
How to Set up a Linux VPS: a Step-by-step User Guide

In the ever-evolving digital world, virtual private servers (VPS) have become a necessity for individuals and businesses. As a scalable and flexible web hosting solution, a VPS allows you to improve the security, control, and performance of your online projects. However, setting up a VPS platform may be a challenging task for beginners who are not familiar with the command line.

This article is about how to set up a Linux VPS server and is designed to help readers efficiently set up and manage a Linux VPS server using the command line, covering everything from initial access to advanced security and performance tuning.

What is Linux VPS?

What is Linux VPS?

Linux Virtual Private Server (VPS) is a virtual server running the Linux OS on a physical server. It offers some of the features and resources of a dedicated server but at a lower cost. Each virtual server operates autonomously with its own dedicated resources, such as a central processing unit (CPU), random access memory (RAM), and disk space, as well as a separate instance of the operating system (OS).

A Linux virtual private server has the following key characteristics:

  1. Insulation. Each VPS operates in isolation from other VPS instances on the same physical server.
  2. Dedicated resources. Guaranteed CPU, RAM, and storage explicitly dedicated to your VPS.
  3. Root access. Complete administrative control over the server, allowing you to install and configure any software.
  4. Scalability. Resources can be easily scaled depending on your needs.

Benefits of Linux VPS Setup from the Command Line

Linux VPS server setup from the command line offers several benefits:

  1. Efficiency and control. The Command Line Interface (CLI) allows for detailed server configuration and optimization, giving complete control over every component.
  2. Resource usage. Command-line tools are lightweight and consume fewer resources than graphical user interfaces (GUIs), which can be helpful for resource-constrained Linux VPS.
  3. Scripts and automation. The CLI allows you to use scripts and automation tools to deploy applications, manage updates, and perform repetitive tasks.
  4. Learning experience. Working with the command line improves your understanding of managing servers and the Linux OS and gives you valuable IT skills.
  5. Remote control. The CLI allows you to manage your server remotely using the SSH (Secure Shell) protocol. The SSH network protocol allows you to remotely manage the OS and tunnel file transfer connections, making it easy to access and administer your Linux VPS server from anywhere.

6 Steps to Set Up a Linux VPS Server

6 Steps to Set Up a Linux VPS Server

Linux VPS server setup from the command line involves several key steps. This guide details the 6 steps to securely set up a Linux VPS server that is optimized for your specific needs.

Step 1: Select a VPS provider

Choosing the right VPS provider is critical to the success of your project.

When choosing a VPS provider, consider the following factors:

  1. Performance: CPU, RAM, and storage specs.
  2. Scalability: the ability to increase the volume of allocated resources as the business grows.
  3. Cost: Cost compared to the resources provided.
  4. Support: availability of technical support.
  5. Reputation: supplier reliability and customer reviews.

To choose a vendor that meets your requirements and budget, consider your requirements, taking into account things like CPU cores, RAM, storage, and data center locations. You should also review customer reviews and comments to gauge the vendor's reputation and quality of support.

Once you register, create an account, and select a custom VPS plan that suits your requirements, the provider will provide you with a virtual server and send you login credentials.

VPS for Your Project

Get the most out of your budget with our affordable, efficient VPS solutions. Fast NVMe, 30+ countries, managed and unmanaged VPS.

VPS Plans

Step 2: Access Your VPS and Create a New User

When you gain access to your VPS, you must keep in mind that you need constant remote access to manage and configure it. The SSH protocol provides secure communication between your local computer and the VPS. To establish an SSH connection, you need your VPS's IP address or domain name and the credentials provided by your ISP. To establish a secure connection, use terminal emulators such as PuTTY on Windows or the built-in SSH client on Linux/macOS.

To connect via SSH, follow these steps.

  1. Open Command Prompt by pressing the Windows + R keys on your keyboard.
  2. In the field, enter cmd and click OK.
  3. In the terminal that opens, use the SSH command, replacing root with your actual username and your_vps_ip with the VPS IP address: 

    ssh root@your_vps_ip

However, to increase security and control access to the system, it is recommended that you create a new user and change privileges on your VPS. Creating user accounts with superuser rights helps regulate who can access your server and what they can do.

To create a new user, follow these steps:

  1. Connect to the server's root user account and enter the following command: 

    adduser username
  2. To run a command with full administrative privileges, the sudo prefix is required. 

    # usermod -aG sudo username
  3. Replace the username with the desired user.
  4. Create a new user password.
  5. To access the VPS using a terminal or SSH client, use these login credentials.

You can add more SSH users to your VPS in the same way.

Avoid unauthorized access by disabling password authentication and using multiple SSH keys for authentication to increase security.

Step 3: Enable Public Key Authentication

Step 3: Enable Public Key Authentication

At this stage, we will tell you how to increase security by enabling public key authentication.

Public key authentication is more secure and complicated to decrypt than regular passwords because it uses a public/private key pair to secure your SSH connection. The public key is stored on your server and used for private authentication, and the private key can only be obtained from the system administrator. Thus, login will be carried out using a passphrase and a private key.

To enable public key authentication, follow these steps:

  1. Create a pair (public and private) of keys with the following command:

    ssh-keygen -t rsa -b 4096
  2. You can choose a location to store the keys; however, in most cases, the default location is under the .ssh/ extension in your home directory. For added security of your key pair, you can set a passphrase for the private key
  3. To copy the public key to the server, run the following command, replacing the username with your actual username and server_ip with the IP address or hostname of the server: 

    sssh-copy-id username@server_ip
  4. Enter the password for the server.
  5. After successful authentication, your public key will be added to the server's ~/.ssh/authorized_keys file.
  6. To apply the configuration changes, restart SSH with the following command: 

    sudo service ssh restart
  7. To test public key authentication, enter the following command followed by the passphrase you set for your private key: 

    ssh -i /path/to/private_key username@server_ip

Step 4: Update and Configure Your VPS

To maintain the security and stability of your VPS, regular updates to system packages and software repositories are necessary. Depending on the Linux distribution you choose, the package management system allows you to install and update software packages quickly:

  • For Debian-based distributions such as Ubuntu, use the Advanced Packaging Tool (APT). To refresh the package cache and update installed packages, run the following commands:

    sudo apt update
  • CentOS users can also use the modified Yellowdog Updater (YUM) package manager to update system packages by running the following commands: 

    sudo yum update
  • Once the package list update is complete, you can proceed to update system packages. The latest security and bug fixes for Debian/Ubuntu will be installed: 

    sudo apt upgrade
  • On CentOS/RHEL: 

    sudo yum upgrade

Sometimes a reboot is required to enable system updates. You can reboot your VPS:

sudo reboot

You can also set up automatic updates and ensure your Linux VPS server is updated regularly. For these purposes, the “unattended-upgrades” update package is used in Debian/Ubuntu, and “yum-cron” in CentOS/RHEL.

It is necessary to regularly monitor and check for available updates and apply them as needed. While updates are required to improve security and performance, they can also introduce compatibility issues or unexpected behavior. Always back up your data before performing updates.

Regularly updating your Linux VPS server ensures that your server is secure and running efficiently, creating a stable environment for applications and websites.

Install critical patches and security updates to protect your Linux virtual private server from potential vulnerabilities and exploitation. To ensure timely maintenance and reduce costs, automate the update process using cron jobs or system timers.

Step 5: Install the Necessary Software

Step 5: Install the Necessary Software

In order to set up a Linux VPS server environment, you need to install the necessary software packages. Here is a list of some required software you must install:

  1. Nginx and Apache: Robust web server solutions that meet varying workloads and performance requirements.
  2. MySQL, PostgreSQL, and MongoDB: database management systems suitable for storing NoSQL and relational data.
  3. Python, Node.js, and Ruby: programming languages for developing and deploying dynamic web applications.
  4. Docker and Kubernetes: containerization technologies for scalable and fault-tolerant application deployment.
  5. Git: Version control systems for streamlining workflows.

To install these development tools, programming languages, databases, and web servers, you can use package managers such as APT (for Debian/Ubuntu) or YUM (for CentOS/RHEL) and the initial commands:

apt install apache2 #
yum install httpd #

However, to avoid further command line mazes when installing software using package managers, we suggest choosing a beautiful and user-friendly GUI control panel that simplifies the task and allows you to manage your online masterpiece easily and with finesse.

Step 6: Protect Your VPS

Reliable firewalls can help protect your Linux VPS server from unauthorized access and malicious threats. Uncomplicated firewall (UFW) for Ubuntu/Debian and firewall for CentOS are Linux firewall products that make it easy to configure and manage firewall policies. Regulate incoming and outgoing traffic by allowing only essential services and ports and blocking potentially dangerous connections. Set a default deny policy, allowing traffic only according to specific rules. Use firewall zones to classify network interfaces according to their trust level. Advanced security tools like Fail2ban can be used to prevent brute-force attacks by dynamically blocking malicious IP addresses. Finally, regularly review your firewall rules and adjust configurations to accommodate changes.

To configure a firewall for your VPS, follow these steps:

  1. Connect to your Linux VPS server using the following command, replacing the username with your actual username and server_ip with the IP address or hostname: 

    ssh username@server_ip
  2. Check existing rules with the following command: 

    sudo iptables -L
  3. Configure firewall rules to allow specific services (in this case, the firewall rule allows incoming TCP traffic on port 22, the default SSH port): 

    sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT
  4. Set the default policy for incoming, outgoing, and forwarding traffic (in this case, setting the default incoming traffic blocking policy): 

    sudo iptables -P INPUT DROP
  5. To save the installed firewall rules, use the following command: 

    sudo iptables-save > /etc/iptables.rules
  6. To load saved rules at boot, enter the following command to create and execute the script: 

    sudo sh -c "iptables-save > /etc/iptables.rules" 
    sudo chmod +x /etc/network/if-pre-up.d/iptables
  7. To check your firewall configuration, reconnect using SSH: 

    ssh username@server_ip

    A successful connection indicates that the firewall rules are configured correctly.

Troubleshoot Common Problems

Troubleshoot Common Problems

When setting up a Linux virtual private server from the command line, you may encounter some common problems. The following sections describe how to troubleshoot VPS connectivity issues, service failures, and performance issues.

Connection Problems

  • SSH connection refused

Symptom: Unable to connect to Linux VPS server via SSH. The error message reads: "Connection refused".

Solution:

  1. Make sure the Linux VPS is running and accessible from the network.
  2. Check the status of the SSH service using the following command and make sure it is active: 

    sudo systemctl status sshd
  3. Make sure the SSH port (default 22) is open on the VPS firewall using:

    sudo ufw status или iptables -L
  4. Ensure the VPS has a valid IP address and the DNS resolution is working correctly.
  • SSH connection timed out

Symptom: An attempt to connect via SSH times out.

Solution:

  1. Make sure the VPS is turned on and connected to the Internet.
  2. Check for network issues or misconfigurations, including incorrect IP addresses or blocked ports.
  3. Use the following command to check your network connection: 

    ping <VPS_IP>
  4. If you are behind a NAT or firewall, ensure port forwarding is configured correctly.
  • Permission denied (public key)

Symptom: The SSH connection is rejected due to key authentication issues.

Solution:

  1. Ensure your SSH key is correctly added to the VPS's ~/.ssh/authorized_keys file.
  2. Check the permissions of the ~/.ssh directory and the authorized_keys file (chmod 700 ~/.ssh and chmod 600 ~/.ssh/authorized_keys).
  3. Confirm that the SSH client is using the correct private key with the -i option (ssh -i /path/to/private_key user@VPS_IP).
Additional Storage

is*hosting guarantees data protection. Reliable storage for backups of your project or for personal use.

Watch Plans

Service Failures

  • gThe service does not startg

Symptom: The service does not start or fails to start.

Solution:

  1. Check the service status using: 

    sudo systemctl status <service_name>
  2. Check the service logs for errors or warnings with: 

    sudo Journalctl -u <service_name>
  3. Ensure that all dependencies for the service are installed and configured correctly.
  4. Check the configuration files for syntax errors or incorrect configurations.
  • gThe service is running but not responding

Symptom: The service appears to be active, but is not responding or working as expected.

Solution:

  1. Restart the service with: 

    sudo systemctl restart <service_name>
  2. Check resource limits such as CPU, memory or disk I/O using tools such as top, htop, or iotop.
  3. Review specific service logs for errors that may indicate underlying problems.
  • Service configuration changes are not applied.

Symptom: Changes to the service configuration file do not take effect.

Solution:

  1. Make sure the service is restarted after the configuration change using: 

    sudo systemctl restart <service_name>
  2. Make sure you are editing the correct configuration file.
  3. Check the configuration file for syntax errors that may prevent the service from restarting correctly.

Performance Issues

  • High CPU usage

Symptom: The VPS is experiencing high CPU usage, resulting in poor performance.

Solution:

  1. Identify the process causing high CPU usage using top or htop.
  2. Optimize or limit resource-intensive processes.
  3. Consider upgrading your Linux VPS server hardware if resource usage is consistently high.
  • Low memory or high swap usag

Symptom: The VPS is running low on memory, resulting in heavy swap usage and poor performance.

Solution:

  1. Monitor memory usage using the free -m or vmstat option.
  2. Reduce memory consumption of running applications or services.
  3. If possible, add more RAM to the VPS.
  4. Optimize or tune apps to use less memory.
  • Disk I/O bottlenecks

Symptom: The VPS is experiencing slow disk I/O, which is affecting overall performance.

Solution:

  1. Use iotop to identify processes causing high disk I/O.
  2. Optimize applications to reduce unnecessary disk writes.
  3. Make sure you have enough disk space and avoid running out of space.
  4. Consider upgrading to faster storage solutions such as SSDs if disk I/O is a persistent problem.

By following these troubleshooting steps, you can quickly identify and resolve most issues that arise when setting up and managing a Linux VPS server from the command line.

Conclusion

Setting up a Linux VPS server from the command line is a fundamental skill for any system administrator or developer looking to manage web applications, databases, or other services effectively. By following the steps described in our guide, you can easily create a secure, reliable, and scalable server environment without any system administrator skills.

Moreover, you can further customize and optimize your VPS to suit your specific needs by installing additional software, deploying applications, and configuring advanced security measures.

Linux VPS

Choose the suitable configuration and enjoy all the benefits of a virtual private server.

From $5.00/mo