Deploying Drupal on a Virtual Private Server (VPS) gives you complete control over your hosting environment while maintaining cost-effectiveness. RamNode offers reliable VPS hosting with excellent performance, making it an ideal choice for Drupal sites. In this comprehensive guide, we’ll walk through setting up a complete Drupal installation using the popular LEMP stack (Linux, Nginx, MySQL, PHP).

Prerequisites

Before we begin, ensure you have:

  • A RamNode VPS with Ubuntu 22.04 LTS (recommended)
  • SSH access to your server
  • A domain name pointed to your server’s IP address
  • Basic command-line knowledge

Initial Server Setup

First, connect to your RamNode VPS via SSH:

ssh root@your_server_ip

Update your system packages:

apt update && apt upgrade -y

Create a non-root user with sudo privileges:

adduser drupaluser
usermod -aG sudo drupaluser

Switch to the new user:

su - drupaluser

Install Nginx

Install Nginx web server:

sudo apt install nginx -y

Start and enable Nginx:

sudo systemctl start nginx
sudo systemctl enable nginx

Configure the firewall to allow HTTP and HTTPS traffic:

sudo ufw allow 'Nginx Full'
sudo ufw allow OpenSSH
sudo ufw enable

Install MySQL

Install MySQL server:

sudo apt install mysql-server -y

Secure the MySQL installation:

sudo mysql_secure_installation

Follow the prompts to:

  • Set a strong root password
  • Remove anonymous users
  • Disallow root login remotely
  • Remove test database
  • Reload privilege tables

Create a database and user for Drupal:

sudo mysql -u root -p

In the MySQL prompt, run:

CREATE DATABASE drupal_db CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'drupal_user'@'localhost' IDENTIFIED BY 'strong_password_here';
GRANT ALL PRIVILEGES ON drupal_db.* TO 'drupal_user'@'localhost';
FLUSH PRIVILEGES;
EXIT;

Install PHP

Install PHP and necessary extensions for Drupal:

sudo apt install php8.1-fpm php8.1-mysql php8.1-gd php8.1-xml php8.1-mbstring php8.1-curl php8.1-zip php8.1-intl php8.1-soap php8.1-bcmath php8.1-opcache -y

Configure PHP for optimal Drupal performance by editing the PHP-FPM configuration:

sudo nano /etc/php/8.1/fpm/php.ini

Adjust these settings:

memory_limit = 512M
upload_max_filesize = 64M
post_max_size = 64M
max_execution_time = 300
max_input_vars = 3000
opcache.enable = 1
opcache.memory_consumption = 128
opcache.max_accelerated_files = 4000

Restart PHP-FPM:

sudo systemctl restart php8.1-fpm

Install Composer

Drupal 10+ requires Composer for dependency management:

curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer
sudo chmod +x /usr/local/bin/composer

Download and Install Drupal

Navigate to the web root directory:

cd /var/www/

Download Drupal using Composer:

sudo composer create-project drupal/recommended-project drupal

Set proper permissions:

sudo chown -R www-data:www-data /var/www/drupal
sudo chmod -R 755 /var/www/drupal

Create the settings directory and files:

sudo mkdir /var/www/drupal/web/sites/default/files
sudo cp /var/www/drupal/web/sites/default/default.settings.php /var/www/drupal/web/sites/default/settings.php
sudo chmod 666 /var/www/drupal/web/sites/default/settings.php
sudo chmod 777 /var/www/drupal/web/sites/default/files

Configure Nginx for Drupal

Create a new Nginx server block for your Drupal site:

sudo nano /etc/nginx/sites-available/drupal

Add the following configuration:

server {
    listen 80;
    server_name your_domain.com www.your_domain.com;
    root /var/www/drupal/web;
    index index.php index.html index.htm;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~ ^/sites/.*/files/styles/ {
        try_files $uri /index.php?$query_string;
    }

    location ~ ^(/[a-z\-]+)?/system/files/ {
        try_files $uri /index.php?$query_string;
    }

    location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg)$ {
        expires 1y;
        add_header Cache-Control "public, immutable";
        try_files $uri /index.php?$query_string;
    }

    location ~ /\.ht {
        deny all;
    }

    location = /favicon.ico {
        log_not_found off;
        access_log off;
    }

    location = /robots.txt {
        log_not_found off;
        access_log off;
        allow all;
    }

    location ~* \.(txt|log)$ {
        deny all;
    }

    location ~ \..*/.*\.php$ {
        return 403;
    }

    location ~ ^/sites/.*/private/ {
        return 403;
    }

    location ~ ^/sites/[^/]+/files/.*\.php$ {
        deny all;
    }

    location ~* ^/.well-known/ {
        allow all;
    }
}

