Django CMS not working with uWSGI - django

I have website developed in Django CMS.
When running it with the manage.py runserver it works just fine, but when I try to run it with nginx and uwsgi I get the following error:
view must be a callable or a list/tuple in the case of include().
I think it might the problem might be that I run the django cms in a virtualenv.
I used the same conf files for another django project but it wasn't inside an virtualenv
I start the uwsgi inside my virtualenv.
My wsgi.ini:
[uwsgi]
chdir = /var/www/user.name/mysite
module = mysite.wsgi
#home = /var/www/user
master = true
processes = 10
socket = /var/www/user/mysite/mysite.sock
chmod-socket = 666
vcuum = true
My nginx conf
upstream django {
server unix:///var/www/user/mysite/mysite.sock;
}
server{
# listen on port
listen 80 default_server;
listen [::]:80 default_server;
server_name example.name www.example.name;
return 301 https://$server_name$request_uri;
}
server{
#Default server?
listen 443 ssl http2 default_server;
listen [::]:443 ssl http2 default_server;
server_name example.name www.example.name;
charset utf-8;
client_max_body_size 75M;
ssl_certificate /etc/letsencrypt/live/user/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/user/privkey.pem;
include snippets/ssl-params.conf;
location ^~ /.well-known{
allow all;
alias /var/www/user/.well-known;
}
location /static {
alias /var/www/user/mysite/mysite/static;
}
location / {
uwsgi_pass django;
include /var/www/user/mysite/uwsgi_params;
}
}

You need to add virtual environment path to wsgi.ini, something similar to:
home = /var/www/myvenv
But I don't know if this will solve it

Related

Hosting two different django project in the same droplet with different subdomains (NGINX, Gunicorn, ubuntu)

As the title says I want to host two different django project in the same droplet (NGINX, Gunicorn, ubuntu) with different subdomains. One will be our main site example.com. which is up and running and working perfectly. We want to host the staging site staging.example.com in the same droplet.
We have created new sockets and service files for the staging site and activated and enabled them but the issue is nginx still points to the files in main domain directory rather than the staging directory and hence we get this error below even though these domains have been added in the allowed hosts of settings.py of the staging site
DisallowedHost at /
Invalid HTTP_HOST header: 'staging.example.com'. You may need to add 'staging.example.com' to ALLOWED_HOSTS
Here is our staging.guinicorn.service file
[Unit]
Description=staging.gunicorn daemon
Requires=staging.gunicorn.socket
After=network.target
[Service]
User=admin
Group=www-data
WorkingDirectory=/home/admin/example1staging
ExecStart=/home/admin/example1staging/venv/bin/gunicorn --access-logfile - --workers 3 --bind unix:/run/staging.gunicorn.sock djangoproject.wsgi:application
[Install]
WantedBy=multi-user.target
Here is our staging.guicorn.socket file
[Unit]
Description=staging.gunicorn socket
[Socket]
ListenStream=/run/staging.gunicorn.sock
[Install]
WantedBy=sockets.target
Lastly here is our nginx config
server {
listen 80;
listen [::]:80;
server_name example.com www.example.com;
return 302 https://$server_name$request_uri;
}
server {
# SSL configuration
listen 443 ssl http2;
listen [::]:443 ssl http2;
ssl_certificate /etc/ssl/cert.pem;
ssl_certificate_key /etc/ssl/key.pem;
ssl_client_certificate /etc/ssl/cloudflare.crt;
ssl_verify_client on;
root /var/www/html;
index index.html index.htm index.nginx-debian.html;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/admin/example1;
}
location / {
include proxy_params;
proxy_pass http://unix:/run/gunicorn.sock;
}
}
server {
listen 80;
listen [::]:80;
server_name staging.example.com www.staging.example.com;
return 302 https://$server_name$request_uri;
}
server {
# SSL configuration
listen 443 ssl http2;
listen [::]:443 ssl http2;
ssl_certificate /etc/ssl/cert.pem;
ssl_certificate_key /etc/ssl/key.pem;
ssl_client_certificate /etc/ssl/cloudflare.crt;
ssl_verify_client on;
root /var/www/html;
index index.html index.htm index.nginx-debian.html;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/admin/example1staging;
}
location / {
include proxy_params;
proxy_pass http://unix:/run/staging.gunicorn.sock;
}
}
Some help here would be extremely welcome.

Django server not opening with 0.0.0.0:8000

