Your own VPS server gives you complete control over your OpenCart hosting environment. In this guide, we'll set up a server from scratch using Ubuntu 22.04, Nginx, PHP 8.2, and MySQL 8.
Choosing VPS Hosting
For OpenCart, I recommend the following minimum specifications:
- RAM: 2 GB (4 GB for stores with 1000+ products)
- CPU: 2 vCPU
- SSD: 40 GB
- Traffic: 2 TB/month
Don't skimp on the server. Slow hosting = lost customers. The difference between $5 and $20/month can mean thousands of dollars in sales.
— Experience from 50+ OpenCart projects
Initial Server Setup
After connecting to the server via SSH:
# System update
sudo apt update && sudo apt upgrade -y
# Install basic utilities
sudo apt install -y curl wget git unzip htop
# Create deploy user
sudo adduser deployer
sudo usermod -aG sudo deployer
# Configure SSH keys
sudo mkdir -p /home/deployer/.ssh
sudo cp ~/.ssh/authorized_keys /home/deployer/.ssh/
sudo chown -R deployer:deployer /home/deployer/.ssh
Installing Nginx
# Install Nginx
sudo apt install -y nginx
# Start and enable autostart
sudo systemctl start nginx
sudo systemctl enable nginx
Nginx Configuration for OpenCart
# /etc/nginx/sites-available/opencart
server {
listen 80;
server_name example.com www.example.com;
root /var/www/opencart;
index index.php;
# Logs
access_log /var/log/nginx/opencart_access.log;
error_log /var/log/nginx/opencart_error.log;
# Gzip compression
gzip on;
gzip_types text/plain text/css application/json application/javascript text/xml;
# Static file caching
location ~* \.(jpg|jpeg|png|gif|ico|css|js|woff2)$ {
expires 30d;
add_header Cache-Control "public, immutable";
}
# Deny access to system files
location ~* (\.twig|\.tpl|\.ini|\.log|(?<!robots)\.txt)$ {
deny all;
}
# SEO URLs
location / {
try_files $uri $uri/ @opencart;
}
location @opencart {
rewrite ^/(.+)$ /index.php?_route_=$1 last;
}
# PHP processing
location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php8.2-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
fastcgi_buffer_size 128k;
fastcgi_buffers 256 16k;
}
}
sudo nginx -t
Installing PHP 8.2
# Add repository
sudo add-apt-repository ppa:ondrej/php -y
sudo apt update
# Install PHP and modules
sudo apt install -y php8.2-fpm php8.2-mysql php8.2-curl \
php8.2-gd php8.2-mbstring php8.2-xml php8.2-zip \
php8.2-opcache php8.2-intl php8.2-bcmath
PHP Optimization for OpenCart
; /etc/php/8.2/fpm/conf.d/opencart.ini
memory_limit = 256M
upload_max_filesize = 50M
post_max_size = 50M
max_execution_time = 300
max_input_vars = 5000
; OPcache
opcache.enable = 1
opcache.memory_consumption = 128
opcache.max_accelerated_files = 10000
opcache.revalidate_freq = 60
Installing MySQL 8
# Installation
sudo apt install -y mysql-server
# Secure installation
sudo mysql_secure_installation
# Create database and user
sudo mysql -e "CREATE DATABASE opencart CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;"
sudo mysql -e "CREATE USER 'opencart'@'localhost' IDENTIFIED BY 'strong_password_here';"
sudo mysql -e "GRANT ALL PRIVILEGES ON opencart.* TO 'opencart'@'localhost';"
sudo mysql -e "FLUSH PRIVILEGES;"
SSL Certificate (Let's Encrypt)
# Install Certbot
sudo apt install -y certbot python3-certbot-nginx
# Obtain certificate
sudo certbot --nginx -d example.com -d www.example.com
# Automatic renewal (already configured)
Firewall Configuration
# Basic rules
sudo ufw allow ssh
sudo ufw allow 'Nginx Full'
sudo ufw enable
# Check status
sudo ufw status
Automatic Backups
#!/bin/bash
# /home/deployer/backup.sh
DATE=$(date +%Y%m%d)
BACKUP_DIR="/home/deployer/backups"
# Database backup
mysqldump -u opencart -p'password' opencart | gzip > $BACKUP_DIR/db_$DATE.sql.gz
# Files backup
tar -czf $BACKUP_DIR/files_$DATE.tar.gz /var/www/opencart
# Delete old backups (older than 7 days)
find $BACKUP_DIR -type f -mtime +7 -delete
# Add to cron (daily at 3:00)
crontab -e
0 3 * * * /home/deployer/backup.sh
Conclusion
Now you have a fully configured VPS server for OpenCart. Don't forget to regularly update the system and monitor server resources.