0

What would be the best way to set up a Raspberry Pi as a webserver, but with the hosted files on an external hard drive? I have a Raspberry Pi 3 Model B+ which I would like to use as a web server. The OS is stored on a 16GB microSD card, which is all I have, however I have attached a 1TB external hard drive to the Pi as well. My plan is to put all the web content on that drive rather than the SD card.

I'm familiar with setting up webservers using nginx on both physical and virtual machines, but the Pi has baffled me at every step. I've tried both Apache and Nginx, but neither of them will serve content that's not in the root folder (/var/www), which is obviously on the system drive. This seems to be due to a permissions error. What is the best way to fix this? Can samba manage permissions on a local drive?

  • but neither of them will serve content that's not in the root folder - you're doing it wrong ... in nginx it's as simple as root /what/ever/path/you/want in a server config – Jaromanda X May 29 at 4:03
  • Maybe mount your drive on /var/www? – NomadMaker May 29 at 8:24
1

Within Apache you could use the alias option in the configuration file for the site as per this answer I gave a while ago but in this case there are two better options as you want the whole site to move.

Note the rest of this answer assumes your external drive is mounted to /mnt/external and the directory 'website' is the new root directory (i.e. holding the index page) so you would access the contents of the index page with

cat /mnt/external/website/index.html

The first this to note is that any directory structure you are creating should have its security set correctly to limit possible hacks and to give www-data access (often read-only to none data files). Normally I would do this by making the owner and group www-data with

cd /mnt/external/website
chown -R www-data:www-data .

Note this will cut the Pi user off from these directories. You may want to make changes using sudo or make Pi a member of the www-data group with:

sudo usermod -a -G www-data pi

Read only can then be done using:

chmod -R 0444 /mnt/external/website

Other values can be set depending on your needs (and blanket use of 0444 breaks many a web site)...

Once you have security set correctly, then there are two ways of simply moving the root:

May LEAST favourite way to move the whole root is to edit the document root declaration (DocumentRoot) in:

/etc/apache2/sites-enabled/000-default
/etc/apache/apache2.conf

e.g.

DocumentRoot "/mnt/external/website"

This impacts every site by default so I find it normally better to define the root location within the site definition files and then enable them with a2ensite. Virtual hosts can have the root defined in their definition file along the lines of:

<VirtualHost *:80>
   DocumentRoot "/mnt/external/website"
   ServerName www.example.co.uk
</VirtualHost>

Full details of remapping URLs to file locations is here on the Apache site.

After any of these changes you will need to restart Apache with

sudo systemctl restart apache2

without this the changes are not recognised.

Normally, my sites live in directories that match the site URL (inc .org / .co.uk etc) and the definition files are the full site name as well. This helps me keep track when using virtual sites but does not save typing!

| improve this answer | |
  • Hi, I followed your instructions, and I keep getting a "Forbidden" page on my web browser. The error log shows this: [Mon Jun 01 13:00:26.462965 2020] [core:error] [pid 3953] (13)Permission denied: [client 222.155.104.249:50791] AH00035: access to /index.html denied (filesystem path '/mnt/external/public_html/mysite.com/index.html') because search permissions are missing on a component of the path – Michael Jun 1 at 1:07
  • Check ownership of /mnt, /mnt/external /mnt/external/public_html and /mnt/external/public_html/mysite.com/ so that www-data can have access possibly 775 rights are needed? Use ls-l to check. Also you may need to set the execute flag with chmod +x on the directories – Andyroo Jun 1 at 3:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.