How to configure lighttpd to serve django static files? - django

I have a web application made with Django 2.0.5, installed in a virtual environment on Debian 9, on which I installed Gunicorn and lighttpd.
I installed my web app in /opt/djangoproject/mywebapp.
I configured lighttpd adding these lines to my lighttpd.conf
$HTTP["url"] !~ "/static" {
proxy.server = ( "" => ( (
"host" => "192.168.1.15",
"port" => 8001
) ) )
}
alias.url = ( "/static/" => "/opt/djangoproject/mywebapp/mystaticfiles" )
The problem is that the alias only work on port 80 instead of to work on 8001.
Update: I forgot to specify that:
Gunicorn run only on 192.168.1.15:8001 (with this static ip)
Lighttpd run on port 80 (default port)
Debian is installed on a server I have to reach from every device connected on the same network

Based on the description, it sounds like lighttpd is running on port 80 and gunicorn on port 8001. If gunicorn is creating content that refers to port 8001, then the client will connect directly to gunicorn (if gunicorn is listening on *:8001 instead of 127.0.0.1:8001) and will bypass lighttpd, so lighttpd never sees it. You should prefer to use root-relative links (starting with / in the url-path).

I understood where the problem was. The configuration was right, but I forgot to include "mod_proxy" in server.modules.
After adding the module (and restart lighttpd) everything worked.
I hope it will be useful to someone.

Related

How to setup django with lighttpd?

I have a live website that is running under lighttpd. I now have a sub-domain that I want to run under django. I do not want to move to Apache as I don't currently have the resources to support two production web servers. The django part is actually working well with the django builtin development server so now I just need to port it into production.
It seems that all documentation to setup django with lighttpd is over 10 years old and uses fastcgi that from what I can see was deprecated and removed from django. The new docs point to use wscgi - scgi under lighttpd. But even that is being hard to get any documentation.
Do I need to run uwsgi and use that to serve the pages to mod_scgi? Or should I use gunicorn? I am sort of at a loss on what to do.
Suggestions welcome!
lighttpd supports uwsgi with mod_scgi and scgi.protocol = "uwsgi"
scgi.protocol = "uwsgi"
scgi.server = ( "/" =>
(
(
"socket" => "/tmp/scgi.sock",
"check-local" => "disable",
"fix-root-scriptname" => "enable"
)
)
)
Using mod_scgi and the uwsgi protocol to communicate with the Django server will likely be faster and use fewer resources than using gunicorn (and communicating using lighttpd mod_proxy)
How to use Django with uWSGI
The uWSGI docs linked there recommend configuring the web server to serve static files rather than sending those requests to Django.
There are a few different options you can consider for serving a Django application under Lighttpd. One popular option is to use mod_proxy to forward requests to a separate process running your Django application, such as Gunicorn or uWSGI.
Gunicorn can be easily integrated with Lighttpd using mod_proxy. You can run Gunicorn in the background as a daemon, and configure Lighttpd to forward requests to it using mod_proxy
uWSGI can also be integrated with Lighttpd using mod_proxy and the main advantage of uWSGI over Gunicorn is its scalability and performance, but it can be more complex to set up.
And if you are new to this, I would recommend trying to set up Gunicorn first as it is the simpler option, and then moving on to uWSGI if you need more performance!
UPDATE:
As you mentioned in the comment, you want to use mod_fastcgi to connect Lighttpd to your Django application, so you will need to start Django using the FastCGI process manager provided by flup.
How to start your Django app using flup:
#!/bin/bash
# activate your virtual environment
source /path/to/venv/bin/activate
# Start the FastCGI process manager
python -m flup.server.fcgi -d -m django.core.servers.fastcgi
You will also need to configure Lighttpd to use mod_fastcgi and point it to the socket created by flup in order to connect to the Django application
fastcgi.server = ( "/" =>
( "localhost" =>
(
"socket" => "/tmp/fcgi.sock",
"check-local" => "disable",
)
)
)

How to serve Flask app on different port than main site running on port 80?

Read through the other threads, but none seemed to address my need.
I have a website running fine on default port 80/443 using Apache on CentOS 7.
I have now installed gunicorn and copied my files to a /api subdirectory on the server. I am able to start gunicorn successfully using "gunicorn -b 0.0.0.0:8000 api:app"; however, I don't seem to be able to browse to https://example.com:8000 (site can't be reached).
Do I need to use a VirtualHost's file specifically for the Flask /api directory? If so, what would that look like without disrupting the main site?
EDIT: Note this is a Flask-RESTful API, no static files or anything on the Flask side. Just endpoints/routes to serve.

How to host a django website in my pc and view it in another pc?

