Okay, let's set up a standard, optimized LEMP (Linux, Nginx, MariaDB, PHP-FPM) stack on your new Debian VPS, which is an excellent foundation for hosting WordPress.
This guide assumes you have a clean Debian installation and root or sudo access via SSH.
Goal: Install Nginx, MariaDB, and PHP-FPM, configure them for basic security and performance for a WordPress site.
Steps:
- Update System Packages
- Install Nginx
- Install MariaDB Server
- Secure MariaDB Installation
- Install PHP-FPM and WordPress Extensions
- Configure PHP-FPM
- Create MariaDB Database and User for WordPress
- Configure Nginx for Your Domain (WordPress)
- Enable the Nginx Site and Restart
- (Optional but Recommended) Install & Configure SSL with Certbot
- Basic Performance Tuning Notes (Nginx & MariaDB)
- Set up Firewall (UFW)
Detailed Steps:
Step 1: Update System Packages
Always start with updating your package list and upgrading existing packages.
sudo apt update
sudo apt upgrade -y
Step 2: Install Nginx
Install the Nginx web server.
sudo apt install nginx -y
Nginx should start automatically. You can check its status:
sudo systemctl status nginx
# Output should show 'active (running)'
You should now be able to visit your VPS's IP address in a web browser and see the default Nginx welcome page.
Step 3: Install MariaDB Server
Install the MariaDB database server.
sudo apt install mariadb-server mariadb-client -y
Check if MariaDB is running:
sudo systemctl status mariadb
# Output should show 'active (running)'
Step 4: Secure MariaDB Installation
Run the built-in security script to remove insecure default settings.
sudo mysql_secure_installation
Follow the prompts:
- It will ask for the current root password. If this is a fresh install, there likely isn't one, so just press Enter.
- Press
Y to set a root password. Choose a strong, unique password.
- Press
Y to remove anonymous users.
- Press
Y to disallow root login remotely (highly recommended).
- Press
Y to remove the test database.
- Press
Y to reload privilege tables.
Step 5: Install PHP-FPM and WordPress Extensions
Nginx doesn't have built-in PHP processing like Apache's mod_php. It uses PHP-FPM (FastCGI Process Manager). We also need several PHP extensions for WordPress to function correctly.
Find the latest recommended PHP version available in Debian's repositories (e.g., php8.2-fpm).
sudo apt install php-fpm php-mysql php-curl php-gd php-mbstring php-xml php-zip -y
Replace php-fpm with the specific version if needed (e.g., php8.2-fpm). The command above tries to install the default.
Check PHP-FPM status:
sudo systemctl status phpX.X-fpm # Replace X.X with the installed version (e.g., php8.2-fpm)
# Output should show 'active (running)'
Step 6: Configure PHP-FPM
A few PHP settings are important for web applications.
- Edit the main PHP configuration file (
php.ini) for the FPM version. The path might vary slightly depending on the PHP version you installed (e.g., /etc/php/8.2/fpm/php.ini).
sudo nano /etc/php/X.X/fpm/php.ini # Replace X.X
-
Look for and modify these lines (uncomment if they start with ;):
upload_max_filesize = 64M (Increase if you need to upload larger files/themes/plugins)
post_max_size = 64M (Should be equal to or larger than upload_max_filesize)
memory_limit = 256M (Or higher depending on your VPS RAM and site needs)
max_execution_time = 300 (Increase time for scripts to run if needed)
date.timezone = America/New_York (Set your server's timezone)
-
Save the file (Ctrl+X, Y, Enter in nano).
-
For performance, you might tune the PHP-FPM process manager settings, found in the pool configuration file (e.g., /etc/php/8.2/fpm/pool.d/www.conf).
sudo nano /etc/php/X.X/fpm/pool.d/www.conf # Replace X.X
sudo systemctl restart phpX.X-fpm # Replace X.X
Step 7: Create MariaDB Database and User for WordPress
You need a dedicated database and user for your WordPress installation.
sudo mysql -u root -p
# Enter the MariaDB root password you set earlier
Inside the MariaDB prompt (MariaDB [(none)]>):
CREATE DATABASE your_wp_database_name;
CREATE USER 'your_wp_username'@'localhost' IDENTIFIED BY 'your_strong_password';
GRANT ALL PRIVILEGES ON your_wp_database_name.* TO 'your_wp_username'@'localhost';
FLUSH PRIVILEGES;
EXIT;
- Replace
your_wp_database_name, your_wp_username, and your_strong_password with your desired credentials. Use a different, strong password here than your MariaDB root password.
'localhost' means this user can only connect from the same server, which is secure and standard for web applications.
Step 8: Configure Nginx for Your Domain (WordPress)
Create a new Nginx server block configuration file for your domain.
sudo nano /etc/nginx/sites-available/yourdomain.com
Paste the following standard WordPress configuration. Replace yourdomain.com with your actual domain name and /var/www/html/yourdomain.com with the path where you will place your WordPress files. Make sure the fastcgi_pass path matches your PHP-FPM socket (check /etc/php/X.X/fpm/pool.d/www.conf for the listen directive - it's usually a socket path like /run/php/phpX.X-fpm.sock).
server {
listen 80;
listen [::]:80;
server_name yourdomain.com www.yourdomain.com; # Your domain name(s)
root /var/www/html/yourdomain.com; # Path to your WordPress installation
index index.php index.html index.htm;
# Basic security measures
client_max_body_size 64M; # Adjust based on upload_max_filesize in php.ini
location / {
try_files $uri $uri/ /index.php?$args; # Handles WordPress permalinks
}
# Pass PHP requests to PHP-FPM
location ~ \.php$ {
include snippets/fastcgi-php.conf;
# With unix sockets (recommended):
fastcgi_pass unix:/var/run/php/phpX.X-fpm.sock; # *** Make sure this matches your PHP-FPM socket ***
# Or with TCP (if using a different setup):
# fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
# AVOID ACCESS TO .htaccess FILES (Apache specific, but good to block)
location ~ /\.ht {
deny all;
}
# Disable logging for favicon.ico requests
location = /favicon.ico {
log_not_found off;
access_log off;
}
# Disable logging for robots.txt requests
location = /robots.txt {
log_not_found off;
access_log off;
}
# Serve static files with browser caching headers (optional but good)
location ~* \.(css|js|jpg|jpeg|gif|png|webp|ico|woff|woff2|ttf|svg|eot)$ {
expires 30d; # Cache for 30 days
access_log off;
log_not_found off;
}
# Log file locations
access_log /var/log/nginx/yourdomain.com-access.log;
error_log /var/log/nginx/yourdomain.com-error.log;
}
- Save the file (Ctrl+X, Y, Enter in nano).
Step 9: Enable the Nginx Site and Restart
Create a symbolic link from sites-available to sites-enabled to activate the new configuration. Remove the default Nginx site if it's still active.
sudo ln -s /etc/nginx/sites-available/yourdomain.com /etc/nginx/sites-enabled/
sudo unlink /etc/nginx/sites-enabled/default # Remove default site config
Test the Nginx configuration for syntax errors:
sudo nginx -t
If the test is successful (syntax is ok, test is successful), reload Nginx to apply the changes:
sudo systemctl reload nginx
Step 10: (Optional but Recommended) Install & Configure SSL with Certbot
Running your site over HTTPS is crucial for security and SEO. Certbot makes getting free Let's Encrypt certificates easy with Nginx.
sudo apt install certbot python3-certbot-nginx -y
Run Certbot for Nginx:
sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com
Certbot will guide you through the process, asking for an email, agreeing to terms, and potentially asking if you want to redirect HTTP traffic to HTTPS (recommended). It will automatically modify your Nginx config.
Step 11: Basic Performance Tuning Notes (Nginx & MariaDB)
Achieving "optimal" is an ongoing process and depends on your specific site's traffic and resource usage. Here are a few starting points:
- Nginx Global (
/etc/nginx/nginx.conf):
worker_processes auto; (Usually optimal)
worker_connections 1024; (Good start, increase if needed)
- Ensure
gzip on; is enabled for compression (it often is by default). Check gzip_types to ensure common web file types are included.
- MariaDB (
/etc/mysql/mariadb.conf.d/50-server.cnf or similar):
- The most important setting is
innodb_buffer_pool_size. This should be set to roughly 70-80% of your available RAM after accounting for the OS, Nginx, PHP-FPM, and other processes. On a 1GB VPS, you might start with something like innodb_buffer_pool_size = 128M or 256M and monitor memory usage. Don't set it too high or you'll swap heavily.
- Other settings like
max_connections might need adjustment if you have many simultaneous users, but the default is often fine for a single site on a small VPS.
- Restart MariaDB (
sudo systemctl restart mariadb) after changing config.
- Tools: Use tools like
htop to monitor resource usage (CPU, RAM, Swap) and mysqltuner.pl (search for installation instructions) to get recommendations for MariaDB tuning based on its uptime and usage patterns. Do NOT blindly apply all mysqltuner recommendations; understand what each setting does.
Step 12: Set up Firewall (UFW)
A firewall is essential for security. UFW (Uncomplicated Firewall) simplifies iptables.
sudo apt install ufw -y
sudo ufw default deny incoming
sudo ufw default allow outgoing
Allow essential services:
sudo ufw allow OpenSSH # For SSH access (usually port 22)
sudo ufw allow 'Nginx HTTP' # For HTTP (port 80)
sudo ufw allow 'Nginx HTTPS' # For HTTPS (port 443) - only after setting up SSL
Enable the firewall:
sudo ufw enable
# Type 'y' and Enter to proceed
Check the firewall status:
sudo ufw status verbose
Now you have a base LEMP stack configured. Your next steps would be to:
- Upload your WordPress files to
/var/www/html/yourdomain.com.
- Import your WordPress database using the
your_wp_database_name, your_wp_username, and your_strong_password created in Step 7.
- Edit
/var/www/html/yourdomain.com/wp-config.php with the correct database details.
- Perform the database search/replace (if changing domains or paths) using
wp-cli (see previous answer).
- Update your domain's DNS A records to point to your new VPS IP address.
- Test your site thoroughly.
This setup provides a solid, secure, and reasonably performant environment for WordPress on a VPS. Remember that "optimal" is iterative; monitor your server's performance and adjust configurations as needed based on your actual usage.