This behavior has been removed from WordPress, so if you need to change the default folder location for uploaded files or images, we need to change wp-config.php
or set using wp-cli
. The default folder is wp-content/uploads
, let say we want to change that to /var/www/images.DOMAIN.com
on the same server and set the URL for files https://images.DOMAIN.com
Install Dependency
We’ll need curl to download the wp-cli file
sudo apt install curl -y
Download WordPress CLI
Download the latest WordPress CLI from the Github
wget https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
move these file to /usr/local/bin
sudo mv wp-cli.phar /usr/local/bin/wp
set the file permission to executable, so we don’t need to run wp-cli using php /usr/local/bin/wp
sudo chmod +x /usr/local/bin/wp
Change WordPress Setting
My current WordPress located at /var/www/DOMAIN.com
. Debian use www-data
as default user for nginx or apache, replace with your webserver user.
# move to WordPress folder cd /var/www/DOMAIN.com sudo -u www-data wp option set upload_path /var/www/images.DOMAIN.com sudo -u www-data wp option set upload_url_path https://images.DOMAIN.com
Create the folder for upload
mkdir /var/www/images.DOMAIN.com
copy data from old location to new location
cp -r /var/www/DOMAIN.com/wp-content/uploads/* /var/www/images.DOMAIN.com/
Setting Web Server
The last part of this setup is to add vhost (server block) for domain images.DOMAIN.com
.
Set A record for DNS images.DOMAIN.com
to Server IP. I use nginx, here’s my config
server { listen 80; server_name images.DOMAIN.com; return 301 https://images.DOMAIN.com$request_uri; } server { listen 443 http2 ssl; ssl_certificate /etc/nginx/DOMAIN/fullchain.cer; ssl_certificate_key /etc/nginx/DOMAIN/images.DOMAIN.com.key; server_name images.DOMAIN.com; error_log /var/log/nginx/images.DOMAIN.com.error_log; root /var/www/images.DOMAIN.com/; index index.htm index.html; }
reload the Nginx then check your WordPress, the images should be served using images.DOMAIN.com
domain.
Verify by checking https://DOMAIN.com/wp-admin/options-media.php