[Configuration Optimization] Things I must do when I get a VPS server

Samuel GrantSamuel Grant
4 min read

Optimizing VPS performance is crucial to ensure that your server runs efficiently and stably. This detailed tutorial will guide you through optimizing various aspects of your VPS performance, including system resources, network settings, and security.

1. Choose the Right VPS Configuration

The first step in optimizing performance is to ensure that your VPS configuration meets your needs. Based on your application requirements, choose the appropriate CPU, memory, storage, and bandwidth.

  • CPU: For compute-intensive applications, choose a multi-core CPU to handle concurrent requests efficiently.

  • Memory: Ensure that your VPS has enough memory to support your application without causing excessive swap usage.

  • Storage: Choose SSD storage to significantly improve data read/write speeds, especially for applications that require frequent file access.

  • Bandwidth: Choose the appropriate bandwidth based on your traffic to ensure that network throughput does not become a bottleneck.

  • Beginners are recommended to use some VPS with better performance, such as DigitalOcean and LightNode

2. System Optimization

a. Update System and Software

Regularly update your operating system and software packages to ensure they are up-to-date and free of any known vulnerabilities or performance issues.

sudo apt update && sudo apt upgrade -y  # For Ubuntu/Debian systems
sudo yum update -y                    # For CentOS/RHEL systems

b. Disable Unnecessary Services

Many system services are enabled by default but may not be necessary for your application. Disabling unnecessary services can free up system resources.

  • To view running services:

      systemctl list-units --type=service
    
  • To stop and disable unnecessary services:

      sudo systemctl stop <service-name>
      sudo systemctl disable <service-name>
    

c. Memory Optimization

  • Reduce Swap Usage: Avoid frequent swap access, which can degrade performance. Adjust the swappiness parameter to control how often the system uses swap space.

    To check the current swappiness setting:

      cat /proc/sys/vm/swappiness
    

    Set swappiness to 10 to reduce swap usage:

      sudo sysctl vm.swappiness=10
    

    To make this permanent:

      sudo echo "vm.swappiness=10" >> /etc/sysctl.conf
      sudo sysctl -p
    

3. Network Optimization

a. Optimize TCP Stack

Modify some TCP configurations to improve network performance, especially for high-latency or high-traffic situations.

  • Modify the following kernel parameters to improve network performance:

      sudo sysctl -w net.core.rmem_max=16777216
      sudo sysctl -w net.core.wmem_max=16777216
      sudo sysctl -w net.ipv4.tcp_rmem='4096 87380 16777216'
      sudo sysctl -w net.ipv4.tcp_wmem='4096 87380 16777216'
      sudo sysctl -w net.ipv4.tcp_congestion_control=cubic
      sudo sysctl -w net.ipv4.tcp_fin_timeout=15
    
  • To make this permanent:

      echo "net.core.rmem_max=16777216" | sudo tee -a /etc/sysctl.conf
      echo "net.core.wmem_max=16777216" | sudo tee -a /etc/sysctl.conf
      echo "net.ipv4.tcp_rmem=4096 87380 16777216" | sudo tee -a /etc/sysctl.conf
      echo "net.ipv4.tcp_wmem=4096 87380 16777216" | sudo tee -a /etc/sysctl.conf
      echo "net.ipv4.tcp_congestion_control=cubic" | sudo tee -a /etc/sysctl.conf
      echo "net.ipv4.tcp_fin_timeout=15" | sudo tee -a /etc/sysctl.conf
      sudo sysctl -p
    

b. Firewall Optimization

If you're using a firewall, ensure it doesn't become a performance bottleneck.

  • Disable unnecessary rules and allow essential traffic:

      sudo ufw status  # Check firewall status
      sudo ufw allow 80/tcp  # Allow HTTP traffic
      sudo ufw allow 443/tcp  # Allow HTTPS traffic
      sudo ufw enable
    

4. Application Layer Optimization

a. Web Server Optimization

For servers using Nginx or Apache, optimizing their configurations can significantly improve performance.

  • Nginx Optimization: Edit the nginx.conf file to optimize Nginx settings.

    Increase worker_processes and worker_connections to handle more concurrent connections:

      worker_processes auto;
      worker_connections 1024;
    
  • Apache Optimization: In the httpd.conf file, configure parameters such as KeepAlive and maximum connections.

      KeepAlive On
      MaxKeepAliveRequests 100
      KeepAliveTimeout 5
    

b. Database Optimization

  • MySQL/MariaDB Optimization: Ensure your database handles requests efficiently by adjusting parameters in the my.cnf configuration file, such as innodb_buffer_pool_size and query_cache_size.

    Increase innodb_buffer_pool_size based on your VPS memory:

      innodb_buffer_pool_size = 1G  # Or adjust based on your memory
    

    Enable query cache:

      query_cache_type = 1
      query_cache_size = 32M
    
  • PostgreSQL Optimization: Modify memory and cache parameters in the postgresql.conf file.

      shared_buffers = 1GB
      effective_cache_size = 3GB
      work_mem = 64MB
    

c. Content Caching

Use caching to reduce the load on your database and application.

  • Use Varnish Cache or Redis to cache static content and reduce the load on the web server.

  • Install and configure Redis or Memcached to accelerate data access.

5. Regular Monitoring and Log Analysis

Regularly monitor your VPS performance by checking CPU, memory, disk, and network usage, and promptly address any performance bottlenecks.

  • Use htop or top to view system resources:

      sudo apt install htop
      htop
    
  • Use tools like Netdata and Grafana for detailed performance monitoring.

  • Analyze system logs to identify performance bottlenecks:

      sudo tail -f /var/log/syslog
      sudo journalctl -xe
    

6. Backup and Disaster Recovery

Optimization isn't just about improving performance; it also involves ensuring system reliability and data security.

  • Perform regular automated backups to quickly restore your system in case of failure.

  • Use RAID configurations or cloud service provider redundancy options to prevent data loss.

This is the optimized and translated version in English. You can now easily copy it for your use.
0
Subscribe to my newsletter

Read articles from Samuel Grant directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Samuel Grant
Samuel Grant