Enable the site and remove the default Nginx configuration:

sudo ln -s /etc/nginx/sites-available/drupal /etc/nginx/sites-enabled/
sudo rm /etc/nginx/sites-enabled/default

Test the Nginx configuration:

sudo nginx -t

If the test passes, reload Nginx:

sudo systemctl reload nginx

Complete Drupal Installation

Open your web browser and navigate to your domain. You should see the Drupal installation page.

Follow these steps in the web installer:

  1. Choose language: Select your preferred language
  2. Choose profile: Select “Standard” for a typical installation
  3. Database configuration: Enter your MySQL details:
    • Database type: MySQL
    • Database name: drupal_db
    • Username: drupal_user
    • Password: The password you created earlier
    • Host: localhost
  4. Site configuration: Configure your site details:
    • Site name
    • Admin username and password
    • Admin email address

SSL Certificate Setup (Optional but Recommended)

Install Certbot for Let’s Encrypt SSL certificates:

sudo apt install certbot python3-certbot-nginx -y

Obtain an SSL certificate:

sudo certbot --nginx -d your_domain.com -d www.your_domain.com

Set up automatic renewal:

sudo crontab -e

Add this line:

0 12 * * * /usr/bin/certbot renew --quiet

Performance Optimization

Enable Drupal Caching

In your Drupal admin panel:

  1. Go to Configuration → Performance
  2. Enable “Cache pages for anonymous users”
  3. Enable “Cache blocks”
  4. Set appropriate cache lifetime values

Configure MySQL for Better Performance

Edit MySQL configuration:

sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf

Add these optimizations under [mysqld]:

innodb_buffer_pool_size = 256M
innodb_log_file_size = 64M
query_cache_type = 1
query_cache_size = 32M

Restart MySQL:

sudo systemctl restart mysql

Enable Gzip Compression in Nginx

Edit your Nginx configuration:

sudo nano /etc/nginx/nginx.conf

Uncomment and configure the gzip settings:

gzip on;
gzip_vary on;
gzip_min_length 1024;
gzip_types text/plain text/css text/xml text/javascript application/javascript application/xml+rss application/json;

Security Hardening

Update File Permissions

After installation, secure your settings file:

sudo chmod 444 /var/www/drupal/web/sites/default/settings.php
sudo chmod 755 /var/www/drupal/web/sites/default/files

Install Security Updates

Enable automatic security updates for the system:

sudo apt install unattended-upgrades -y
sudo dpkg-reconfigure unattended-upgrades

Configure Fail2Ban

Install and configure Fail2Ban to protect against brute force attacks:

sudo apt install fail2ban -y
sudo systemctl enable fail2ban
sudo systemctl start fail2ban

Maintenance and Monitoring

Regular Backups

Create a backup script for your Drupal site:

#!/bin/bash
DATE=$(date +%Y%m%d_%H%M%S)
mysqldump -u drupal_user -p drupal_db > /backups/drupal_db_$DATE.sql
tar -czf /backups/drupal_files_$DATE.tar.gz /var/www/drupal/

Monitor Server Resources

Install htop for better process monitoring:

sudo apt install htop -y

Troubleshooting Common Issues

Permission Problems

If you encounter permission issues:

sudo chown -R www-data:www-data /var/www/drupal
sudo find /var/www/drupal -type d -exec chmod 755 {} \;
sudo find /var/www/drupal -type f -exec chmod 644 {} \;

Memory Limit Errors

Increase PHP memory limit in /etc/php/8.1/fpm/php.ini and restart PHP-FPM.

Database Connection Issues

Verify MySQL credentials and ensure the database service is running:

sudo systemctl status mysql

Conclusion

You now have a fully functional Drupal installation running on your RamNode VPS with Nginx and MySQL. This setup provides excellent performance, security, and scalability for most Drupal websites. Remember to keep your system updated, monitor performance regularly, and maintain proper backups.

The LEMP stack offers several advantages for Drupal hosting, including better resource utilization compared to Apache and excellent caching capabilities. Your RamNode VPS provides the reliability and performance needed to run a successful Drupal website.

For production sites, consider implementing additional monitoring tools, CDN integration, and advanced caching strategies to further optimize performance.