Setting up a VPS Linux server for optimal performance to handle 10,000 daily users involves fine-tuning several services like Nginx, Apache, MySQL, PostgreSQL, and PHP. Below is a step-by-step guide written in English for configuring these services.
1. Initial Server Setup
Before configuring specific services, make sure your VPS is up-to-date and has essential software installed.
Step 1: Update your system
sudo apt update && sudo apt upgrade -y
Step 2: Install basic tools
sudo apt install vim curl wget git -y
Step 3: Set up a firewall (UFW)
sudo ufw allow OpenSSH
sudo ufw allow 'Nginx Full'
sudo ufw enable
2. Nginx Configuration
Nginx is often used as a reverse proxy or load balancer, especially when combined with Apache. For optimal performance, we will adjust worker processes, connection limits, and caching settings.
Step 1: Install Nginx
sudo apt install nginx -y
Step 2: Nginx Configuration
Open the main configuration file:
sudo vim /etc/nginx/nginx.conf
Modify the following lines for performance optimization:
worker_processes auto; # Use all available cores
worker_connections 4096; # Set a high number of connections per worker
# Enable Gzip Compression for faster content delivery
gzip on;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
# Cache static files for better performance
client_max_body_size 100M; # Allow larger file uploads
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
Step 3: Restart Nginx
sudo systemctl restart nginx
3. Apache Configuration
Apache is a powerful and flexible web server. By default, it’s optimized for stability rather than speed, so adjustments are needed to handle a large number of users.
Step 1: Install Apache
sudo apt install apache2 -y
Step 2: Apache Configuration
Open the Apache configuration file:
sudo vim /etc/apache2/apache2.conf
Tweak the following settings for performance:
# Tune the Max Connections
StartServers 5
MinSpareServers 5
MaxSpareServers 10
MaxRequestWorkers 500
MaxConnectionsPerChild 10000
# Enable KeepAlive for faster connection handling
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 5
# Enable mod_deflate for gzip compression
LoadModule deflate_module modules/mod_deflate.so
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript application/javascript
</IfModule>
Step 3: Restart Apache
sudo systemctl restart apache2
4. MySQL Configuration
MySQL can be configured to handle high-traffic workloads by adjusting buffer sizes, query cache, and connection limits.
Step 1: Install MySQL
sudo apt install mysql-server -y
Step 2: MySQL Configuration
Open the MySQL configuration file:
sudo vim /etc/mysql/my.cnf
Tweak the following settings for performance optimization:
[mysqld]
max_connections = 500 # Increase max connections
query_cache_size = 64M # Enable query caching for faster reads
query_cache_limit = 2M
innodb_buffer_pool_size = 1G # Allocate more memory for InnoDB buffers
innodb_log_file_size = 128M
innodb_flush_log_at_trx_commit = 2 # Reduces I/O overhead
max_allowed_packet = 64M
Step 3: Restart MySQL
sudo systemctl restart mysql
5. PostgreSQL Configuration
PostgreSQL is highly scalable and can handle complex queries efficiently. You’ll want to configure it to make full use of available system resources.
Step 1: Install PostgreSQL
sudo apt install postgresql postgresql-contrib -y
Step 2: PostgreSQL Configuration
Open the PostgreSQL configuration file:
sudo vim /etc/postgresql/12/main/postgresql.conf
Tweak the following parameters:
# Connection Settings
max_connections = 500
# Memory Settings
shared_buffers = 2GB # Allocate about 25% of your total RAM
work_mem = 16MB # Memory per connection for sorting
maintenance_work_mem = 256MB # Memory for maintenance operations
# Disk I/O Settings
wal_buffers = 16MB
checkpoint_completion_target = 0.7
# Query Optimizations
effective_cache_size = 4GB # About 50-75% of RAM
Step 3: Restart PostgreSQL
sudo systemctl restart postgresql
6. PHP Configuration
PHP settings should be optimized to handle more requests and improve execution speed. This can be achieved by tuning memory limits, opcache, and execution limits.
Step 1: Install PHP and required extensions
sudo apt install php-fpm php-mysql php-pgsql php-cli php-curl php-gd php-mbstring php-xml php-zip -y
Step 2: PHP Configuration
Open the PHP configuration file:
sudo vim /etc/php/7.4/fpm/php.ini
Modify the following parameters for performance:
memory_limit = 512M # Increase memory limit
upload_max_filesize = 100M # Allow large file uploads
post_max_size = 100M
max_execution_time = 300 # Increase script execution time
# Enable Opcache for faster PHP execution
opcache.enable=1
opcache.memory_consumption=128
opcache.max_accelerated_files=4000
opcache_revalidate_freq=60
Step 3: Restart PHP-FPM
sudo systemctl restart php7.4-fpm
7. Monitoring and Optimizations
Install monitoring tools
To monitor your system’s performance in real time, install some useful tools:
- htop (for CPU and memory monitoring):
sudo apt install htop
- iostat (for disk performance monitoring):
sudo apt install sysstat
- Netdata (a complete monitoring solution):
bash bash <(curl -Ss https://my-netdata.io/kickstart.sh)
Conclusion
After following these steps, you should have an optimized VPS setup that can efficiently handle 10,000 concurrent daily users. The key to ongoing performance is regular monitoring and further tuning as needed based on the actual traffic and server load.
Leave a Reply