I have installed the following on my Windows machine (everything is 64bit):
Python 3.4.3
Apache 2.4.16
mod_wsgi 4.4.13
Then I installed Django(v1.8.3) in a virtual environment using virtualenv. Using django-admin I created two project and I created two VirtualHost for that. And everything is working perfect. Now I would like to use a different environment for the second project but though the Windows doesn't support WSGIDaemonProcess and WSGIProcessGroup I don't know how to do that.
WSGIPythonPath "C:/_pythonDev/project1;C:/_pythonDev/project2;C:/_pythonDev/env1/Lib/site-packages"
<VirtualHost *:80>
ServerName one.local.com
WSGIScriptAlias / "C:/_pythonDev/project1/project1/wsgi.py"
<Directory "C:/_pythonDev/project1/project1">
<Files wsgi.py>
Require all granted
</Files>
</Directory>
</VirtualHost>
<VirtualHost *:80>
ServerName two.local.com
WSGIScriptAlias / "C:/_pythonDev/project2/project2/wsgi.py"
<Directory "C:/_pythonDev/project2/project2">
<Files wsgi.py>
Require all granted
</Files>
</Directory>
</VirtualHost>
So, If someone knows how to run Django projects in different environment please guide me.
Solution: click this link
You need to address two things.
Don't set WSGIPythonPath in the Apache configuration. Activate the Python virtual environment and set sys.path from the WSGI script file.
Change the WSGI script file so as not to use setdefault() in os.environ as that screws things up.
For reading see:
http://blog.dscpl.com.au/2012/10/requests-running-in-wrong-django.html
http://blog.dscpl.com.au/2014/09/using-python-virtual-environments-with.html
Related
I have created a Django protect that works perfectly fine on windows Apache with Xampp. However, if I try to create a virtual host for a non-Django website, it doesn't work.
If I then put my Django website into a virtual host it doesn't work, but then my non-Django website does work.
By doesn't work I mean it takes me to this https://i.stack.imgur.com/DS0a5.png
Here is all my code for my Django website inside a virtual host and my other non-project in a virtual host.
#Django Website
<VirtualHost *:443 _default_:443 neostorm.us.to:443>
ServerName neostorm.us.to
ServerAlias neostorm.us.to
Alias /static "C:/xampp/htdocs/neostorm/static"
<Directory "C:/xampp/htdocs/neostorm/static">
Require all granted
</Directory>
WSGIScriptAlias / "C:/xampp/htdocs/neostorm/neostorm/wsgi_windows.py" application-group=neostorm
<Directory "C:/xampp/htdocs/neostorm/neostorm">
<Files wsgi_windows.py>
Require all granted
</Files>
</Directory>
ErrorLog "C:\xampp\apache\logs\neostorm_error.log"
CustomLog "C:\xampp\apache\logs\neostorm_custom.log" common
</VirtualHost>
#Non Django Website
<VirtualHost *:443 mail.neostorm.us.to:443>
ServerName mail.neostorm.us.to
DocumentRoot "C:/xampp/htdocs/webmail"
<Directory "C:/xampp/htdocs/webmail">
Require all granted
</Directory>
</VirtualHost>
Any help would be appreciated.
The problem was there was a virtual host inside the httpd-ssl.conf file. Simply delete the virtual host inside that file and create your virtual hosts inside your virtual host file or where ever you want it.
The reason you may see your index of files is because there is no index.html to open.
This is an example of a virtual host
<VirtualHost *:443>
ServerName example.com
DocumentRoot "C:/xampp/htdocs/example"
<Directory "C:/xampp/htdocs/neostorm/webmail">
AllowOverride All
Require all granted
Options +Indexes
</Directory>
SSLEngine on
SSLCertificateFile "conf/example/example.com-chain.pem"
SSLCertificateKeyFile "conf/example/example.com-key.pem"
</VirtualHost>
If there is no index file inside that document root, you will see the index/ page.
Just a footnote, Options +Indexes tells your web server to show an index page like seen in the photo. Use Options -Indexes to prevent showing the index/ page, this is beneficial if you have a static folder.
I was deploying my django app on my VPS using the apache web server.
It works totally fine on http://192.46.209.82:8000/ but when I try to access the same using my IP address, 192.46.209.82. I get
Forbidden
You don't have permission to access this resource.
Apache/2.4.41 (Ubuntu) Server at 192.46.209.82 Port 80
Here is my conf file
nano /etc/apache2/sites-available/000-default.conf
<VirtualHost *:80>
ServerAdmin webmaster#example.com
DocumentRoot /root/django/myproject
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
Alias /static /root/django/myproject/static
<Directory /root/django/myproject/static>
Require all granted
</Directory>
<Directory /root/django/myproject/myproject>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess myproject python-path=/root/django/myproject python-home=/root/django/myprojectenv
WSGIProcessGroup myproject
WSGIScriptAlias / /root/django/myproject/myproject/wsgi.py
</VirtualHost>
I am using python's virtual environment, where myprojectenv is my virtual env path and the pwd is /root/django
Note : I tried the answer from the question that I was getting as suggestion to this question but that did not work for me.
EDIT : Updated permission
In my case even though I set up all the permission correctly, I still was getting error so I moved my entire project and virtual environment to /opt and it worked for me for now. But I would like to know the reason why it was not working under /home even when I gave all permissions
I have multiple django projects and i want to host them under same domain
eg: example.com/one<br> example.com/two
I have searched for various solutions and found the below given link which helped me alot.
Is it possible to host multiple django projects under the same domain?
From the above reading , I get to know that I need mod_wsgi for this but I am confused that where to install this mod_wsgi - Do i need to install under every project folder (seperate for every myenv) or it should be installed only once .
Please help me in how and where to install this mod_wsgi and finally how to host multiple projects under same domain name.
Some Code Tried By Another User With Same Problem But Also Not Working
<VirtualHost *:80>
ServerAdmin admin#my_domain.com
ServerName my_domain.com
ServerAlias www.my_domain.com
DocumentRoot /var/www/my_domain.com
ErrorLog ${APACHE_LOG_DIR}/my_domain.com_error.log
CustomLog ${APACHE_LOG_DIR}/my_domain.com_access.log combined
# site_1
Alias /site_1_project/static /var/www/my_domain.com/site_1_project/static
<Directory /var/www/my_domain.com/site_1_project/static>
Require all granted
</Directory>
Alias /site_1_project/media /var/www/my_domain.com/site_1_project/media
<Directory /var/www/my_domain.com/site_1_project/media>
Require all granted
</Directory>
<Directory /var/www/my_domain.com/site_1_project/site_1_project>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess site_1_project python-path=/var/www/my_domain.com/site_1_project python-home=/var/www/my_domain.com/site_1_project/django_env_site_1
WSGIProcessGroup site_1_project
WSGIScriptAlias /site_1_project/ /var/www/my_domain.com/site_1_project/site_1_project/wsgi.py
# site_2
Alias /site_2_project/static /var/www/my_domain.com/site_2_project/static
<Directory /var/www/my_domain.com/site_2_project/static>
Require all granted
</Directory>
Alias /site_2_project/media /var/www/my_domain.com/site_2_project/media
<Directory /var/www/my_domain.com/site_2_project/media>
Require all granted
</Directory>
<Directory /var/www/my_domain.com/site_2_project/site_2_project>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess site_2_project python-path=/var/www/my_domain.com/site_2_project python-home=/var/www/my_domain.com/site_2_project/django_env_site_2
WSGIProcessGroup site_2_project
WSGIScriptAlias /site_2_project/ /var/www/my_domain.com/site_2_project/site_2_project/wsgi.py
#RewriteEngine on
#RewriteCond %{SERVER_NAME} =my_domain.com [OR]
#RewriteCond %{SERVER_NAME} =www.my_domain.com
#RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
Installing mod_wsgi depends on what your host OS is. Check the instructions. If you're using CentOS or RedHat, I'd recommend looking at IUS Community; they provide a repository with yum installable packages for Python 3.6 and mod_wsgi. The version of mod_wsgi you install has to be compiled against the same version of Python you are running into your virtual environment.
Then you need to configure your VirtualHost properly. If you have a host at the root, it has to come last in your definition. Here's an example:
<VirtualHost *:443>
TimeOut 300
SSLEngine On
ServerName mysite.example.com
# Set to the lobal Application Group
WSGIApplicationGroup %{GLOBAL}
# Pass Authorizations through to the WSGI app for Django REST Framework Token Auth
WSGIPassAuthorization On
WSGIDaemonProcess subsite-develop-https python-home=/web/subsite-develop/venv request-timeout=300 user=apache group=apache
WSGIProcessGroup subsite-develop-https
WSGIScriptAlias /subsite /web/subsite-develop/config/wsgi.py process-group=subsite-develop-https
<Directory /web/subsite-develop/config>
Require all granted
</Directory>
Alias /subsite/static/ /web/subsite-develop/static/
<Directory /web/subsite-develop/static>
Header always set Access-Control-Allow-Origin "*"
Require all granted
</Directory>
WSGIDaemonProcess django-mysite-develop-https python-home=/web/django-mysite-develop/venv request-timeout=300 user=apache group=apache
WSGIProcessGroup django-mysite-develop-https
WSGIScriptAlias / /web/django-mysite-develop/config/wsgi.py process-group=django-mysite-develop-https
<Directory /web/django-mysite-develop/config>
Require all granted
</Directory>
Alias /static/ /web/django-mysite-develop/static/
<Directory /web/django-mysite-develop/static>
Header always set Access-Control-Allow-Origin "*"
Require all granted
</Directory>
Alias /media/ /var/media/mysite-www/
<Directory /var/media/mysite-www>
Require all granted
</Directory>
</VirtualHost>
This example will host one site at /subsite/ and another at the root, /. Notice that the root site comes last. It also means that you won't be able to use the route /subsite/ within the root project, since Apache will have diverted it to via the WSGIScriptAlias definition.
This is also for a site with TLS; you may have to switch the 443 to 80, and remove SSLEngine On if you're not using TLS. The WSGIPassAuthorization is for Django REST Framework tokens, you can probably remove it as well, but I've left it for a more complete example. This is for Apache 2.4+, when they switched to the Require all granted syntax.
IUS Community, if on RedHat/CentOS: https://ius.io/
I'll tell you how we did in our project. We have a single Django project with different routes. For example /players, /tablet. We hosted our project in two Docker containers. We have NGINX as our reverse proxy. NGINX redirects the request to the appropriate container based on the route. NGINX is exposed to the world. But, I'm not sure if it is useful for you or not.
I got a forbidden message error when I type localhost/project on windows 10. Tried suggestions with
Order allow,deny
Allow from all
Tried also the solution suggested here, without success, conf file of the apache on wamp 2.5 already listens on
Listen 0.0.0.0:80
Listen [::0]:80
Could it be because of the WSGIScriptAlias or WSGIPythonPath?
If so see how I've declared it:
WSGIScriptAlias /project "C:/project/project/code.py"
WSGIPythonPath "C:/project/"
<Directory "C:/project/project/">
<Files code.py>
Order deny,allow
Require all granted
</Files>
</Directory>
Thank you!
As I changed the Files code.py with wsgi.py it worked. The thing is that now I encounter rather different problem. Which I think is related to this one.
The picture shows the difference between the apache and Django web server. On the left is the address localhost/admin on the right is localhost:8000/admin
could that be because of the mod_wsgi not working properly?
tried to put in settings file STATIC_ROOT and use the command python manage.py collectstatic but without success. Does anybody knows how to be done in django1.8?
According to the documentation
Alias /static/ C:/project/static/
<Directory C:/project/static>
Require all granted
</Directory>
that has been required along with change in the settings.py file
STATIC_ROOT = path to your projects folder (where manage.py is located)
WSGIScriptAlias / C:/project/webpage/wsgi.py
WSGIPythonPath C:/project/
<Directory C:/project/webpage>
<Files wsgi.py>
Order deny,allow
Allow from all
Require all granted
</Files>
</Directory>
Thanks to #GrahamDumpleton and #DanielRoseman
I am having difficulty troubleshooting this issue. I have a Django app running on an Ubuntu 14.04 server (with Apache 2.4 and mod_wsgi for Python 3.4). It connects to SQL Server via pymssql.
In development, the app works fine. I query the database, and the database returns the expected results.
In production (under the Apache user), however, the script hangs at the exact point that a database query is made. My browser (Chrome or Firefox) shows a spinning wheel that continues to spin as long as the browser window is open.
I have the following in my apache2.conf file:
ServerName localhost
# WSGIDaemonProcess application
WSGIPythonPath /home/production_code/python3env/lib/python3.4/site-packages:/home/production_code/school
# WSGIProcessGroup application
WSGIScriptAlias / /home/production_code/school/school/wsgi.py
# Python virtualenv home
WSGIPythonHome /home/production_code/python3env
# Include the virtual host configurations:
IncludeOptional sites-enabled/*.conf
And the following in my sites-enabled/000-default.conf file:
<VirtualHost *:80>
ServerAdmin *****#school.edu
ServerName localhost
DocumentRoot /var/www/html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
Alias /static/ /home/production_code/school/static/
<Directory /home/production_code/school/>
Require all granted
</Directory>
<Directory /home/production_code/school/>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
<Directory /home/production_code/school/static>
Require all granted
</Directory>
</VirtualHost>
Does anyone have any idea what might be causing this or how I might troubleshoot this? The Apache error logs and access logs are not particularly helpful in this situation, since a response to the request is never rendered. Similarly, Django debugging is also not useful here.
Instead of:
# WSGIDaemonProcess application
WSGIPythonPath /home/production_code/python3env/lib/python3.4/site-packages:/home/production_code/school
# WSGIProcessGroup application
use:
WSGIDaemonProcess application python-path=/home/production_code/python3env/lib/python3.4/site-packages:/home/production_code/school
WSGIProcessGroup application
WSGIApplicationGroup %{GLOBAL}
A key part of this is the WSGIApplicationGroup directive, with it being set to %{GLOBAL}.
This is to get around faulty third party extension modules for Python that don't work in sub interpreters and which can fail with a dead lock or crash.
See:
https://code.google.com/p/modwsgi/wiki/ApplicationIssues#Python_Simplified_GIL_State_API
It is also recommend you go back to using daemon mode. It is generally not a good idea to use embedded mode.
http://blog.dscpl.com.au/2012/10/why-are-you-using-embedded-mode-of.html