I have a django project in my pc.
In terminal I've run python3 manage.py runserver <my ipaddress>:8001
When I try to open the link in another pc, it is showing error page which says:
Invalid HTTP_HOST header: '<my ipaddress>:8001'. You may need to add '<my ipaddress>' to ALLOWED_HOSTS.
What should I do?
And moreover is it possible to put some text in place of ipaddress in the url?
For example, I want to host it as myproject/ instead of that complex url.
On one condition this will work
if both computers are on the same network like local Hotspot or same
LAN network
steps:
add '*' in your django projects's setting file in Allowed Host it will look like
ALLOWED_HOSTS = ['*']
run your server on this ip 0.0.0.0 and port any like 8000 using this command
manage.py runserver 0.0.0.0:8000
run ifconfig if you are using linux ipconfig if windows then you will get your ip address of your server
Open browser in another computer and enter the ip of server shown in 3rd step with port as 8000
http://ip-of-server:8000
Instead of passing <my-ip-address> to the runserver command, pass 0.0.0.0.
If both the machines are in the same network you can run the application on 0.0.0.0 IP address (refers to all IPv4 addresses on the local machine). Refer this link wiki 0.0.0.0 for more details. So, on application server run this:
manage.py runserver 0.0.0.0:8001
Now, from the other machines, access it using http://youripaddresss:8001 , where < youripaddress > is the actual ip address of your machine.
If both computers are not on the same network (local hotspot or LAN network)
You can use ngrok to view whatever is running on your localhost from any device
Using ngrok to view your django project from any device
follow the steps below:
Add '*' in your django projects's setting file in Allowed Hosts:
ALLOWED_HOSTS = ['*']
Download ngrok from the official website
Unzip the downloaded file and then go to the directory where the ngrok file is located via your terminal
Then type the command:
ngrok http 8000
or
./ngrok http 8000
Now you can open the url generated by ngrok on any device to view what is running on your pc:
https://randomly_generated_subdomain.ngrok.io
PS: this can also be used for any webserver running locally, not just django site
ngrok is a great tool that can be used to:
Run personal cloud services from your home
Demo websites without deploying
Build webhook consumers on your dev machine
Test mobile apps connected to your locally running backend
Stable addresses for your connected devices that are deployed in the field
Run your server with this command python manage.py runserver 0.0.0.0:8000
Then you can access your website from any system connected to your wifi with an URL like 192.168.0.1:8000
You may get your IP Address using this command in CMD ipconfig in windows and ifconfig in mac
Also add your IP to ALLOWED_HOSTS in setting.py
ALLOWED_HOST = ['*'] in your settings.py
python manage.py runserver 0.0.0.0:8000
then make sure your machine firewall allows incoming and outgoing traffic.
I use linux machine so from control center go to firewall and allow both incoming and outgoing.
then on your local network machine.
:8000
This is it !
worked for me

How can I change django runserver url?

I'm trying to change django project url, so that users, who wants to connect to website in local area network will see url instead of localhost:8000 or 127.0.0.1. I need to change localhost:8000/users/board to be http://example.eu. I've tried to python manage.py runserver http://example.euand then thought about changing url's but it doesn't work. Also tried to change hosts file(Windows OS), but as far as I understand I need real ip address not url. Is it possible to do this thing and how?
You can use python manage.py runserver 0.0.0.0:8000. 0.0.0.0 means all IPv4 addresses on the local machine. So the server can be reachable by 127.0.0.1 and your private ip address like 10.10.5.8. So now others can access the server using http://10.10.5.8:8000. You the runserver on port 80, so that port can be removed from the url (by default is 80).
But to use any domain instead of ip, you have to change the hosts file of all the clients using the server to add domain to ip address mapping. Alternatively you can configure local network server to map the particular url to your system ip.
Run a local domain with the same port
Opening the /etc/hosts file on your mac with
sudo nano /etc/hosts
And for windows I believe you need to open:
C:\Windows\System32\Drivers\etc\hosts
In here add the domains you want, for example I added vazkir.com
....
127.0.0.1 localhost
127.0.0.1 vazkirtje.com
127.0.0.1 www.vazkirtje.com
.....
Lastly you can add it the domain to you ALLOW_HOSTS in your settings.py:
ALLOWED_HOSTS = ["localhost", "127.0.0.1", "vazkirtje.com", "www.vazkirtje.com"]
And now you can visit your Django application (with port 8000) at:
http://vazkirtje.com:8000
The solution is not perfect since you do have to specify the port you are using, but this does let you use a domain for your local Django application in a relatively easy manner;)
Run a local non-existent domain without specifying the port
If you do want to run it without specifying the port; so just vazkirtje.com then you can use port 80, because this is the default port for HTTP.
Make sure the domain you are testing does not exist, since the domain lookup will first be done on existing domains. So check if you get a similar message to the one I got on chrome, when visiting the url:
Now you can specify this port by adding port "80" to the "runserver" command. You only do need to use "sudo" to run the command at this port, since you need admin rights for this. So run:
sudo python manage.py runserver 80
And now you should be able to access your Django application by visiting:
http://vazkirtje.com

Why does Gunicorn use port 8000/8001 instead of 80?

I busy setting up a development environment for Django Framework using Gunicorn (as Django service) and NGINX (as a Reverse Proxy).
When I look at several tutorials like this one and this one, I see that they use port 8000 and port 8001 (http://127.0.0.1:8000 and http://127.0.0.1:8001). Is there a special reason not to use port 80, like any other webserver?
Port 8000 is often used for radio streaming and malware, so why?
BTW: I am running it using Virtualenv on a Ubuntu 12.04 system.
All ports under 1024 are privileged ports. To bind to a privileged port requires root user permissions and typically you don't want to run gunicorn with root level permissions.
What's done instead is to allow nginx to bind to 127.0.0.1:80 and then proxy requests to port 80 to a non-privileged port like 8000 using an nginx configuration like:
server {
location / {
proxy_pass http://127.0.0.1:8000;
}
}
NGINX listens on port 80 and forwards to Gunicorn. Gunicorn operates on the 127.0.0.1 IP rather than 0.0.0.0, so it isn't listening publicly, and therefore the only way to access the site externally is through port 80.