I have a AWS server runs on Nginx and which hosts a React application working fine on server.
Now I want a Django app for restframework to be available on the same server.
Iam following the Document and uploaded the Django app on the server and try to run the app by trying python3 manage.py runserver 0.0.0.0:8000. There is no error but I cannot access my ip with http://server_domain_or_IP:8000.
Nginx config at /etc/nginx/conf.d/example.org.conf
server {
listen 80 default_server;
server_name example.org;
return 301 https://example.org;
}
server {
listen 443 ssl;
server_name example.org;
ssl_certificate /etc/nginx/ssl/ssl_bundle.crt;
ssl_certificate_key /etc/nginx/ssl/example.key;
location / {
root /home/ubuntu/example/build;
index index.html index.htm;
}
}
Please help where am I going wrong?
So your Django is running on localhost:8000 and in your Nginx setup, you are not forwarding your request to that address. You have to match the domain request with your Django running server, In production, you should have something like Gunicorn
Try with -
upstream backend {
server localhost:8000;
}
server {
server_name example.com www.example.com;
location / {
include proxy_params;
proxy_pass http://backend;
}
this would work for non-https. You can add listen 443 ssl, and SSL certificates to make your server block listen only to HTTPS.

Accessing via www. will show Nginx welcome page [+ Gunicorn + Django]

Nginx + Ubuntu 18.04 + Django 2.2.10
Accessing directly via "www.examples.com" will show nginx welcome page, but accessing anything else--"examples.com", "https://examples.com", "https://www.examples.com"--will work as expected.
On DigitalOcean, I have two A-type records [www.examples.com, examples.com] directing to the IP address--I believe they are correctly set up.
On my Django project, I have ALLOWED_HOSTS = ['localhost', 'examples.com', '137.68.49.136', 'www.examples.com'] set.
Here is my /etc/nginx/sites-available/project:
server {
server_name examples.com www.examples.com;
charset UTF-8;
error_log /home/jay/eco/nginx-error.log;
location = /favicon.ico { access_log off; log_not_found off; }
location /static {
alias /home/jay/eco/static;
}
location /media/ {
alias /home/jay/eco/media/;
}
location / {
include proxy_params;
proxy_pass http://unix:/run/gunicorn.sock;
}
listen [::]:443 ssl ipv6only=on; # managed by Certbot
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/examples.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/examples.com/privkey.pem; # managed by Certb$
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
server {
if ($host = examples.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80;
listen [::]:80;
server_name examples.com;
return 404; # managed by Certbot
}
I believed this was everything but apparently not. What am I missing?
You have two server blocks, the first processes requests using the https protocol and the second processes requests using the http protocol. There is also a default server block in some other file, which responds with the Nginx welcome page.
The second server block in your question only processes requests for http://example.com. You need to add www.example.com to the server_name directive, and update the logic so that both domain names are redirected to the https service.
For example:
server {
listen 80;
listen [::]:80;
server_name example.com www.example.com;
return 301 https://$host$request_uri;
}
See this document for details.

403 Forbidden error when registering custom domain for my django website

I am new to Django,I was following a tutorial on how to deploy a site using ngnix ubuntu and gunicorn,I bought a domain at namecheap and the site is hosted by LinodeBut whenever.Every step was succesful,no errors when I checked for gunicorn status,he server also restarts succesfully ,but I when I visit the domain I get a 403forbiden error.
I have checked out for similar problems but none of them are of help
settings.py
ALLOWED_HOSTS = ['www.devbrian.com',]
sudo nano /etc/nginx/sites-available/blog
server {
listen 80;
server_name wwww.devbrian.com;
location = /favicon.ico {access_log off;log_not_found off;}
client_max_body_size 100M;
location /static/ {
root /home/brian/blog;
}
location /media/ {
root /home/brian/blog;
}
location / {
include proxy_params;
proxy_pass http://unix:/home/brian/blog.sock;
}
}
sudo nano /etc/nginx/sites-available/default
#
server {
listen 80 default_server;
listen [::]:80 default_server;
# SSL configuration
#
# listen 443 ssl default_server;
# listen [::]:443 ssl default_server;
#
# Note: You should disable gzip for SSL traffic.
# See: https://bugs.debian.org/773332
#
# Read up on ssl_ciphers to ensure a secure configuration.
# See: https://bugs.debian.org/765782
#
# Self signed certs generated by the ssl-cert package
# Don't use them in a production server!
#
# include snippets/snakeoil.conf;
root /var/www/html;
# Add index.php to the list if you are using PHP
}

letsencrypt standalone certification not showing up on GET request

I was able to create certifications under the path /etc/letsencrypt/{{mywebdomain}}/ and set under my server where the ssl_cert and ssl_cert_key absolute pathfile locations for my nginx server.
When I run the command sudo nginx -t I receive a successful configuration output and the nginx server is running in the reverse proxy for Django without any problems. But when I access the root of my website on my chrome browser, I'm receiving the "http://website.com" instead of "https://website.com".
Please point me in the right direction if anyone was able to correctly encrypt their domain content with gunicorn-django-nginx configuration.
My website snippet conf:
upstream app_server {
unix:/home/me/Documents/masterdomain/src/portfolio_revamp.sock;
}
server {
client_max_body_size 4M;
listen 80;
listen [::]:80;
listen 443 ssl;
listen [::]:443 ssl;
listen www.mysite.com:80;
server_name example.com www.example.com;
http://example.com;
ssl_certificate
/etc/letsencrypt/live/mysite.com/fullchain.pem;
ssl_certificate_key
/etc/letsencrypt/live/mysite.com/privkey.pem;
root /home/akeem/Documents/SpencerMaster/src;
index templates/home.html templates/main.html;
location / {
proxy_pass
http://unix:/home/me/Documents/masterdomain/src/portfolio_revamp.sock;
alias /home/me/Documents/master/templates/home.html;
}
location ~ /.well-known {
allow all;
}
location /static {
autoindex on;
alias /home/me/Documents/masterdomain/static;
}
location /media {
autoindex on;
alias /home/me/Documents/masterdomain/media;
}
}
I'm running a xenial ubuntu 16.04 server if that makes a difference.
I believe the issue is that you aren't redirecting to HTTPS - unless you specifically enter https://example.com, you'll be directed to the standard http://example.com.
I use this guide from DigitalOcean which recommends two server blocks. In your case it will look something like:
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name mysite.com www.mysite.com;
return 301 https://$server_name$request_uri;
}
server {
client_max_body_size 4M;
listen 443 ssl default_server;
listen [::]:443 ssl default_server;
No server_name required here
... Everything else ...
}