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.
Related
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
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.
I have an app that functions properly on my local wamp server using the virtual host configuration seen below. I recently purchased shared hosting, but unlike my localhost, I dont have access to Apache's httpd-vhost.conf and instead must manipulate the site root's .htaccess file in order to "wire up" my application properly.
The problem is that I do not know how to use the RewriteEngine to write RewriteRules that can emulate (to some degree) this virtual host config:
<VirtualHost *:80>
ServerName sitename
ServerAdmin webmaster#localhost
DocumentRoot "c:/wamp/www/siteroot"
AliasMatch /(.*)\.(js|css|rdf|xml|ico|txt|gif|html|png|jpg|jpeg|json|eot|woff|svg|ttf|pdf)$ "c:/wamp/www/siteroot/app/$1.$2"
AliasMatch /rest/(.*) "c:/wamp/www/siteroot/rest/$1"
AliasMatch /img/(.*) "c:/wamp/www/siteroot/app/img/$1"
AliasMatch /(.*) "c:/wamp/www/siteroot/app/index.php"
ErrorLog "logs/sitename_app_error.log"
LogLevel warn
CustomLog "logs/sitename_app_access.log combined" %a
</VirtualHost>
<VirtualHost *:80>
ServerName admin.sitename
ServerAdmin webmaster#localhost
DocumentRoot "c:/wamp/www/siteroot"
AliasMatch /app/(.*)\.(js|css|rdf|xml|ico|txt|gif|html|png|jpg|jpeg|json|eot|woff|svg|ttf|pdf)$ "c:/wamp/www/siteroot/app/$1.$2"
AliasMatch /i18n/(.*)\.json$ "/wamp/www/siteroot/app/i18n/$1.json"
AliasMatch /(.*)\.(js|css|rdf|xml|ico|txt|gif|html|png|jpg|jpeg|json|eot|woff|svg|ttf|pdf|csv)$ "c:/wamp/www/siteroot/adminApp/$1.$2"
AliasMatch /rest/(.*) "c:/wamp/www/siteroot/rest/$1"
AliasMatch /img/(.*) "c:/wamp/www/siteroot/adminApp/img/$1"
AliasMatch /(.*) "c:/wamp/www/siteroot/adminApp/index.html"
ErrorLog "logs/sitename_app_error.log"
LogLevel warn
CustomLog "logs/sitename_app_access.log combined" %a
</VirtualHost>
<Directory "c:/wamp/www/siteroot">
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory "c:/wamp/www/siteroot/app">
Options Indexes FollowSymLinks MultiViews
AllowOverride All
</Directory>
<Directory "c:/wamp/www/siteroot/adminApp">
AuthName "Login required"
AuthType Basic
AuthUserFile "c:/wamp/www/siteroot/adminApp/.htpasswd"
AuthGroupFile "/dev/null"
Require valid-user
</Directory>
<Directory "c:/wamp/www/siteroot/rest">
Options Indexes FollowSymLinks MultiViews
AllowOverride All
</Directory>
*note that sitename and siteroot aren't the actual names of the site/filepath.
The aliasMatches are what's most important for my site to "work." This is why I think I can get by using the RewriteRules in .htaccess.
Even if you aren't familiar with RewriteRules, if you could tell me what the regular expressions mean, that would be of great help too! Any assistance is appreciated!
I am trying to create a small django site and use iRedMail for e-mail. I installed iRedMail first, and ensured that it worked. I could go to both www.domain.com/iredadmin and www.domain.com/mail and have it work perfectly. My next step was to install my django site and configure Apache. Unfortunately, this caused my django site to try and handle /mail/ and /iredadmin/. I've been fidgeting with the config for a few hours now and have no idea what to do. Here are the settings:
apache2.conf:
# Defaults...
WSGIPythonPath /path/to/website.com/website
sites-enabled/website.com:
<VirtualHost *:80>
ServerName website.in
ServerAlias www.website.in
ErrorLog ${APACHE_LOG_DIR}/error.log
Alias /static /path/to/website.com/website/static
Alias /media /path/to/website.com/website/media
Alias /mail /usr/share/apache2/roundcubemail/
Alias /admin /usr/share/apache2/iredadmin/
<Directory /usr/share/apache2/roundcubemail/>
Order allow,deny
Allow from all
</Directory>
WSGIScriptAlias / /path/to/website.com/website/website.wsgi
<Location "/">
SetHandler python-program
PythonHandler django.core.handlers.modpython
SetEnv DJANGO_SETTINGS_MODULE website.settings
PythonDebug Off
PythonPath "['/path/to/website.com/website/']+sys.path"
</Location>
<Directory /path/to/website.com/website>
<Files wsgi.py>
Order deny,allow
Allow from all
</Files>
</Directory>
<Directory /path/to/website.com/website/static>
Order allow,deny
Allow from all
</Directory>
<Location /static/>
SetHandler None
</Location>
<Directory /path/to/website.com/website/media>
Order allow,deny
Allow from all
</Directory>
<Location /media/>
SetHandler None
</Location>
</VirtualHost>
The django website displays fine, although I have been getting internal server errors.
You are trying to use both mod_wsgi and mod_python to handle the Django site at the same time, with mod_python overriding mod_wsgi. Choose one of the other. Since mod_python is no longer developed or supported and support for it in Django deprecated, probably not a good option to keep using it.
The next thing which is wrong is:
Alias /mail /usr/share/apache2/roundcubemail/
Alias /admin /usr/share/apache2/iredadmin/
Remove the trailing slashes:
Alias /mail /usr/share/apache2/roundcubemail
Alias /admin /usr/share/apache2/iredadmin
Even then it will still not work, because when using mod_python you have to tell mod_python not to handle those paths.
<Location /mail/>
SetHandler None
</Location>
<Location /admin/>
SetHandler None
</Location>
A further problem you may have is that /admin is usually used for the Django admin interface and you are overriding that.
I am using a django based framework and have successfully figured Apache settings for http mode. Basically I have done the setting correctly on <VirtualHost *:80> ... </VirtualHost> and when I do, http://mysite.domain.com I get routed correctly to my site and the site pages and the skins get render correctly.
I have setup https://mysite.domain.com to work with shibboleth, shibboleth is working and when use the https I get routed to login credential page via shibboleth server, and after successful login I get redirect to https://mysite.domain.com but site doesn't get rendered correctly and skins don't show up as same as http://mysite.domain.com.
Here is my Apache settings, I am trying to understand what I am doing wrong here
<VirtualHost *:443>
ServerAdmin myname#mydomain.com
DocumentRoot /code/vEnviornment/mysite
ServerName mydomain.com
#<LocationMatch "^(?!/admin)">
#<LocationMatch "^(?!/m)">
# RewriteEngine on
# RewriteRule django.wsgi(.*)$ https://mydomain.com:443$1 [L,R=301]
#</LocationMatch>
SSLEngine on
#your SSL keys
#I have removed this wasn't comfortable putting SSL key info
#Alias /admin/media/ /usr/local/lib/python2.6/site-packages/django/contrib/admin/media/
Alias /admin/media/ /usr/local/lib/python2.7/dist-packages/django/contrib/admin/media/
WSGIScriptAlias /m/ /code/vEnviornment/mysite/django.wsgi
<Directory "/">
AuthType shibboleth
ShibRequestSetting requireSession 1
Require valid-user
</Directory>
Alias /Shibboleth.sso /tmp
# CustomLog /var/log/httpd/mysite/access_log common
# ErrorLog /var/log/httpd/mysite/error_log
CustomLog /var/log/apache2/mysite/access_log common
ErrorLog /var/log/apache2/mysite/error_log
</VirtualHost>
And here is how I have hetup http:
<VirtualHost *:80>
ServerAdmin myname#mydomain.com
DocumentRoot /code/vEnviornment/mysite
ServerName mysite.mydomain.com
#aliases to serve static media directly
#will probably need adjustment
Alias /m/ /code/vEnviornment/mysite/static/
Alias /upfiles/ /code/vEnviornment/mysite/myframework/upfiles/
<DirectoryMatch "/code/vEnviornment/mysite/myframework/skins/([^/]+)/media">
Order deny,allow
Allow from all
</DirectoryMatch>
<Directory "/code/vEnviornment/mysite/myframework/upfiles">
Order deny,allow
Allow from all
</Directory>
#must be a distinct name within your apache configuration
WSGIDaemonProcess mysite2
WSGIProcessGroup mysite2
WSGIScriptAlias / /code/vEnviornment/mysite/django.wsgi
#make all admin stuff except media go through secure connection
<LocationMatch "/admin(?!/media)">
RewriteEngine on
RewriteRule /admin(.*)$ https://128.101.35.71/admin$1 [L,R=301]
</LocationMatch>
# CustomLog /var/log/httpd/mysite/access_log common
# ErrorLog /var/log/httpd/mysite/error_log
CustomLog /var/log/apache2/mysite/access_log common
ErrorLog /var/log/apache2/mysite/error_log
LogLevel debug
</VirtualHost>
What am I doing wrong here to render the site incorrectly via https?
Alias /m/ /code/vEnviornment/mysite/static/
Alias /upfiles/ /code/vEnviornment/mysite/myframework/upfiles/
These two lines are missing in https virual host
and
your WSGIScriptAlias should point to / not /m/