Table of Contents
1. Server Monitoring Solutions
External Monitoring: Uptime Robot
Uptime Robot offers an excellent free service to monitor your site's availability and uptime. It provides a way to ensure that your web resources are up and running smoothly. The features offered in the Uptime Robot Free plan should be adequate for most sites. It's actually a great way to monitor the uptime of your sites without requiring any server-side agent installation.
Server Resource Monitoring: Htop
Htop is an excellent tool for monitoring server resources and processes in real time. It provides a comprehensive overview of CPU, memory, and swap usage, allowing users to quickly identify any resource-intensive processes. Htop makes it easy to pinpoint problematic processes by displaying detailed information such as CPU and memory usage for each process.
Installing and Using Htop
Running Htop
F6 - Sort processes by various criteria (CPU, Memory, etc.)
ESC - Return to main view
Q - Quit Htop
Htop Workflow
Glances - No Longer Recommended
Third-Party Monitoring Services
External monitoring services can be valuable for keeping track of server health and performance. These third-party services will normally install an agent on your server. This agent is used to gather data. However, you need to remember that these agents will also require resources.
- Excessive resource usage by monitoring agents can impact the server's ability to serve your site visitors efficiently
- It's crucial to strike a balance between monitoring needs and resource allocation for serving your visitors
- Opting for monitoring solutions that offer lightweight agents or allow customization of monitoring parameters can help mitigate resource usage issues
Netdata Cloud
Netdata Cloud offers both free and paid server monitoring. Under pricing for home, you have the free or community edition of Netdata Cloud.
2. System Updates and Maintenance
Basic Update Commands
Regular system updates are essential for security and stability. Here are the basic commands for updating your Ubuntu server:
Update Package Lists
Upgrade Installed Packages
Remove Unnecessary Packages
Combined Update Commands
You can chain these commands together for efficiency:
Interactive Combined Command
Non-Interactive Combined Command (with -y flag)
System Update Process Flow
(apt update)
(apt upgrade)
(apt autoremove)
Creating a Bash Alias for Updates
To simplify the update process, you can create a bash alias:
Edit Bash Aliases File
Add the Following Alias
Activate the Alias
To activate the alias, either source the file or exit and log back in:
Or simply:
server_updates to
run all update commands automatically.
Creating an Update Script
For more control, you can create a dedicated update script:
Create the Script File
Script Contents
Make the Script Executable
Run the Script
3. WordPress Updates and Permissions
Understanding WordPress Permissions
Proper file permissions are crucial for WordPress security and functionality. This section covers both default and hardened permission configurations.
Default PHP Pool User Permissions
These permissions allow WordPress to update plugins, themes, and core files directly from the dashboard:
Navigate to Site Directory
Check Current Permissions
Set Ownership
Set Directory Permissions (770)
Set File Permissions (660)
Reload PHP-FPM
Hardened Permissions Configuration
For enhanced security, use hardened permissions that restrict write access except where necessary:
Set Ownership
Set Hardened Directory Permissions (550)
Set Hardened File Permissions (440)
Allow Write Access to wp-content Directory (770)
Allow Write Access to wp-content Files (660)
WordPress Permission Workflow
(770/660)
(550/440 with wp-content 770/660)
Permission Update Workflow
When you need to run updates with hardened permissions in place:
Step 1: Change Permissions to Allow Updates
Step 2: Run Updates in WordPress Dashboard
Log into your WordPress dashboard and perform the necessary updates.
Step 3: Re-apply Hardened Permissions
Step 4: Reload PHP-FPM
| Permission Type | Directories | Files | Use Case |
|---|---|---|---|
| Standard | 770 | 660 | Full WordPress functionality, updates |
| Hardened | 550 | 440 | Read-only, enhanced security |
| wp-content (Hardened) | 770 | 660 | Allow uploads and dynamic content |
4. Security Tools
ClamAV - Antivirus Scanner
ClamAV is an open-source antivirus engine designed for detecting malware and viruses on your server.
Installation
Disable Automatic Updates (ClamAV-Freshclam)
If you prefer to update the database manually:
Manual Database Update
Running a Manual Scan
To scan a specific directory:
Running ClamAV After Disabling Automatic Updates
Update the database first, then run the scan:
-r - Recursive scan (scan subdirectories)
/path/2/scan - Replace with your actual directory path
RKHunter - Rootkit Detection
RKHunter (Rootkit Hunter) is a security tool that scans for rootkits, backdoors, and possible local exploits.
Installation
Update RKHunter Database
Run Complete System Check
--checkall - Perform all checks
--sk, --skip-keypress - Skip keypress prompts for automation
View RKHunter Logs
To view the complete log file:
To view the log with pagination:
Disable Automatic RKHunter Scans
If you prefer to run scans manually, remove the cron jobs:
Remove Daily Cron Job
Remove Weekly Cron Job
Security Scanning Workflow
(ClamAV & RKHunter)
(freshclam & rkhunter --propupd)
(clamscan & rkhunter --checkall)
(Check for threats)
| Tool | Purpose | Update Command | Scan Command |
|---|---|---|---|
| ClamAV | Antivirus & Malware Detection | sudo freshclam | sudo clamscan -r /path |
| RKHunter | Rootkit & Backdoor Detection | sudo rkhunter --propupd | sudo rkhunter --checkall --sk |
5. Database Optimization
InnoDB Buffer Pool Monitoring
The InnoDB buffer pool is a crucial component for database performance. Monitoring its usage helps optimize memory allocation.
View InnoDB Buffer Pool Actual Memory Usage
Database Size Analysis
View All Database Sizes
View InnoDB Database Sizes
View Buffer Pool Usage by Database
Table I/O Statistics
Understanding which tables are most active helps identify optimization opportunities.
View Database Table I/O Statistics
MySQLTuner
MySQLTuner is a script that analyzes your MySQL/MariaDB installation and provides recommendations for optimization.
Running MySQLTuner
Database Optimization Workflow
6. Disk Space Management
Overview
Administering disk space on an Ubuntu server is an important task, as your server can crash if you run out of space. The process involves several tasks, including monitoring disk usage and then cleaning up unnecessary files.
Check Disk Space
View Disk Usage in Human-Readable Format
-h - Display sizes in human-readable format (KB, MB, GB)
Cleanup Operations
Remove Unused Packages and Clean Package Cache
apt autoremove - Removes packages that were automatically installed as dependencies but are no longer needed
apt clean - Clears the local repository of downloaded package files
Cleanup System Logs
System logs can accumulate and consume significant disk space over time. You can limit journal logs to a specific time period:
Analyze Directory Sizes
List Directory Sizes Sorted by Size
du - Disk usage command
-a - Show all files and directories
-h - Human-readable format
--max-depth=1 - Only show first level subdirectories
sort -h - Sort by human-readable sizes
The largest directories are displayed at the bottom. You can change to any directory and run the du -ah command to further investigate space usage.
Disk Space Management Workflow
(df -h)
(du -ah | sort -h)
(apt autoremove && apt clean)
(journalctl --vacuum-time)
| Task | Command | Purpose |
|---|---|---|
| Check Disk Space | df -h | View overall disk usage |
| Clean Packages | sudo apt autoremove && sudo apt clean | Remove unused packages and cached files |
| Clean Logs | sudo journalctl --vacuum-time=1days | Remove old system logs |
| Analyze Directories | du -ah --max-depth=1 | sort -h | Find large directories |
7. Performance Tuning
Nginx Backlog Configuration
The backlog parameter determines the maximum length of the queue for pending connections. A higher value can improve performance under high load.
Configuration Setting
Implementation in Nginx Configuration
PHP OPcache File Monitoring
Monitoring the number of PHP files per pool helps optimize OPcache settings for better performance.
Create OPcache Monitoring Script
Script Contents
Make Script Executable and Run
Understanding OPcache Prime Numbers
PHP's OPcache uses prime numbers for the max_accelerated_files setting to optimize hash table distribution. The actual value used will be the first prime number that is greater than or equal to your configured value.
| Configured Value | Actual Prime Value Used | Recommended For |
|---|---|---|
| 200-223 | 223 | Very small sites |
| 400-463 | 463 | Small sites |
| 900-983 | 983 | Small-medium sites |
| 1900-1979 | 1979 | Medium sites |
| 3800-3907 | 3907 | Medium-large sites |
| 7800-7963 | 7963 | Large sites |
| 16000-16229 | 16229 | Very large sites |
Performance Optimization Workflow
Best Practices Summary
Regular Maintenance Schedule
- Daily: Monitor server resources with Htop, check external monitoring alerts
- Weekly: Run system updates, check disk space usage
- Monthly: Run security scans (ClamAV, RKHunter), review database performance
- Quarterly: Review and optimize database settings, clean up old logs and backups
Security Best Practices
- Keep all software updated with the latest security patches
- Use hardened file permissions for WordPress installations when not performing updates
- Regularly scan for malware and rootkits using ClamAV and RKHunter
- Monitor server logs for suspicious activity
- Implement external monitoring to detect downtime quickly
Performance Optimization Tips
- Configure OPcache max_accelerated_files based on actual PHP file count
- Adjust Nginx backlog settings for high-traffic sites
- Optimize database buffer pool size based on database size and usage
- Monitor and clean up disk space regularly to prevent server crashes
- Use MySQLTuner recommendations to optimize database configuration