Configureing mod_wsgi on Apache2 for Django project - django

I am having some issues in configuring the various elements of a mod_wsgi setup. This is my fist time using mod_wsgi so I have been following several tutorials, the main one being a YouTube video Install Django on Apache with mod_wsgi on Linux . From what I understand after following the below steps I should at least see the Django 'It Works' page.
Setup
Ubunto 12.04
Apache 2.2.22
Python 2.7.3
Django 1.6
I have created a WSGI script file firstweb.wsgi in my home directory
/home/firstweb.wsgi
Its contents are
import os
import sys
sys.path = [‘/var/www/firstweb’] + sys.path
os.environ[‘DJANGO_SETTINGS_MODULE’] = ‘firstweb.settings’
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
I then created a host file for the project located at
/etc/apache2/sites-available/firstweb.conf
Its contents are
<VirtualHost *:80>
ServerAdmin webmaster#firstweb.com
ServerName www.firstweb.com
ServerAlias firstweb.com
WSGIScriptAlias / /home/firstweb.wsgi
Alias /static /var/www/firstweb/static/
<Directory /var/www/firstweb/>
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
I then enabled the configuration file with the command
a2ensite firstweb.conf
I then started my django project.
I went to /var/www/ and used the Django start project command
django-admin.py startproject firstweb
I then restarted my server
sudo service apache2 restart
Lastly I then reconfigured my Apache hosts file to point the domain firstweb.com to my new, local, Django project
/etc/hosts
127.0.0.1 localhost
127.0.1.1 ubuntu
134.226.38.147 firstweb.com
So with all of this done when I visit www.firstweb.com I should see the Django "It Works!" page
Can anyone tell me what I'm doing wrong?

You haven't listed 'www.firstweb.com' in your /etc/hosts file. Both hosts listed under ServerName and ServerAlias must have a resolvable IP address.

Related

Deploying Django restfule service using WAMP and mod_wsgi

I am trying to deploy an API built on Django rest-framework using mod_wsgi and WAMP.
When I created a new Django project using 'django-admin startproject Predictor' and deployed it on WAMP, it was working fine as I can see the default Django window.
Now I created an app in the project using 'python manage.py startapp Predictor'
I built an API which accepts GET call and tested it using 'python manage.py runserver' and it was working fine.
Now I started WAMP service again and try to go to 'localhost:8000' it keeps loading. And as soon as I stop WAMP service it crashes saying Unable to connect. Not sure what's happening. Can someone help what's wrong I am doing? FYI I am on windows and I have created and virtual environment for my Django project. This is how my .conf and wsgi files look like
wsgi_windows.py
activate_this = 'C:/Users/DELL/Envs/predenv/Scripts/activate_this.py'
# execfile(activate_this, dict(__file__=activate_this))
exec(open(activate_this).read(),dict(__file__=activate_this))
import os
import sys
import site
# Add the site-packages of the chosen virtualenv to work with
site.addsitedir('C:/Users/DELL/Envs/predenv/Lib/site-packages')
# Add the app's directory to the PYTHONPATH
sys.path.append('C:/Users/DELL/Envs/predenv')
sys.path.append('C:/Users/DELL/Envs/predenv/Predictor')
os.environ['DJANGO_SETTINGS_MODULE'] = 'Predictor.settings'
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "Predictor.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
httpd.conf
Listen localhost:8000
Listen [::0]:80
ServerName localhost:8000
LoadFile "c:/python376/python37.dll"
LoadModule wsgi_module "c:/python376/lib/site-packages/mod_wsgi/server/mod_wsgi.cp37-win_amd64.pyd"
WSGIPythonHome "c:/python376"
WSGIPythonPath "C:/Users/DELL/Envs/predenv/Predictor"
httpd-vhosts.conf
WSGIPythonPath "C:/Users/DELL/Envs/predenv/Predictor"
<VirtualHost *:80>
WSGIPassAuthorization On
ErrorLog "logs/predictor.error.log"
CustomLog "logs/predictor.access.log" combined
DocumentRoot "C:/Users/DELL/Envs/predenv/Predictor"
WSGIScriptAlias / "C:\Users\DELL\Envs\predenv\Predictor\Predictor\wsgi_windows.py"
<Directory "C:\Users\DELL\Envs\predenv\Predictor\Predictor">
<Files wsgi_windows.py>
Require all granted
</Files>
</Directory>
</VirtualHost>
The issue was it was not able to 'import pandas' in my views.py
To resolve this I have to add this line in my apache 'httpd-vhosts.conf' file
WSGIApplicationGroup %{GLOBAL}

