Apache mod_wsgi better configuration - django

I am deploying my django-based web apps using apache mod_wsgi. This is my virtualhost:
<VirtualHost _default_:*>
ServerAdmin my_email#emails.com
DocumentRoot /var/www/appWSGI/gestioner/gestioner/
Alias /static /var/www/appWSGI/gestioner/static/
<Directory /var/www/appWSGI/gestioner/>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess gestioner python-path=/var/www/appWSGI/gestioner python-home=/var/www/appWSGI/env
WSGIProcessGroup gestioner
WSGIApplicationGroup %{GLOBAL}
WSGIScriptAlias / /var/www/appWSGI/gestioner/gestioner/wsgi.py
WSGIPassAuthorization On
</VirtualHost>
This basic configuration is working fine. I would like to know if it is possible to improve this maybe there are other directives that i don't know about..
It is possible to have another configuration that boots performance ?
thank you all in advance!!

import os
from django.core.wsgi import get_wsgi_application
import sys
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "your_project.settings")
application = get_wsgi_application()
If you are using python2
sudo apt-get install python-pip apache2 libapache2-mod-wsgi
If your are using python3
sudo apt-get install python3-pip apache2 libapache2-mod-wsgi-py3
Then Here goes the apache configuration
<VirtualHost *:80>
ServerName dev.example.com #your server name
ServerAlias dev.example.com #your server alias
DocumentRoot #your document root
WSGIProcessGroup dev.example.com
WSGIPassAuthorization On
WSGIDaemonProcess dev.example.com python-home=/home/robert/django/robertenv python-path=/home/robert/django/
WSGIScriptAlias /backend /home/robert/django/project/wsgi.py process-group=dev.example.com
Alias "/uploads" "/home/robert/django/uploads" #uploads directory
<Directory #your document root>
Require all granted
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^(.*) /index.html [NC,L]
</Directory>
<Directory /home/robert/django/uploads>
Require all granted
</Directory>
<Directory /home/robert/django/project>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
Source apache2 wsgi con and source1

Related

Apache not serving WSGI file

I have been deploying a flask application. I have both a .cgi file and a .wsgi file in the app directory because the server is failing to serve the wsgi app.
Here is my apache config:
<VirtualHost 172.93.54.122:80>
ServerName wedding.bentsea.net
ServerAlias www.wedding.bentsea.net
ServerAdmin webmaster#wedding.bentsea.net
DocumentRoot /home/wedding/public_html
UseCanonicalName Off
ScriptAlias /cgi-bin/ /home/wedding/public_html/cgi-bin/
WSGIDaemonProcess wedding processes=2 threads=15 display-name=%{GROUP}
WSGIProcessGroup wedding
WSGIScriptReloading On
WSGIScriptAlias / /home/wedding/public_html/cgi-bin/wedding.wsgi
CustomLog /usr/local/apache/domlogs/wedding.bentsea.net.bytes bytes
CustomLog /usr/local/apache/domlogs/wedding.bentsea.net.log combined
ErrorLog /usr/local/apache/domlogs/wedding.bentsea.net.error.log
# Custom settings are loaded below this line (if any exist)
IncludeOptional "/usr/local/apache/conf/userdata/wedding/wedding.bentsea.net/*.conf"
<IfModule mod_setenvif.c>
SetEnvIf X-Forwarded-Proto "^https$" HTTPS=on
</IfModule>
<IfModule mod_userdir.c>
UserDir disabled
UserDir enabled wedding
</IfModule>
<IfModule mod_suexec.c>
SuexecUserGroup wedding wedding
</IfModule>
<IfModule mod_suphp.c>
suPHP_UserGroup wedding wedding
suPHP_ConfigPath /home/wedding
</IfModule>
<IfModule mod_ruid2.c>
RMode config
RUidGid wedding wedding
</IfModule>
<IfModule itk.c>
AssignUserID wedding wedding
</IfModule>
<Directory "/home/wedding/public_html">
WSGIProcessGroup wedding
WSGIApplicationGroup %{GLOBAL}
Order deny,allow
Allow from all
</Directory>
</VirtualHost>
Here is my app.cgi file:
#!/home/wedding/venv/bin/python
from wsgiref.handlers import CGIHandler
from myapp import app
import os
os.environ['SCRIPT_NAME']=''
CGIHandler().run(app)
Here is my wedding.wsgi file:
#!/home/wedding/venv/bin/python
import sys,os
import logging
logging.basicConfig(stream=sys.stderr)
path = "/home/wedding/public_html/cgi-bin/"
if path not in sys.path:
sys.path.append(path)
from wedapp import app as application
I have them configured to serve different apps for testing purposes, one of them being a simple hello world.
Despite this configuration, my server is processing the .htaccess file in my public_html directory which serves the app.cgi file. Any ideas what I'm doing wrong?

