CentOS VPS Optimization Tips for Improved Performance
This tutorial provides essential tips for optimizing your CentOS VPS to improve its performance and efficiency, covering system tweaks, software optimizations, and resource management.
centos
XLarge
VPS
4
vCPU
16 GB
Memory
160 GB
NVMe Disk
8192 GB
Traffic
39.90€
/month
* Up to 8 vCPU, 48GB RAM, 480GB NVMe Disk Space and 1Gbit/s Network Speed
#### Prerequisites
Before you begin, ensure you have:
- A CentOS VPS (This guide is tested with CentOS 8)
- A user account with sudo privileges
- Basic familiarity with the command line
#### Step 1: Update Your System
Keeping your system up-to-date ensures you have the latest performance improvements and security patches:
```bash
sudo dnf update -y
```
#### Step 2: Optimize System Services
Disable unnecessary services to free up system resources. List all running services:
```bash
sudo systemctl list-units --type=service --state=running
```
Disable any services you don't need:
```bash
sudo systemctl disable service_name
sudo systemctl stop service_name
```
Replace `service_name` with the actual name of the service you want to disable.
#### Step 3: Tune Kernel Parameters
Adjust kernel parameters to improve system performance. Open the sysctl configuration file:
```bash
sudo nano /etc/sysctl.conf
```
Add the following parameters to optimize network performance:
```plaintext
net.core.netdev_max_backlog = 5000
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.ipv4.tcp_rmem = 4096 87380 16777216
net.ipv4.tcp_wmem = 4096 65536 16777216
net.ipv4.tcp_window_scaling = 1
net.ipv4.tcp_max_syn_backlog = 4096
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_timestamps = 1
net.ipv4.tcp_sack = 1
net.ipv4.tcp_no_metrics_save = 1
net.ipv4.tcp_congestion_control = cubic
net.ipv4.tcp_moderate_rcvbuf = 1
```
Save and exit the file, then apply the changes:
```bash
sudo sysctl -p
```
#### Step 4: Optimize Disk Performance
Enable write-back caching to improve disk performance:
```bash
sudo hdparm -W1 /dev/sda
```
Replace `/dev/sda` with your actual disk identifier.
Use `fstrim` to free up unused blocks on SSDs, improving performance:
```bash
sudo fstrim -v /
```
Schedule a weekly trim by adding a cron job:
```bash
echo "0 2 * * 7 /sbin/fstrim -av" | sudo tee -a /etc/cron.d/fstrim
```
#### Step 5: Optimize Memory Usage
Adjust the `swappiness` value to control the tendency of the kernel to swap. Lower values reduce swapping:
```bash
sudo sysctl vm.swappiness=10
```
Make this change permanent by adding it to `/etc/sysctl.conf`:
```plaintext
vm.swappiness=10
```
Enable HugePages for better memory management:
```bash
echo "vm.nr_hugepages = 128" | sudo tee -a /etc/sysctl.conf
sudo sysctl -p
```
#### Step 6: Install and Configure a Performance Monitoring Tool
Install `htop` for real-time monitoring of system resources:
```bash
sudo dnf install htop -y
htop
```
For more detailed monitoring, install and configure `sysstat`:
```bash
sudo dnf install sysstat -y
sudo systemctl start sysstat
sudo systemctl enable sysstat
```
#### Step 7: Use a Lightweight Web Server
If you're running a web server, consider using a lightweight server like Nginx instead of Apache:
```bash
sudo dnf install nginx -y
sudo systemctl start nginx
sudo systemctl enable nginx
```
#### Step 8: Optimize MySQL Performance (If Applicable)
If you're running MySQL, optimize its performance by tuning its configuration:
Open the MySQL configuration file:
```bash
sudo nano /etc/my.cnf
```
Add or adjust the following parameters:
```plaintext
[mysqld]
innodb_buffer_pool_size = 1G
innodb_log_file_size = 256M
innodb_flush_log_at_trx_commit = 2
query_cache_size = 64M
query_cache_type = 1
max_connections = 200
```
Adjust the values based on your server's resources and workload. Save and exit the file, then restart MySQL:
```bash
sudo systemctl restart mysqld
```
#### Step 9: Use a Content Delivery Network (CDN)
Offload static content delivery to a CDN to reduce server load and improve response times. Services like Cloudflare offer free plans for basic usage.
#### Conclusion
By following these optimization tips, you have improved the performance and efficiency of your CentOS VPS. Regularly monitor your system and make adjustments as needed to maintain optimal performance.
**Additional Resources:**
- **CentOS Documentation:** [https://www.centos.org/docs/](https://www.centos.org/docs/)
- **Nginx Documentation:** [https://nginx.org/en/docs/](https://nginx.org/en/docs/)
- **MySQL Performance Tuning:** [https://dev.mysql.com/doc/refman/8.0/en/optimization.html](https://dev.mysql.com/doc/refman/8.0/en/optimization.html)