How to serve any static directory containing web pages in django? - html

I'm working on an application where we want the following to happen:
The administrators upload zip archives containing web content (whole static websites) through admin interface
The archive is unzipped in the background to a random directory
The static content is served at a url that looks like http://<host>/site/<user-friendly-url>/, with the index.html at the root, all the paths to css and js files, etc.
I know how to configure the route, but I was wondering what to put in the view. I have tried with STATIC_{ROOT,URL} and MEDIA_{ROOT,URL} but I feel it's not the right way.
In summary : is there a way to put something like return serve_this_static_directory() in the view? or is there any workaround there?
Thanks for your answers
edit:
Trying to clarify my question.
The url (http://<host>/site/<user-friendly-url>/) doesn't match the directory name (which could be anything, like XYZ_html/).
Administrators must be able to upload new archives, which should be extracted and served automatically.
For those two reasons, I don't have any idea how to serve this content using the webserver, because how is it possible to configure the routes then?

First you must realize that django won't serve your static or media files by default, you should use your HTTP server (nginx, apache or other) for that.
Next thing to do is to configure your HTTP server to serve "/path/to/your/project/public" and any files inside on root URL of your domain, and instead of serving 404 error if file is not found, it should redirect to django (so if there is some file in path /site/something/ your HTTP server should serve that file, if not, it shoudl serve anything that django will output on that path. Sample configuration for nginx might look like this:
server {
listen 80;
server_name oskar.local;
root /path/to/your/project/public;
location #default {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
include /etc/nginx/uwsgi_params;
uwsgi_pass app_server; # or some proxy_pass if you're not using uwsgi
break;
}
location / {
try_files $uri #default;
}
}
And last step is to configure your django app's MEDIA_* settings:
MEDIA_ROOT = "/path/to/your/project/public"
MEDIA_URL = "/"
But be careful! With that approach there is potential vulnerability: any user can put files into path that is normally handled by django. Better approach will be to put all of "sub-sites" inside some sub-directory or into separate domain.

Related

Handle subdomains with Yii2 (LAMP)

I'm developing using php and Yii2, apache2 and ubuntu.
So we have domain such as domain.com (as an example). Now I need create some pages as page1.domain.com as landings, but they will be created automatically?
How should I setup vhost for this one? I don't know what subdomain name will be (can be any a-Z0-9). Should I create new vhost and enable it or I can somehow enable it on existing one?
Next problem is URL on page1.domain.com When we open page1.domain.com all links are like "http://page1.domain.com/xxx" but I need "http://domain.com/xxx"
So main thing is to change base url no matter what domain but reacts on it's subdomain if exist.
Two things there:
1) Active subdomain wildcard
In your vhosts config file, add:
ServerAlias *.example.com
Then you have to add an entry in your DNS. For localhost, you can't add it directly in your hosts file:
https://serverfault.com/questions/319518/add-wildcard-subdomains-to-localhost-with-apache
2) Create the rules
You could add a pattern in your url manager to match the subdomains to a controller handler.
'http://(\d+).domain.com/' => 'controller/index',
More info:
[1] http://www.yiiframework.com/doc-2.0/guide-runtime-routing.html#rules-with-server-names
[2] http://www.yiiframework.com/doc-2.0/guide-runtime-routing.html

LAMP - make directory accessible only to certain web pages

