Giải pháp CMS WordPress dùng chung code cho nhiều website (multi-domain nhưng không dùng Multisite). Nội dung sẽ có:
define('UPLOADS')/var/www/cms/.site1.com, site2.com, site3.com cùng trỏ về thư mục code này.wp-content/uploads/.define('UPLOADS', ...)Trong wp-config.php có thể thêm dòng sau:
define('UPLOADS', 'uploads/' . $_SERVER['HTTP_HOST']);
📌 Ý nghĩa:
site1.com, file sẽ lưu ở:/var/www/cms/uploads/site1.com/site2.com, file sẽ lưu ở:/var/www/cms/uploads/site2.com/👍 Ưu điểm:
👎 Nhược điểm:
abc.com → xyz.com) thì media path sẽ lệch.Bạn có thể tạo thư mục riêng cho từng site ở ngoài code, ví dụ:
/var/www/uploads/site1.com/ /var/www/uploads/site2.com/ /var/www/uploads/site3.com/
Sau đó trong wp-content/ của CMS, tạo symlink:
cd /var/www/cms/wp-content ln -s /var/www/uploads/site1.com uploads/site1.com ln -s /var/www/uploads/site2.com uploads/site2.com ln -s /var/www/uploads/site3.com uploads/site3.com
📌 Với symlink:
wp-content/uploads/site1.com/.../var/www/uploads/site1.com/.👍 Ưu điểm:
$_SERVER['HTTP_HOST'].👎 Nhược điểm:
www-data:www-data (nếu dùng Nginx/Apache chạy dưới user đó)..php có thể bị khai thác..php trong thư mục uploads/.Ví dụ với Nginx:
location ~* /uploads/.*\.php$ {
deny all;
}
Ví dụ với Apache (.htaccess trong uploads):
<FilesMatch "\.php$">
Deny from all
</FilesMatch>
server {
server_name site1.com;
root /var/www/cms;
location / {
index index.php;
try_files $uri $uri/ /index.php?$args;
}
# Media riêng cho site1
location /uploads/ {
alias /var/www/uploads/site1.com/;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass unix:/run/php/php8.2-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
server {
server_name site2.com;
root /var/www/cms;
location / {
index index.php;
try_files $uri $uri/ /index.php?$args;
}
location /uploads/ {
alias /var/www/uploads/site2.com/;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass unix:/run/php/php8.2-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
<VirtualHost *:80>
ServerName site1.com
DocumentRoot /var/www/cms
<Directory /var/www/cms>
AllowOverride All
Require all granted
</Directory>
# Alias cho uploads
Alias /uploads/ /var/www/uploads/site1.com/
<Directory /var/www/uploads/site1.com>
Options -Indexes
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
<VirtualHost *:80>
ServerName site2.com
DocumentRoot /var/www/cms
<Directory /var/www/cms>
AllowOverride All
Require all granted
</Directory>
Alias /uploads/ /var/www/uploads/site2.com/
<Directory /var/www/uploads/site2.com>
Options -Indexes
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
👉 Kết luận:
define('UPLOADS').