Can't Deploy Mezzanine CMS using Apache/Ubuntu

I'm trying to become better with deploying a website from scratch so I setup a new Nanode from Linode using the latest Ubuntu (19.04 I think).
I followed the instructions to install Mezzanine CMS (Django Based) and it all went fine, I was able to run the dev server to test the website fine on port 8000.
I did install UFW and right now only activity on port 80 is not rejected. Also I have mysql database running with Mezzanine not Djangos default SQLlite.
I installed Apache and WSGI. At first Apache would not restart due to some configuration issue which I think I've since worked out.
I have a domain alexmerced which is on godaddy, but I have a DNS "a" records which is pointing to my linodes IP address "mezz.alexmerced.com"
Mezzanine is still a fresh install so I haven't really changed anything outside of the WSGI and settings.py.
I currently get a forbidden error when I type the URL or IP address, the permissions of the application directory is 755.
below are my configuration file settings to see if I made any mistakes.
django_project.conf (the only conf file enabled in the /sites-available/ directory
<VirtualHost *:80>
ServerName mezz.alexmerced.com
ServerAdmin mezzanine#localhost
DocumentRoot /mymezz
WSGIScriptAlias / /mymezz/mymezz/wsgi.py
Alias /static /mymezz/static/
<Directory /mymezz/>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
# vim: syntax=apache ts=4 sw=4 sts=4 sr noet
here is the wsgi.py file
"""
WSGI config for mymezz project.
It exposes the WSGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/1.11/howto/deployment/wsgi/
"""
import os
from django.core.wsgi import get_wsgi_application
from mezzanine.utils.conf import real_project_name
os.environ.setdefault("DJANGO_SETTINGS_MODULE",
"%s.settings" % real_project_name("mymezz"))
application = get_wsgi_application()
other stuff I read on asked me to add the following to my settings.py
PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))
sys.path.append(os.path.join(PROJECT_ROOT, ".."))
settings_module = "%s.settings" % PROJECT_ROOT.split(os.sep)[-1]
os.environ["DJANGO_SETTINGS_MODULE"] = settings_module
and
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
I never did figure this out with Apache, but I did successfully deploy this blog using Nginx and Uwsgi, just be careful regarding the path your virtual environment once completed I realized that was the problem I was having apache.

Deployment of a Django app on Apache2 404 not found

I googled it and i searched everywhere including, but i could't find any solution for my problem.
I'm trying to deploy an django application on a VPS server.
I set up apache2 with my project but when i try to go to http://xxx.xxx.xxx.xxx i always get a 404 not found.
Here are my configurations:
project path : /var/www/master
WSGI file path /var/www/master/projectx249/wsgi.py
Apache2 site conf path: /etc/apache2/site-available/mekhoukh.conf
Apache2 site conf file: for this part this is the last conf i tried on it.
<VirtualHost *:80>
<Directory /var/www/master/projext249>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess mekhoukh python-home=/usr/lib/python3.5 python-path=/var/www/master
WSGIProcessGroup mekhoukh
WSGIScriptAlias / /var/www/master/projext249/wsgi.py
i'm using ssh to connect trough the server, i also updated the /etc/hosts with the public ip pointig to the domain name and the localhost ip pointing to it too.
EDIT:
Here's my wsgi.py config file:
import os
from django.core.wsgi import get_wsgi_application
sys.path.append('/var/www/master/projetx249/')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "projetx249.settings")
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

How I can run /myapp/my_app.py by default when accessing `localhost` using Bottle?

