LINUX - Simple HTTP Fileserver / Webserver on Debian

SIMPLE FILESERVER / WEBSERVER ON DEBIAN
=========================================

Networking PreSteps
---------------------
Im assuming your web server is behind a NAT (router/firewall etc.) So all of the port forwards are completely configured from your router/firewall.

Best Case Scenario: 

(1)
Port Forward port 80 TCP from your public IP (Router) to port 80 on your Webserver, thats behind this router. This way you can access everything like this http://ip-or-hostname/ (ip or hostname of your routers wan ip by the way, your WAN address, in other words) without having to do http://ip-or-hostname:50000/

(2)
Another option is to port forward any port, for this article Ill use 50000 for examples sake, to port 80 on your server. No config changes would be required here since the server is still listening on port 80.
The effect is that to access your webserver/fileserver from the internet you would need to go to this website like this: http://ip-or-hostname:50000/
To access your webserver locally(within your network) you would have to do it like this: http://ip-or-hostname/ since locally everything is on 80 you dont need
Note that even though you didnt need to change you apache fileservers configs to listen in on a different port, you still need to access the webserver / fileserver from the internet with the other port, 50000 in this case.

(3)
Worst case, because you have to edit 2 files to accomodate, portforward port 50000 from router to port 50000 on your webserver (or any other port) and edit your /etc/apache2/ports.conf and /etc/apache2/sites-available/default files. Those 2 files refer to port 80 which works with the above 2 cases, but not to port 50000. So change the 80 to port 50000.
The effect is that to access your webserver/fileserver from the internet you would need to go to this website like this: http://ip-or-hostname:50000/
To access your webserver locally you would have to do it like this: http://ip-or-hostname:50000/

(4)
Another wierd case, and is like number 3 and 2 but you portforward to a different port then port 80 on your router. So it looks like this:
From Router port 50000 -----Port Forward to-----> port 40000 on your linux server
Again 50000 and 40000, any valid port number would do (hopefully above 1024 and below the legal limit of 65535)
The effect is that to access your webserver/fileserver from the internet you would need to go to this website like this: http://ip-or-hostname:50000/
To access your webserver locally you would have to do it like this: http://ip-or-hostname:40000/
Also your 2 files /etc/apache2/ports.conf and /etc/apache2/sites-available/default  would need to be edited to use port 40000 instead of the default 80.

What if your Web Server happens to be right on the internet without behind a router, so your not behind a NAT - maybe you are the NAT, or there is no NAT?
So your ISP assigned you a public IP and you gave it to your webserver: Well you wouldnt have to do any config changes to apache, and you wouldnt have to port forward any port since your webserver is sitting touching the internet directly. So you would access everything like this http://ip-or-hostname/ without any ports. Of course -optionally- you can make your apache webserver listen on any other port and force it to use anything else, for example if you wanted to run a covert webserver with your server thats sitting on the internet on port 4444, just edit the 2 files to talk about 4444 instead of 80 and restart apache, no need for portforwading, and you can access everything on the web/fileserver like this: http://ip-or-hostname:4444/. Of course its very important to understand that you should have much security in place with firewalls etc on a server thats sitting directly attached to the internet, so make sure you let your firewall know that port 80 or whatever different port, if your doing that, needs access inbound to your apache2 server.

Note: In order to get the webserver/fileserver where you dont need to do the whole :PORT you need to use either method (1) or you would need to get a public IP on your webserver.

Install Instructions
----------------------

Log in to your Debian machine with root

apt-get update; apt-get -y install apache2 php5 php5-mysql mysql-server

Note for me apache started after it downloaded, I found out like this: "service apache2 status" or "service --status-all" and scrolling up and seeing a + sign next to apache2

Your now done with the install - if your case (1) or (2) from the "Networking PreSteps" section, your also done if your the case where your webserver has a public ip. Only continue with the next steps if you need to make your webserver listen in on a different port, because of the way you have your network(port forwards) setup.

cd /etc/apache2

For example if in the above case you are in case (3) from the "Networking PreSteps" section.
If you need your webserver to listen on a different port for HTTP (HTTPS is another topic, not covered in this simple article) then edit the next two files, and anywhere you see a reference to port 80 change it to port 50000

vim ports.conf
vim sites-available/default

service apache2 restart

Note: After editing any file in /etc/apache2, restart the apache2 server

Your done, you can start putting files and accessing them.

TEST
------
To test it open webbrowser to http://ip-or-hostname/ and if your using a different port, like for example port 50000, http://ip-or-hostname:50000/ and you should see a test website, something different then a failure message.
That test site is actually a file /var/www/index.html, deleting it and then going back to http://ip-or-hostname/ or http://ip-or-hostname:50000/ will show you the directory structure and some files.
Remember apache automatically loads any index.html or index.shtml or index.php. Also you can simply go to ip-or-hostname or ip-or-hostname:50000 on your webbrowser without the http, just make sure you dont use https.

Make it like a Fileserver
-----------------------------

To Make it feel like a webserver: 
rm /var/www/index.html
Deleting that file will bring up the directory folder structure look when you go to the file, instead of loading the webpage.

Make a file and try and download it:
echo "Test File!" > /var/www/file1.txt

Give files at least this permission for download and security:
chmod 644 /var/www/file1.txt

Or to set everything in that folder:
chmod -R 644 /var/www/*

Note of course you can implement subfolders and everything :-)

Subfolder Example:
-------------------

From Linux:
cd /var/www
mkdir temp
cd /var/www/temp
echo "This file is awesome" > file1.txt
chmod 644 /var/www/temp/file1.txt
chmod -R 644 /var/www/temp

From another PC:
http://ip-or-hostname/var/www/temp/file1.txt AND IT WILL DOWNLOAD

Or if you are using another port to listen on apache then use that 
http://ip-or-hostname:50000/var/www/temp/file1.txt and it will download

Download the file
-------------------
http://ip-or-hostname/file.txt
http://ip-or-hostname:50000/file.txt

Comments