deploy django application on apache server - django

I'm new to django and deploying apps. I tried to follow this tutorial. Everything worked great except that by the end, instead of the django app I got the depault apache2 page. (If it's matter, I work on ubuntu).
/etc/apache2/sites-available/000-default.conf:
<VirtualHost *:8079>
Alias /static /home/or/myproject/static
<Directory /home/or/myproject/static>
Require all granted
</Directory>
<Directory /home/or/myproject/myproject/>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess myproject python-path=/home/or/myproject/ python-home=/home/or/myproject/myprojectenv
WSGIProcessGroup myproject
WSGIScriptAlias / /home/or/myproject/myproject/wsgi.py
</VirtualHost>
Any suggestions? maybe firewall or something? I'm pretty lost...
[1]: https://www.digitalocean.com/community/tutorials/how-to-serve-django-applications-with-apache-and-mod_wsgi-on-ubuntu-14-04

So I'm not sure what went wrong but I used this tutorial and it works great (except that I edited '000-default' file instead of 'default' file.

Related

LOST on WSGIPythonPath takes one argument, Python module search path

I'm sorry if ask very dumb question.
But I had no idea what i'm doing, I'm just following the tutorial
Link here: https://www.youtube.com/watch?v=F6-yJpPEpoE
Now I'm just trying to start up Apache server to run my Django code in productions
I encounter this error:
C:\WINDOWS\system32>C:/Frontier_Website/Apache24/bin/httpd.exe -k startserver
AH00526: Syntax error on line 542 of C:/Frontier_Website/Apache24/conf/httpd.conf:
WSGIPythonPath takes one argument, Python module search path.
I'm assuming the error is unable to find my python path, something related to python, which apologies I'm not sure what is looking for.
This are the codes in httpd setting:
#python and mod_wsgi setting
LoadModule wsgi_module "c:/users/user/appdata/local/programs/python/python37/lib/site-packages/mod_wsgi/server/mod_wsgi.cp37-win_amd64.pyd"
WSGIScriptAlias / "C:\Frontier_Website\FrounterWeb postgreDB-the secnond\FrounterWeb\wsgi.py"
WSGIPythonHome C:/users/user/appdata/local/programs/python/python37
WSGIPythonPath C:\Frontier_Website\FrounterWeb postgreDB-the secnond\zigview
<Directory C:\Frontier_Website\FrounterWeb postgreDB-the secnond\zigview\static>
Require all granted
</Directory>
<Directory C:\Frontier_Website\FrounterWeb postgreDB-the secnond\zigview>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
In advance thank you, so much on the help
Use this template to a django app in Apache:
<VirtualHost *:80>
. . .
Alias /static /home/user/myproject/static
<Directory /home/user/myproject/static>
Require all granted
</Directory>
<Directory /home/user/myproject/myproject>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess myproject python-path=/home/user/myproject python-home=/home/user/myproject/myprojectenv
WSGIProcessGroup myproject
WSGIScriptAlias / /home/user/myproject/myproject/wsgi.py
</VirtualHost>
WSGIPythonPath is for additional directories to search for Python modules, not neccesssary if you use a virtual env, I guess
Source: https://www.digitalocean.com/community/tutorials/how-to-serve-django-applications-with-apache-and-mod_wsgi-on-ubuntu-14-04 (The apache conf works in Windows too)

How to set the apache2.conf file for a django project?

