How to harden OpenCart into a secure setup?

Summary
Security hardening for an OpenCart installation begins with proper file permissions and web server rules to minimize attack surfaces. Key practices include setting appropriate folder permissions (folders: 755, PHP files: 644), ensuring writeable directories like image/, system/storage/cache/, etc., are restricted, and making critical folders read-only. Apache configuration involves disabling directory browsing with `Options -Indexes`, blocking access to sensitive folders using `Require all denied`, preventing PHP execution in specific directories, and restricting config.php access. Nginx server block rules enforce similar security measures. Additionally, practices like renaming the admin folder path, restricting admin panel access via IP, using strong passwords, removing unused extensions, clearing logs, backing up before installation, testing on a staging environment, and auditing third-party code are recommended to further enhance security. While these steps reduce risks, they do not fully protect against malicious extension code; continuous monitoring and updates are essential.
Admin
Moderating
Donor Member
Master Member
Elite Member
Active Member
Beigetreten
Mai 31, 2020
Nachrichten
2,980
Reaktionswert
1,693
Security hardening starts with proper file permissions, then web server rules to reduce attack surfaces.

1. File Permissions (base requirement)​

Apply these permissions before webserver rules. Never use 777 globally.

  • Folders: 755
  • PHP, twig, html, css, js files: 644
  • Writeable directories (only these folders need write permission):image/, system/storage/cache/, system/storage/logs/, system/storage/download/, system/storage/upload/Set these writable folders to 755 (or 775 if your host requires group write).
  • Critical folders must be read-only: catalog/, admin/, install/ (delete install folder completely after setup).
Rules to follow:

  • Do NOT give PHP execution permission inside upload/image directories.
  • Remove write permission from PHP files after installation.
  • Delete unused files and old leftover modules/themes from server.

2. Apache (.htaccess rules, place at site root)​

  1. Disable directory browsing
Options -Indexes
  1. Block access to sensitive folders
<FilesMatch "\.(php|twig)$">
<If "%{REQUEST_URI} =~ m#^/(system|catalog|admin)/#">
Require all denied
</If>
</FilesMatch>
  1. Block PHP execution inside image and upload folders
<Directory "/image">
<FilesMatch "\.(php|phtml|php5|twig)$">
Require all denied
</FilesMatch>
</Directory>
<Directory "/system/storage/upload">
<FilesMatch "\.(php|phtml|php5|twig)$">
Require all denied
</FilesMatch>
</Directory>
  1. Prevent access to config files
<Files "config.php">
Require all denied
</Files>
<Files "admin/config.php">
Require all denied
</Files>
  1. Disable unsafe PHP functions in htaccess (if host allows php_value)
php_value disable_functions eval,passthru,system,shell_exec,popen

3. Nginx server block rules​

  1. Disable directory listing
  1. Deny direct access to sensitive folders
location ~ ^/(system|catalog|admin)/ {
deny all;
}
  1. Block PHP execution in image and storage upload folders
location ~ ^/(image|system/storage/upload)/.*\.(php|twig)$ {
return 403;
}
  1. Block config.php
location ~ /config\.php$ {
return 403;
}
  1. Restrict PHP entry point, only allow index.php
location ~* ^(?!/index\.php).*\.php$ {
return 403;
}
  1. Limit request size to prevent large malicious uploads
client_max_body_size 32M;

Extra OpenCart security practices​

  1. Rename your admin folder path, do not keep default /admin.
  2. Restrict admin panel access by IP in Apache / Nginx if you have static IP.
  3. Use strong admin passwords and enable 2FA if your OpenCart version supports it.
  4. Remove unused extensions and themes, delete leftover module files.
  5. Regularly clear cache and review logs under system/storage/logs/.
  6. Always backup database + files before installing new modules.
  7. Test all webserver rules on staging first, bad rules can break your store.
Important note:Web server rules and permission settings reduce attack risks, but they cannot fully protect you from malicious extension code. Always audit extensions and test them on staging before production use.
 
Trending Resources
Zurück
Oben