running django on apache - django

First of all: please don't blame me for making newbie mistakes or anything like that. I am still learning and need just a bit of help.
So I created a droplet on digitalocean with ubuntu 16.04 and logged in and ran:
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install python-setuptools python-pip apache2 libapache2-mod-wsgi
and installed django:
sudo pip install Django==11.1.4
and created the project (without virtualenv) in a directory:
django-admin.py startproject mysite
However, if I do "runserver"
python manage.py runserver
and type xxx.xxx.xxx.xx:8000 in firefox there is nothing even though I see in the terminal that the runserver did work. If I type xxx.xxx.xxx.xx then I see the default page of apache. My goal is to run django over apache but I cannot even get started because the runserver which is just for testing didn't even work. How can I make this work, where did I make a mistake?
Edit: the output of the runserver is:
Performing system checks...
System check identified no issues (0 silenced).
September 02, 2017 - 22:21:12
Django version 1.11.4, using settings 'mysite.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

The manage.py runserver is NOT meant for serving your pages through Apache, it is ONLY meant as a lightweight local development server for running your code as you write it.
Instead, you should be following a guide to run DJango on Apache using mod_wsgi.
The only bit of advice I'd be willing to give that might help in this situation is that if you're trying to host runserver inside a virtual machine or Docker container, you need to explicitly set the host binding to
manage.py runserver 0.0.0.0:8000
Note that the default IP address, 127.0.0.1, is not accessible from other machines on your network. To make your development server viewable to other machines on the network, use its own IP address (e.g. 192.168.2.1) or 0.0.0.0 or :: (with IPv6 enabled).
Again, runserver is for local development ONLY. Do NOT use it to try and host a site through Apache since that's what hosting drivers like mod_wsgi are for.

The runserver and apache are 2 different things.
The local runserver is used by a developer to code and run the web application locally. Usually the settings loaded locally are different, for example the production website use settings and locally you use settings_local.py that ovverrides the production settings (very simplified example).
If you run your runserver and you load http://127.0.0.1:8000/ you should be able to see something and on your terminal you should see the runserver response to your requests from the browser.
If your goal is deploy your web application than you need something like apache, but not locally. IMHO, I suggest you to avoid Apache and use Nginx + Gunicorn + supervisord to manage your webapp on your production server. Much much easier to configure.

Related

Gunicorn not working on Amazon ec2 server

I am trying to deploy a django website on an aws ec2 instance (ubuntu 18.04) following this tutorial that is consistent with other online resources. Everything is working fine but there is some issue with gunicorn. The worker doesn't seem to boot.
I figured there was something wrong with my installation and I tried uninstalling and installing with different commands-
inisde my virtualenv with
pip install gunicorn
and
sudo -H pip install gunicorn
I even physically deleted all gunicorn files and reinstalled gunicorn but its always the same. Where have I gone wrong?
p.s: I had initially done sudo apt-get
From the screenshot you attached, it seems that gunicorn is installed correctly, but perhaps you have not passed a configuration file. The command to run gunicorn with a configuration is
gunicorn -c /path/to/gunicorn.conf.py myapp.wsgi:application

Linode/Django Can't see site when I runserver at ip:8000

I had started a fresh linode running ubuntu 19.04 and the first time I used the directions at:
https://www.rosehosting.com/blog/how-to-install-mezzanine-cms-on-ubuntu-18-04/
To install Mezzanine CMS it worked just fine, I could run the runserver command and see the django website. Eventually it started giving me a problem after trying 50 ways to deploy the site using apache and mod_wsgi.
I gave up and rebuilt the server and then still couldn't see the new install at the IP when I ran run server. I figured maybe it was because I accidentally installed some things using "python" and others with "python3" so I rebuilt the server.
This third time I followed the direction perfectly, the only difference is I didn't install a mysql server just kept the default SQLlite server and created a DB and Django Superuser.
I have added my ip as a host in settings.py and local_settings.py
I have already ran makemigrations and migrate
I did check to see if maybe the IP had changed when I rebuilt, it hadn't
My local environment on my laptop works fine, just not the linode
Any suggestions on anything I'm missing?
Deployment Guide
Step 1 (Installation)
Install apache2 mod_wsgi
Install virtualenv
Install virtualenv
Install Nginx for asynchronous event-driven approach to handle multiple client requests
Install mysql
sudo apt-get update
sudo apt-get install python3-pip python3-dev apache2 libapache2-mod-wsgi-py3
sudo apt-get install virtualenv ufw
sudo apt-get install nginx
sudo apt-get install mysql-server libmysqlclient-dev
sudo mysql_secure_installation
Step 2 (Apache & Hostname & User)
Set hostname and add it into /etc/hosts with hostname and your Server IP
Create new user and give add to the group sudo for safety
sudo adduser username
sudo usermod -aG sudo
Enable SSH authentication for login & Edit default port of ssh in /etc/ssh/sshd_config. https://askubuntu.com/questions/1074034/not-able-to-change-ssh-port-on-ubuntu-18-04-1-lts
Edit /etc/apache2/site-availabledefault-000.conf for your new Django configuration
Step 3 (Firewall)
sudo ufw allow 8000
sudo ufw allow http
sudo ufw allow ssh
sudo ufw default allow outgoing
sudo ufw default deny incoming
Enable all other required port numbers
ssh sudo ufw enable
Step 4 (Django configuration)
Chown static and media forlders and edit it's permission recursively
Add allowed host in settings.py
Checkout deployment checklist in django official website and do it.
Step 5 (Please checkout)
For apache configuration please visit https://pythonprogramming.net/deploying-to-server-django-tutorial/
Edit the path given in your apache configuration (path for WSGI Script,python-path, python-home ) if any errors found like Internal server error, miss configuration etc
For reference of python-home path please refer Get virtualenv's bin folder path from script
You can also add python-path to WSGIDaemonProcess
Additionally, you can visit puttygen for public and private key generation to login through SSH
For this particular problem turned out I just needed to suddenly bind dev server to 0.0.0.0
the command to do so was
python manage.py runserver 0.0.0.0:8000
Rinshans answers is definetley the details for deployment, I've followed those steps just kept making some mistake in the config and wsgi scripts. I'm going to try deploying with Gunicorn or use the Fabric self-deployment tools built in to Mezzanine CMS, just haven't done so yet.