Desired Behaviour
I want to serve content created by the file at:
/myapp/my_app.py
when accessing localhost.
Question
I know that if I add the following to test.py and run the file directly, the results will be accessible at localhost:8080:
from bottle import route, run
#route('/')
def hello():
return "Hello World!"
run(host='localhost', port=8080, debug=True)
But how do I trigger this file to run by default when accessing localhost?
Environment
Linux Mint 17
MongoDB
RockMongo (Apache2, PHP, MongoDB Driver)
What I've Tried
I installed mod_wsgi and created /var/www/html/myapp/app.wsgi with this content:
import bottle
import os
os.chdir(os.path.dirname(__file__))
#route('/')
def hello():
return "Hello World!"
application = bottle.default_app()
And then restarted Apache.
But going to localhost just shows a file directory.
I then created /etc/apache2/sites-enabled/mygreatapp.conf with this content:
<VirtualHost *>
ServerName google.com
WSGIScriptAlias / /var/www/html/myapp/app.wsgi
<Directory /var/www/html/myapp>
WSGIProcessGroup yourapp
WSGIApplicationGroup %{GLOBAL}
Order deny,allow
Allow from all
</Directory>
</VirtualHost>
But I still just see a file directory.
Official Instructions
http://bottlepy.org/docs/dev/deployment.html#apache-mod-wsgi
Further Information
Troubleshooting
See if mod_wsgi is loaded
apache2ctl -M
...
wsgi_module (shared)
Definitions
For my own reference, I'm trying to fully comprehend several elements that I think are involved in the process, feel free to correct if my understandings are wrong:
WSGI - A convention for how applications should communicate with servers.
It does not exist anywhere else other than as words in the PEP 3333 - Source
mod_wsgi - An Apache module that allows Python files to communicate with Apache.
Web Framework - See a list of them here: https://wiki.python.org/moin/WebFrameworks
Web Server - http://en.wikipedia.org/wiki/Web_server
Apache Server - http://en.wikipedia.org/wiki/Apache_HTTP_Server
I finally have "Hello World!" displaying in the browser at localhost.
I will paste the various required configs and files below.
I'm a newb so I don't know if this is definitive or best practise.
/var/www/html/myapp/app.wsgi
# view the effect of import method:
http://stackoverflow.com/a/2452639/1063287
import bottle
import os
os.chdir(os.path.dirname(__file__))
# note i am using bottle.route and not just route
#bottle.route('/')
def hello():
return "Hello World!"
application = bottle.default_app()
/etc/apache2/sites-available/000-default.conf (note: this is the default .conf file)
<VirtualHost *:80>
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html
WSGIScriptAlias / /var/www/html/myapp/app.wsgi # just added this
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Viewing the Apache error log is what eventually led me to try a few different things:
sudo tail -100 /var/log/apache2/error.log
Where I saw things like:
Exception occurred processing WSGI script '/var/www/html/myapp/app.wsgi'.
Traceback (most recent call last):
File "/var/www/html/myapp/app.wsgi", line 8, in <module>
#route('/')
NameError: name 'route' is not defined
This was only after I had made the step of not using a new .conf file and just modifying the default one.
So for some reason it didn't like any new .conf files.

Django (1.6) + Apache2

First time I setup Django and Apache, and I'm having a tough time. It's seems easy by following tutorials, but it doesn't seem to work.
Basically, the steps I followed (on Debian):
Install Django (and some libraries)
Install Apache2
Install mod_wsgi
Then I've put my Django app in /root/, so the path is something like :
root/
.Projet/
.myprojet/
.site/
. .#here the models.py, views.py, etc of my site
.myprojet/
. .#here the settings.py, wsgi.py
. .static/
. .#static files in folders
.templates
. .#my templates
.database.sql
.manage.py
Then in /etc/apache2 I created the file httpd.conf, with this inside :
WSGIScriptAlias / /root/Projet/myprojet/myprojet/wsgi.py
WSGIPythonPath /root/Projet/myprojet
<Directory /root/Projet/myprojet/myprojet>
<Files wsgi.py>
Order deny,allow
Allow from all
</Files>
</Directory>
I do a2enmod wsgi (it tells me "Module wsgi already enabled").
Then service apache2 restart (successful, but tells me I have no VirtualHosts).
When I go to my website (using the IP adress), just the Apache "It works" display.
Thank you for your help ^^
You shouldn't put your Apache configuration file in /etc/apache2/. The fact that you mention a2enmod implies that you are on a Debian-based distro like Ubuntu. For those systems, you need to give that file a name related to the app - call it django if you like - and put it in /etc/apache2/sites-available. Then you can run a2ensite django to link it to sites-enabled, restart Apache, and it should work.