I am tinkering around with webpages on a LAMP server running Apache2 and was wondering if it was possible to make a directory accessible only to your web pages and not from outside?
Example scenario:
Directory to protect: dir1 containing images (jpg, png)
My own webpage: mypage.html that calls images from dir1
My website: www.myweb.com that contains both dir1 and mypage.html
Currently, files inside the website can be accessed via www.myweb.com/dir1/somefile.jpg or by calling mypage.html
I would like it to only be accessible by calling mypage.html
I have tried the following:
modifying .htaccess to disallow access of image types
<files "*.jpg">
deny from all
</files>
(doesn't work because mypage.html cannot access it either)
Modify apache2 conf file with:
<Directory /var/www/dir1>
AllowOverride None
<Limit GET POST OPTIONS>
Order deny,allow
Deny from all
</Limit>
</Directory>
(this actually semi-worked as it allowed me to write to directory but not read, maybe this can be modified to allow requests coming from internal web pages to go through?)
I guess to conclude, is there a way to get Apache2 to ONLY accept requests to access a directory if it is of a certain url of your choosing?
Thanks in advance.
So, I've decided that the approaches I've taken so far really don't cut it and found you could actually call a php function where
<img src='somefile.php?query=xxx' alt='pic'>
and where in the somefile.php I have that takes in img file name created from the query above.
echo file_get_contents($imgresource);
By serving the image from a php script and blocking this php script from being called without proper credentials, sessions, cookies and IP blocking, there is some security set.
So I guess it doesn't really answer the question in its entirety of blocking access only to some URLs but it works for the purpose of not being able to be accessed externally since I have buried the directory below (or above?) the web root directory where it can't be called from a url and only from internal script.

Photos are not showing up when using No-IP ddns and linux apache

I don't have a static IP address so a person recommended that I use no-ip.com
I did and now my website is accessible from external internet yay
The problem now is that my photos aren't showing, I thought this would have been a simple issue
I'm not sure if I have to do something with Virtual Host or...
My domain name is greenace92.ddns.net
I can access my index.html file, that shows up fine but the photos aren't showing
On my httpd.conf file I have
ServerRoot "/usr/local/apache2"
The html files are in htdocs under apache2
So, on virtual host that httpd-vhosts.conf file I have the following
ServerAdmin "jacobcun#ricekidengineer.com"
DocumentRoot "/usr/local/apache2/"
ServerName "www.greenace92.ddns.net"
*ricekidengineer.com is my other domain with email configured
I didn't change the error logs from stuck "dummy" stuff
So on my html file for image href's I have
http://www.greenace92.ddns.net/htdocs/images/normalbus-icon-on.png
I assume that www.greenace92.ddns.net is /usr/local/apache2
So I'm just continuing the directory location
Is that correct? What is wrong?
Yeah I don't understand, I even tried three different directories for a few photos, trying directly in apache2, then htdocs, then the images folder
What am I not seeing?
You will have to decide what your want as the domain for your site:
www.greenace92.ddns.net is not setup for your web site
greenace92.ddns.net is the domain setup for your web site.
You either have to
configure no-ip.com to point to the domain www.greenace92.ddns.net
(or)
change your apache ServerName directive to
ServerName "greenace92.ddns.net"
and change the image URLs in your HTML pages from something like
http://www.greenace92.ddns.net/htdocs/images/normalbus-icon-on.png
to
http://greenace92.ddns.net/images/normalbus-icon-on.png
Note: With your current configuration, the 2nd URL mentioned above should already work (It worked when I tested!).

Broken links in hg serve behind nginx via proxy_pass

I have a simple Ubuntu 10.10, Nginx, Mercurial, PHP, and MySQL stack.
Current file structure:
/opt/nginx/html - document root for nginx (where my website files are)
/opt/nginx/html/.hg - repository data
/usr/share/mercurial/ - where the templates/static folder is
I am running hg -serve which is hosted at localhost:8000.
I do not have a clear understanding of how mercurial is linked to its resources after this point.
I have the following Nginx configuration for my desired entry point:
location /mercurial/ {
auth_basic "Authentication";
auth_basic_user_file /opt/nginx/html/.hg/file.password;
proxy_pass http://localhost:8000/;
}
This works fine except for fully loading the Mercurial Instance after authenticating. I get the page i expect to see but the /static references are all broken, and the links I click on such as one of the changesets is broken as well (they are not found or take me back to the broken main page). It tries to find these files at my-ip-address/static/... which is incorrect.
The mercurial ui directory is outside of my document root /opt/nginx/html.
The path of where /static actually is: /usr/share/mercurial/templates
Is this the problem?? That the files are outside of my nginx document root?
When mercurial runs where is /rev and how can i make sure that mercurial runs correctly with the correct resource URLs?
Note: Just going to my-ip-address:8000 works 100%. Nothing is broken and no css or js is missing. The problem is happening with regards to the proxy_pass.
How can I have these files properly linked up? Is a rewrite required?
The problem is that by default, the web server launched with hg serve doesn't add any prefix to the URLs it generates. It has no way to know that you're NGinx proxy pass request for the /mercurial location.
You must set the prefix to use with the --prefix option. hg help serve tells us this about the option :
--prefix PREFIX prefix path to serve from (default: server root)
So just run the server this way : hg serve --prefix mercurial and everything should be fine ! (don't forget to remove your alias...)
If you prefer, the prefix option can also be set in the configuration file if you have one :
[web]
prefix = mercurial
Soultion: I needed to add the following to my NGINX config file;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

AWS page just showing Apache Start page and not html file

I'm running an apache server on an Amazon EC2 basic linux instance, and I'm trying to get it to just display a simple test html file that I created. The file is in /home/ec2-user/public_html (just for test purposes). I've changed the DocumentRoot and Directory to point to this directory, but the public dns address is still just showing the default Apache start page. What else do I need to do/change to get this to actually display online?
Edit: I've also uncommented the VirtualHost *:80 line, changed the DocumentRoot and ServerName within the VirtualHost tags.
It turns out I had forgotten to change some permissions on the test.html file itself and that was causing a holdup.
check running instance's security group HTTP rule. ADD rule if not added:
Select HTTP from the Type list, and leave the source as Anywhere (0.0.0.0/0).