I wanted to upload files directly into the Wordpress from my bash script and did not want to sit around tinkering with PHP to achieve it. So what I ended up doing is setting up FTP, making life much more simpler.
vsftpd
is a FTP server program. Install it on your remote machine with,
$ sudo apt-get install vsftpd
To configure vsftpd
, open up the /etc/vsftpd.conf
file in your text-editor:
$ sudo vi /etc/vsftpd.conf
The default options are good enough, but if you want to modify them, they are well commented, so you shouldn't find a problem modifying it to your preferences. I didn't modify anything on mine except for the ftpd_banner
.
Additionally, replace the line that says pam_service_name=vsftpd
with
pam_service_name=ftp
Why? Read the error section to know about it.
And remember to restart the FTP server after you're done editing the file.
$ sudo service vsftpd restart
Next, we add a local user on the machine and set his HOME
directory to the directory we want them to be able to upload to (in my case /var/www/html/wordpress/wp-content/uploads/
)
$ sudo useradd -d /var/www/html/wordpress/wp-content/uploads/ uploader
We then set a password for this user,
$ sudo passwd uploader
We should be able to upload files using FTP and the new user we created, so from the local machine,
$ curl -T viEditorCheatSheet.pdf ftp://192.168.56.101 --user uploader:pass
$ curl -I http://192.168.56.101/wordpress/wp-content/uploads/viEditorCheatSheet.pdf
HTTP/1.1 200 OK
Date: Thu, 15 Jan 2015 09:21:55 GMT
Server: Apache/2.4.7 (Ubuntu)
Last-Modified: Thu, 15 Jan 2015 06:47:42 GMT
ETag: "435bd-50cab3b985df0"
Accept-Ranges: bytes
Content-Length: 275901
Content-Type: application/pdf
And that's it! You're all set champ! ;)
Before I changed the pam_service_name=vsftpd
to pam_service_name=ftp
in the config file, I got this error:
$ curl -T viEditorCheatSheet.pdf ftp://192.168.56.101 --user uploader:pass
ftp://192.168.56.101 --user uploader:pass
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- 0:00:03 --:--:-- 0
curl: (67) Access denied: 530
To see what was happening, I tried logging into the FTP server in the debugging mode.
$ ftp -dv 192.168.56.101
Connected to 192.168.56.101.
220 Welcome to blah FTP service.
ftp: setsockopt: Bad file descriptor
Name (192.168.56.101:nitin): uploader
---> USER uploader
331 Please specify the password.
Password:
---> PASS XXXX
530 Login incorrect.
Login failed.
---> SYST
530 Please login with USER and PASS.
I knew that my login credentials were correct, but something was amiss.
After some digging around, I found that you needed to an edit the configuration. So, going back to /etc/vsftpd.conf
$ sudo vi /etc/vsftpd.conf
Hunt for the line that says pam_service_name=vsftpd
and change it to
pam_service_name=ftp
And, restart the FTP server,
$ sudo service vsftpd restart
And, voila! FTP-ing works!
$ curl -T viEditorCheatSheet.pdf ftp://192.168.56.101 --user uploader:pass
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 269k 0 0 100 269k 0 957k --:--:-- --:--:-- --:--:-- 958k
$ curl -I http://192.168.56.101/wordpress/wp-content/uploads/viEditorCheatSheet.pdf
HTTP/1.1 200 OK
Date: Thu, 15 Jan 2015 09:21:55 GMT
Server: Apache/2.4.7 (Ubuntu)
Last-Modified: Thu, 15 Jan 2015 06:47:42 GMT
ETag: "435bd-50cab3b985df0"
Accept-Ranges: bytes
Content-Length: 275901
Content-Type: application/pdf
Download the official 2buntu app for both Android and Ubuntu Touch.