Ubuntu Apache taking long time to respond and getting This site can’t be reached

Hi Guys I am trying to route my domain to server 139.5X.X.XXX
Following is My DNS record Details in Hostinger :-
Type Name Priority Content IP-V4 TTL
A www 0 139.5X.X.XXX 600
A # 0 139.5X.X.XXX 14400
Now I am seeing default apache page while browsing the domain (Server serving default apache page (Digital Ocean Ubuntu Droplet)) .
But after configuring a Django service to domain it taking too long to respond and ending up with "This Site can't be Reached"
following is the conf file which I am using
<VirtualHost *:80>
ServerName tellie.in
ServerAlias www.tellie.in
Redirect permanent / https://tellie.in/
RewriteEngine on
RewriteCond %{SERVER_NAME} =tellie.in [OR]
RewriteCond %{SERVER_NAME} =www.tellie.in
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
<VirtualHost *:443>
ServerAdmin admin#tellie.in
ServerName tellie.in
ServerAlias www.tellie.in
DocumentRoot /home/srv/telli
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
Alias /static /home/srv/telli/telli/static
<Directory /home/srv/telli/telli/static>
Options FollowSymLinks
AllowOverride None
Require all granted
</Directory>
Alias /media /home/srv/telli/telli/media
<Directory /home/srv/telli/telli/media>
Options FollowSymLinks
AllowOverride None
Require all granted
</Directory>
<Directory /home/srv/telli/telli/telli>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess telli python-path=/home/srv/telli/telli python-home=/home/srv/telli/venv
WSGIProcessGroup telli
WSGIScriptAlias / /home/srv/telli/telli/telli/wsgi.py
Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/tellie.in/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/tellie.in/privkey.pem
</VirtualHost>
Is there anything wrong that could cause the problem I am facing
Make sure that Apache has rx access to the directories under /home/srv/, It is better to move the code out of HOME and in a general mountpoint as /var or /data

Apache2 Django NameError: name "TypeError" is not defined

I am trying to run a django application on VPS via apache2, but I get the following in the website-error file, also 400(Bad Request):
Exception ignored in: <function Local.__del__ at 0x7f47273f48b0>
Traceback (most recent call last):
File "/usr/local/lib/python3.8/dist-packages/asgiref/local.py", line 96, in __del__
NameError: name 'TypeError' is not defined
I successfully ran a simple website that was made with "django-admin startproject" and could be viewed, but uploading project, made with the following skeleton, produces this error: https://django-project-skeleton.readthedocs.io/en/latest/apache2_vhost.html
I have tried including the python site-packages in the WSGIDaemon and by exluding them it produces the same effect.
In addition to this, I have also added:
<Directory /var/www/mysite/mysite>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
But again, no change
Because this post was upvoted and maybe it can help the others searching for that problem: this is my website.conf, just change example to your domain name. It also redirects to your https version of the website:
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/example
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]
</VirtualHost>
<VirtualHost *:443>
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/example
# Insert the full path to the wsgi.py-file here
WSGIScriptAlias / /var/www/example/example/wsgi.py
Alias /static/ /var/www/example/run/static/
Alias /media/ /var/www/example/run/media/
WSGIDaemonProcess example python-path=/var/www/example
# PROCESS_GROUP specifies a distinct name for the process group
WSGIProcessGroup example
<Directory /var/www/example/example>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
<Directory /var/www/example/run/static>
Require all granted
</Directory>
<Directory /var/www/example/run/media>
Require all granted
</Directory>
LogLevel info
ErrorLog ${APACHE_LOG_DIR}/example_error.log
CustomLog ${APACHE_LOG_DIR}/example_access.log combined
SSLEngine on
SSLCertificateFile /var/ssl/example.crt
SSLCertificateKeyFile /var/ssl/example.key
SSLCertificateChainFile /var/ssl/example.ca-bundle
</VirtualHost>
Open your apache configuration's file then add "LogLevel info" inside the tag : VirtualHost at the end (before closing tag). It's works for me
I've encountered the same (ignored) exception in my apache2 error logs while running a functional application. It seems like you're using WSGI, so this ASGI-related python package shouldn't be the cause of your 400 error.

mod_wsgi apache and django configuration not working

