2.7 KiB
Optimizing an Ubuntu server is a critical step for ensuring stability, security, and peak performance. Here is a general tutorial on how to best configure your server.
1. Update Your System
Always start by ensuring your system is up to date with the latest security patches and software versions.
sudo apt update
sudo apt upgrade -y
2. Security
Securing your server is a top priority to prevent unauthorized access.
-
Secure SSH: Change the default SSH port (22) to a random, non-standard port. It's also highly recommended to disable root login and use key-based authentication instead of passwords.
sudo nano /etc/ssh/sshd_configModify these lines:
Port 22toPort <a different number>PermitRootLogin yestoPermitRootLogin noPasswordAuthentication yestoPasswordAuthentication no
-
Set Up a Firewall (UFW): Enable the firewall and only open the ports you need for your services.
sudo ufw allow ssh # or your new SSH port sudo ufw allow http sudo ufw allow https sudo ufw enable
3. Performance
Optimize your server's performance based on the services it will run.
For a Web Server (LAMP/LEMP Stack)
- PHP OPcache: This is one of the most important steps for PHP performance. Ensure it's enabled and configured to your site's needs.
- Web Server Fine-Tuning: Optimize your web server (Nginx or Apache). For Nginx, you can adjust the number of worker processes. For Apache, you can tune
MaxRequestWorkersand other settings. - Database (MySQL/MariaDB): Fine-tune database cache settings like
innodb_buffer_pool_size. A good rule of thumb is to allocate 50-70% of your total RAM to this setting.
General Server Performance
-
Enable Zram: Zram creates a compressed block device in RAM, which significantly improves the speed of swap space and reduces I/O load.
sudo apt install zram-tools -
Optimize
sysctl: Adjust kernel parameters for better network performance. This can tune the TCP stack for higher throughput.sudo nano /etc/sysctl.confAdd the following lines for network optimization:
net.core.rmem_max = 16777216 net.core.wmem_max = 16777216 net.core.netdev_max_backlog = 262144 net.ipv4.tcp_rmem = 4096 87380 16777216 net.ipv4.tcp_wmem = 4096 65536 16777216 net.ipv4.tcp_max_syn_backlog = 65536
4. Monitoring
Monitor your server to identify bottlenecks. Tools like top, htop, nmon, or Grafana can help you keep an eye on CPU, memory, and I/O usage, allowing you to make further optimizations where needed.