đ Table of Contents
Introduction
This comprehensive guide covers essential server maintenance tasks for backend server professionals. Regular server maintenance is crucial for maintaining the security, stability, and performance of your server infrastructure. These updates encompass a variety of tasks, including installing security patches to safeguard against threats, fixing bugs to enhance reliability, and optimizing performance through software upgrades and the removal of obsolete components.
While automated updates can seem convenient, they may introduce compatibility issues. Automated updates may not always account for specific server configurations or dependencies. They may inadvertently install patches or upgrades that haven't been thoroughly tested for compatibility or reliability. Regular manual oversight allows server administrators to carefully assess the impact of updates, thereby ensuring compatibility with the existing server configuration.
1. Server Updates
Understanding Update Commands
There are three essential update commands you need to run to keep the packages installed on your server up to date:
| Command | Purpose |
|---|---|
| sudo apt update | Updates the package list from repositories |
| sudo apt upgrade | Upgrades the packages identified in the package list |
| sudo apt autoremove | Removes any unneeded packages from the server |
Basic Update Commands
Individual Commands:
Combined Commands (with confirmation prompts):
The double ampersand (&&) chains commands together. It means "execute the next command only if the previous command succeeds." If sudo apt update completes successfully, then sudo apt upgrade will execute, and so on.
Combined Commands (automatic confirmation with -y flag):
Using the -y flag will automatically confirm all prompts. While this speeds up the process, it's recommended to check which packages are being updated, especially if components of your hosting stack (nginx, PHP, MariaDB) are being upgraded.
Update Process Flow
Each step must complete successfully before the next one executes
Understanding Common Messages
Deferred Upgrades Due to Phasing
You may encounter messages indicating that upgrades have been deferred due to phasing. This means that some software repositories use a phased approach to roll out updates gradually to users. Updates are not made available to everyone at once, but are instead released to a subset of users initially.
Packages Kept Back
Having some packages kept or held back from upgrading could be due to dependency conflicts. The package manager APT defers their upgrade until the issues preventing their upgrade are resolved.
Do NOT attempt to manually force an upgrade. The packages will upgrade automatically once any underlying issues have been resolved. These issues are not specific to your server, but rather exist at the repository level, or are related to dependencies or conflicts with the packages themselves. Allow APT to manage the upgrades for you.
Method 1: Creating a Bash Alias
To speed up the process, you can create a bash alias that combines all update commands:
Step 1: Navigate to home directory and edit .bash_aliases
Step 2: Add the alias
Step 3: Activate the alias
You can activate the alias by typing the following source command or by logging out and logging back in:
After logging back in, simply type:
Method 2: Creating a Bash Script
Another approach is to create a bash script for server updates:
Step 1: Create the script file
Step 2: Add the script contents
The script shown above does NOT include the -y flag after upgrade and autoremove, allowing you to review packages before confirming. You can add -y flags if you prefer automatic confirmation.
Step 3: Make the script executable
Step 4: Run the script
2. WordPress Updates
WordPress updates require specific file permissions to run smoothly through the dashboard. There are two approaches: default permissions and hardened permissions.
Method 1: Default PHP Pool User and Permissions
This method uses standard permissions that allow easy updates through the WordPress dashboard.
Step 1: Navigate to the site directory
Step 2: Check current permissions
Step 3: Set ownership to PHP pool user
Step 4: Set directory permissions
Step 5: Set file permissions
Step 6: Run updates in WordPress dashboard
Open the WordPress dashboard and all updates will run without issue.
Step 7: Restart PHP-FPM
Method 2: Hardened Permissions (Recommended for Production)
This method provides enhanced security by using restrictive permissions, but requires permission changes before and after updates.
Initial Setup with Hardened Permissions:
WordPress Update Process with Hardened Permissions
Step 1: Change permissions to allow updates
Step 2: Run WordPress updates through the dashboard
Open the WordPress dashboard and perform all necessary updates.
Step 3: Harden permissions after updates
Step 4: Reload PHP-FPM
Hardened permissions significantly improve security by restricting write access. The wp-content directory maintains write permissions (770/660) to allow uploads and plugin/theme installations, while the rest of the WordPress installation uses read-only permissions (550/440).
Permission Explanation Table
| Permission | Directories | Files | Purpose |
|---|---|---|---|
| Standard | 770 | 660 | Allows updates and modifications through WordPress |
| Hardened (Root) | 550 | 440 | Read and execute only - maximum security |
| Hardened (wp-content) | 770 | 660 | Allows uploads and plugin/theme management |
3. ClamAV Antivirus
ClamAV is an open-source antivirus engine for detecting trojans, viruses, malware, and other malicious threats on your server.
Installation
The ClamAV definition database will be updated automatically after installation.
Disable Automatic Updates (Optional)
If you prefer manual control over virus definition updates, you can disable the automatic freshclam service:
Manual Database Updates
To manually update the virus definitions:
Running Manual Scans
To scan a directory or path on your server:
- -r: Recursive scan (scans subdirectories)
- /path/2/scan: Replace with the actual path you want to scan
Complete Manual Workflow
When using ClamAV with automatic updates disabled, follow this workflow:
Update database and run scan:
ClamAV Workflow Diagram
Example: Scanning WordPress Installation
4. Rootkit Hunter (rkhunter)
Rootkit Hunter (rkhunter) is a Unix-based tool that scans for rootkits, backdoors, and possible local exploits on your server.
Installation
Initial Setup
After installation, update the file properties database:
Running a Complete System Scan
To run a comprehensive check of your system:
- --checkall: Performs all available tests
- --sk or --skip-keypress: Skips the need to press Enter after each test (useful for automated runs)
Viewing Scan Results
After running a scan, you can view the detailed log file:
Using cat (displays entire log):
Using less (allows scrolling through log):
Disable Automatic Scheduled Scans
By default, rkhunter may be configured to run automatically via cron. If you prefer manual control:
Remove daily cron job:
Remove weekly cron job:
Rootkit Hunter Workflow
Always run sudo rkhunter --propupd after system updates or when you make legitimate
changes to system files. This prevents false positives in future scans.
5. Database Tuning
Proper database tuning is essential for optimal performance. This section covers MariaDB/MySQL tuning and monitoring queries.
InnoDB Buffer Pool Monitoring
The InnoDB buffer pool is the memory area that holds cached data and indexes. 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 only:
View buffer pool usage by database:
Table I/O Statistics
Understanding which tables are frequently accessed helps optimize queries and indexing:
MySQLTuner
MySQLTuner is a script that analyzes your MariaDB/MySQL installation and provides optimization recommendations.
Running MySQLTuner:
MySQLTuner requires sudo privileges as it needs to login to MariaDB to gather statistics and analyze performance.
Database Tuning Process
Key Performance Metrics Table
| Metric | Query | Purpose |
|---|---|---|
| Buffer Pool Usage | Innodb_buffer_pool_bytes_data | Monitor actual memory usage |
| Database Sizes | information_schema.tables | Identify large databases |
| Table I/O | performance_schema | Find frequently accessed tables |
6. Disk Space Management
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 monitoring disk usage and cleaning up unnecessary files.
Check Disk Space
The -h flag means "human readable format" - displays sizes in KB, MB, GB instead of bytes.
Cleanup Methods
1. Remove Unused Packages
2. Cleanup System Logs
System logs can accumulate and consume significant disk space. You can clean logs older than a specified time:
- --vacuum-time=1days: Remove logs older than 1 day
- --vacuum-time=7days: Remove logs older than 7 days
- --vacuum-size=100M: Keep only 100MB of logs
Analyze Directory Sizes
To identify which directories are consuming the most disk space:
Navigate to root directory:
List directory sizes sorted by size:
- -a: Display all files and directories
- -h: Human readable format
- --max-depth=1: Only show top-level directories
- | sort -h: Sort results by size (largest at bottom)
The largest directories are displayed at the bottom. You can change to any directory and run the
du -ah command to drill down further.
Disk Space Management Workflow
Common Space-Consuming Directories
| Directory | Typical Contents | Cleanup Strategy |
|---|---|---|
| /var/log/ | System and application logs | Use journalctl --vacuum or manually delete old logs |
| /var/cache/apt/ | Package cache files | sudo apt clean |
| /tmp/ | Temporary files | Usually auto-cleaned, but can manually remove old files |
| /var/www/ | Web files and backups | Remove old backups and unused files |
Always verify what you're deleting before removing files. Never delete system files or logs that are currently in use without understanding the consequences.
7. OPcache File Monitoring
OPcache improves PHP performance by storing precompiled script bytecode in memory. Monitoring the
number of PHP files per pool helps optimize the opcache.max_accelerated_files setting.
Understanding OPcache Max Accelerated Files
The opcache.max_accelerated_files directive determines how many PHP files can be cached.
PHP uses prime numbers for this setting to optimize hash table distribution.
PHP uses the following prime numbers: 223, 463, 983, 1979, 3907, 7963, 16229, 32531, 65407, 130987, 262237, 524524, 1048793
The actual value used will be the first prime number in this set that is greater than or equal to the configured value.
Nginx Backlog Configuration
Before diving into OPcache monitoring, it's worth noting the backlog configuration for nginx:
In nginx configuration:
OPcache Monitoring Script
Create a script to monitor OPcache settings and count PHP files:
Create the script:
Script contents:
Script Features
| Feature | Description |
|---|---|
| Prime Rounding | Calculates the actual prime number used by PHP OPcache |
| Pool Analysis | Checks opcache settings in PHP-FPM pool configuration files |
| File Counting | Counts PHP files in each WordPress installation |
| Comparison | Allows you to compare configured vs. actual values |
Running the Script
Optimizing OPcache Settings
Set opcache.max_accelerated_files to the next prime number above your total PHP file
count. For example, if you have 2,500 PHP files, set it to 3907. This ensures all files can be
cached with optimal hash table distribution.
Conclusion
Regular server maintenance is essential for ensuring the security, stability, and performance of your hosting infrastructure. This guide has covered the fundamental maintenance tasks that every backend server professional should perform regularly:
- Server Updates: Keep your system secure with regular package updates using apt commands or automated scripts
- WordPress Updates: Manage permissions correctly to ensure smooth updates while maintaining security
- Security Scanning: Use ClamAV and rkhunter to detect malware, rootkits, and security threats
- Database Optimization: Monitor and tune MariaDB/MySQL for optimal performance
- Disk Space Management: Prevent server crashes by monitoring and managing disk usage
- OPcache Optimization: Improve PHP performance through proper OPcache configuration
- Manual oversight is preferable to fully automated updates for critical systems
- Security requires layered approaches: permissions, scanning, and monitoring
- Performance optimization is an ongoing process requiring regular analysis
- Documentation and scripting save time and reduce errors
- Regular maintenance prevents major issues and downtime
By following these practices and understanding the commands and processes involved, you'll be able to maintain a secure, stable, and high-performing server infrastructure. Remember to always test changes in a development environment before applying them to production systems, and maintain regular backups as a safety net.