I am now deploying a django test project on aws-ec2 and the AMI is Ubuntu18.04 with Python 3.6, Django 2.1, Apache2.
The project is under /var/www/Project and I am trying to add the setting to apache.conf.
The project is simply generated by django-admin startproject Project and I want make sure that when hit the public IP provided by the instance, it should show up the django default page.
WSGIDaemonProcess ubuntu processes=2 threads=12 python-path=/var/www/Project
WSGIProcessGroup ubuntu
WSGIRestrictEmbedded On
WSGILazyInitialization On
WSGIScriptAlias / /var/www/Project/Project/wsgi.py
<Directory /var/www/Project/Project>
Require all granted
</Directory>
Now i got the internal server error.
The server encountered an internal error or misconfiguration and was unable to complete your request.
Please contact the server administrator at webmaster#localhost to inform them of the time this error occurred, and the actions you performed just before this error.
More information about this error may be available in the server error log.
I have tried this previously. And it seems like it only works when i use python2.7 with django 1.11.
WSGIScriptAlias / /var/www/Project/Project/wsgi.py
WSGIPythonPath /var/www/Project
<Directory /var/www/Project/Project>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
what's wrong with my conf file?
I finally come up with the following solution.
If you are using Apache2, Python2 and Ubuntu18.04 LTS, you can change the apache2.conf file by adding the following:
WSGIScriptAlias / [path_to_wsgi.py]
WSGIPythonPath [path_to_project]
<Directory [path_to_project]>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
If you want to use the latest Django 2+ version, change the /etc/apache2/sites-available/000-default.conf which is the settings for HTTP port 80. (000-default.conf file can't contains WSGIPythonPath in virtualhost)
<VirtualHost *:80>
ServerAdmin webmaster#localhost
Alias /static [path_to_static_folder]
<Directory [path_to_static_folder]>
Require all granted
</Directory>
<Directory [path_to_project]>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess [name] python-home=[path_to_virtualenv] python-path=[path_to_project]
WSGIProcessGroup [name]
WSGIScriptAlias / [path_to_project_wsgi.py]
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Python Djangocms Install App WSGI vs 8000 port

I follow the tutorial http://docs.django-cms.org/en/develop/introduction/plugins.html, when I install the Polls App as indicated it is visible on 8000 port But not in WSGI/Apache mode (error message no module polls). To see the APP polls I need to copy Polls application files in the default root directory. Idem with the Aldryn Blog News. I guess I have to specify some more PATH in wsgi mode to help Python to find the modules. Where and how to do that in my Virtualenv, to be also effective when I deploy all the stuff on a remote platform ?
Thanks for any help
Thank you for your interest. IMHO I don't think the problem is on APACHE configuration, everything is OK and DjangoCMS works without polls. Herinafter the conf file, the domain is a local virtual one.
DocumentRoot "/var/www/djangocms"
WSGIScriptAlias / /var/www/djangocms/default/wsgi.py
ServerName djangocms.net
Alias /static/ /var/www/djangocms/default/static/
Options +ExecCGI
Order Allow,Deny
Allow from All
ErrorLog "logs/errordjangocms_log"
LogLevel error
Marcel
Here is a working config i use
<VirtualHost *:80>
ServerName iot.mydomain.com
ServerAlias iot
WSGIDaemonProcess iot.local python-path=/home/name/PycharmProjects/iotdata
WSGIProcessGroup iot.local
WSGIScriptAlias / /home/name/PycharmProjects/iotdata/iotdata/wsgi.py process-group=iot.local
#WSGIPythonPath /home/name/PycharmProjects/iotdata
Alias /static/ /home/name/PycharmProjects/iotdata/static/
<Directory /home/name/PycharmProjects/iotdata>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
<Directory /home/name/PycharmProjects/iotdata/static>
Require all granted
</Directory>
</VirtualHost>

Django, mod_wsgi--daemon mode, 403 Forbidden, works when in /var/www/html

When I move /home/django/mysite to /var/www/html/mysite, and chown -R apache:apache, it works. But leaving in the home directory I get 403 forbidden.
I've literally spent a good 6 hours trying to get this to work. HUGE progress for me to see that it works in /var/www/html, as I know all my "config" settings are correct.
Here is my httpd.conf
(first setting had to add to stop 503 internal server error)
WSGISocketPrefix /var/run/wsgi
<VirtualHost 208.115.206.227:80>
WSGIDaemonProcess django user=django group=django python-path=/home/django/mysite
WSGIProcessGroup django
ServerAdmin webmaster#mktrn.net
ServerName endor.mktrn.net
WSGIScriptAlias / /home/django/mysite/mysite/wsgi.py
<Directory />
Options FollowSymLinks
AllowOverride None
Order deny,allow
Allow from all
</Directory>
<Directory /home/django/mysite/mysite>
<Files wsgi.py>
Order allow,deny
Allow from all
</Files>
</Directory>
</VirtualHost>
So again, the above works if I just replace /home/django/mysite, with /var/www/html.
I'm running mod_wsgi as daemon under the user django--so I don't think that is it. What am I doing wrong? I know it's not smart to put in /var/www/html, even if I have no document root, so really want to figure this out!
A home directory such as /home/django would normally not be readable to others and so the user that Apache runs as would not be able to see into that directory and so access would be forbidden. This is why it would work when you move it elsewhere.

can't reload apache with wsgi

So I'm following the tutorial on Django on how to deploy : https://docs.djangoproject.com/en/1.6/howto/deployment/wsgi/modwsgi/
I copypasted this in httpd.conf
WSGIScriptAlias / /path/to/mysite.com/mysite/wsgi.py
WSGIPythonPath /path/to/mysite.com
<Directory /path/to/mysite.com/mysite>
<Files wsgi.py>
Order deny,allow
Require all granted
</Files>
</Directory>
and now all my websites are redirected to my django app
so I tried puting a VirtualHost around that script, and if I do that I cannot reload apache2. Any idea why or how I should do it?
I don't know how long I've been up working on that problem. But I found something:
WSGIPythonPath /path/to/mysite.com
If I write this line I can't reload apache.
So I removed it, and I wrote:
import sys
sys.path.append('/home/myproject/')
in wsgi.py
and it works :)