*Using Django 1.5, mod_WSGI 3.3 and Apache 2.2*
The Mod_WSGI module has been successfully installed into Apache.
I have also created a very basic project using django-admin.py called "check"
So now according to the Django Documentation on how to configure mod_wsgi
I have entered the following code into the Apache httpd.conf where it looks like this -
<VirtualHost 192.254.132.95:80>
ServerName bangtestwsgi.mbox140.com
ServerAlias www.bangtestwsgi.mbox140.com
DocumentRoot /home/bangwsgi/public_html
ServerAdmin webmaster#bangtestwsgi.mbox140.com
UseCanonicalName Off
CustomLog /usr/local/apache/domlogs/bangtestwsgi.mbox140.com combined
CustomLog /usr/local/apache/domlogs/bangtestwsgi.mbox140.com-bytes_log "%{%s}t %I .\n%{%s}t %O ."
## User bangwsgi # Needed for Cpanel::ApacheConf
UserDir enabled bangwsgi
<IfModule mod_suphp.c>
suPHP_UserGroup bangwsgi bangwsgi
</IfModule>
<IfModule !mod_disable_suexec.c>
<IfModule !mod_ruid2.c>
SuexecUserGroup bangwsgi bangwsgi
</IfModule>
</IfModule>
WSGIScriptAlias / /home/bangwsgi/check/check/wsgi.py
WSGIPythonPath /home/bangwsgi/check
<Directory /home/bangwsgi/check/check>
<Files wsgi.py>
Order deny,allow
Require all granted
</Files>
</Directory>
</VirtualHost>
Currently my DNS hasn't propagated so I am using 192.254.132.95/~bangwsgi/ to access the app (as told to me by Hostgator)
The thing is that nothing is happening. There is nothing in the Apache error log. There seems to be sign that the wsgi script is even running. Can someone tell me what I can do differently to make this work?
I'm not sure why you are going to /~bangwsgi/. Your WSGI app is being served at /, as defined by the first parameter to WSGIScriptAlias.
It works on removing the line
WSGIPythonPath /home/bangwsgi/check
And changing Require all granted
to Allow from all

How do I do multi-domain https with mod_wsgi?

I'm a developer and a bit of an apache novice and I'm trying to transition a bunch of our Django https servers to mod_wsgi, which previously used mod_python:
<VirtualHost *:443>
ServerName *.example.com
SSLEngine On
SSLCertificateFile /etc/ssl/star_example_com.crt
SSLCertificateKeyFile /etc/ssl/star_example_com.key
SSLCertificateChainFile /etc/ssl/DigiCertCA.crt
UseCanonicalName Off
VirtualDocumentRoot /var/www/%0
<Directory "/var/www/foo.example.com">
SetHandler python-program
PythonHandler django.core.handlers.modpython
SetEnv DJANGO_SETTINGS_MODULE example.foo.settings
PythonPath "['/home/django'] + sys.path"
PythonDebug on
</Directory>
<Directory "/var/www/bar.example.com">
SetHandler python-program
PythonHandler django.core.handlers.modpython
SetEnv DJANGO_SETTINGS_MODULE example.bar.settings
PythonPath "['/home/django'] + sys.path"
PythonDebug on
</Directory>
</VirtualHost>
This allows us to run several https sites on the same physical server (ip). Adding a new https host simply involves adding a new <Directory> section, and creating the corresponding directory under /var/www/.
The closest I've gotten is by using
<Directory "/var/www/bar.example.com">
SetHandler wsgi-script
Options ExecCGI
</Directory>
and creating /var/www/bar.example.com/bar.wsgi, then I can access urls like https://bar.example.com/bar.wsgi/...rest..of..path..
I haven't been able to find a directive (that can be used inside <Directory>) that is equivalent to WSGIScriptAlias when the target is a file-path, like we're using for the non-https sites:
<VirtualHost *:80>
ServerName bar.example.com
WSGIScriptAlias / /home/venv/upgrade/example/bar/bar.wsgi
<Directory "/var/www/bar/">
Order deny,allow
allow from all
</Directory>
</VirtualHost>
Which allows us to go to http://bar.example.com/...rest..of..path.. (ie. no bar.wsgi prefix).
Is it possible to get rid of the bar.wsgi part in the https url?
[Solution:] (based on #GrahamDumpleton's link):
<Directory /var/www/bar.example.com/>
Options ExecCGI Indexes FollowSymlinks
AddHandler wsgi-script .wsgi
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /bar.wsgi/$1 [QSA,PT,L]
Order allow,deny
Allow from all
</Directory>
works beautifully.
This is covered in:
http://code.google.com/p/modwsgi/wiki/ConfigurationGuidelines#The_Apache_Alias_Directive
See the bit towards the end of the section where shows use of mod_rewrite and an application wrapper to do fixups of SCRIPT_NAME.