How to deploy Django on IIS with git hooks using virtenv on a remote Windows server?

I successfully installed and setup Django (2.0.2) using Python 3.6.4 on IIS on a remote Windows Server 2012 R2 (in a VPN environment) accordingly to this instruction:
http://blog.mattwoodward.com/2016/07/running-django-application-on-windows.html
I'm using SQL Server as backend and I stored the sensitive data such as the DJANGO_SECRET_KEY, DATABASE_PASSWORD etc. as environment variables in the FastCGI Settings at the IIS (see the above link, section Configure FastCGI in IIS step 14)
At this point I asked me, how to deploy my apps and do all the necessary setps like install packages from requirements.txt, collectstatic, makemigrations etc.
With googleing I found a possible solution using git hooks, especially a post-receive hook accordingly to this post:
https://dylanwooters.wordpress.com/2015/02/21/one-command-deployments-to-iis-using-git/
So I successfully setup Bonobo Git Server on the same IIS accordingly to this instruction:
https://bonobogitserver.com/install/
My thought was to put all the required commands into this post-receive hook file as I would do in the development environment. I ended up with this file at location \inetpub\wwwroot\Bonobo.Git.Server\App_Data\Repositories\myapp-master\hooks\post-receive:
#!/bin/sh
DEPLOYDIR=/c/apps/myapp
VIRTENVDIR=/c/virtualenvs/myapp
GIT_WORK_TREE="$DEPLOYDIR" git checkout -f
cd $VIRTENVDIR
source Scripts/activate
cd $DEPLOYDIR
pip install -r requirements.txt
python manage.py collectstatic --noinput
python manage.py makemigrations
python manage.py migrate
I grant permission to the Scripts/activate file and I receive no erros during push from my development environment. But the current problem is that the command at line 6 doesn't activate the virtual environment and therefore no migrations etc. happens. Normally I would activate the virtual environment with the command line prompt using these commands:
cd C:\virtualenvs\myapp
Scripts\activate.bat
but I can't use this in the post-reveive file because it uses unix-based commands.
Can anyone help me or have a better idea how to deploy in a nicer way in a windows environment without going into the cloud?

makemigrations or migrate while server is runnig

I'm wondering how we can handle database migration in django while the site in production as while developing we stop the server then make changes in database then rerun the server I think it may be stupid question but I am learning by myself and can't figure it out thanks in advance.
You can connect to the server using ssh and run commands to migrate without stopping the server and once you are done, you restart the server.
python manage.py makemigrations
and then
python manage.py migrate
and then restart the server.
for example: in case of nginx and gunicorn
sudo service gunicorn restart
sudo service nginx restart

Setting up SSL Certificate using mod_wsgi

I have developed a Django 1.8 application using Python 3.3. I am trying to deploy the application on a university dedicated server with Red Hat Enterprise Linux Server release 7.2 (Maipo). (Note: RHEL does not let me install newer versions of Django and Python)
I had difficulty with installing mod_wsgi using the RHEL Apache and I ended up installing:
pip install -U mod_wsgi-httpd
pip install mod_wsgi
Everything works perfectly when I use the following setup:
python manage.py runmodwsgi --setup-only --port=80 --user=iman --group=root --server-root=/etc/mod_wsgi-express-80
However, when I want to setup HTTPS, using the following setup does not return any error message, but the website does not show up neither with http:// nor with https:// prefixes.
python manage.py runmodwsgi --setup-only --port=80 --user=iman --group=root --server-root=/etc/mod_wsgi-express-80 --https-only --https-port=443 --ssl-certificate-file=/etc/sslcert/iman.crt --ssl-certificate-key-file=/etc/sslcert/private/iman.key --ssl-ca-certificate-file=/etc/sslcert/certs/ca-bundle.crt --server-name=iman123.university.edu
Here are helpful resources that I found about mod_wsgi:
mod_wsgi docs
GitHub repository
Running HTTPS and client authentication with mod_wsgi-express
pip install mod_wsgi-express left out mod_ssl.so
Thank you so much for your time and concern and apologies in advance if I am doing something wrong.