porting django project to apache

I have Django running on a server with Ubuntu/Apache. I tried to upload my test project from my development server to the new server. Upon running python manage.py runserver 0.0.0.0:8000 I get:

ImportError: Couldn’t import Django. Are you sure it’s installed and available on your PYTHONPATH environment variable? Did you forget to activate a virtual environment?

I did not forget to run the virtual environment. Obviously I’ve done something wrong but i don’t know what.

Thanks.

You should not use runserver in a production deployment environment.

The docs at How to deploy with WSGI | Django documentation | Django describe three options for deploying your project, using uWSGI, gunicorn, and mod_wsgi, each with links to more detailed pages.

(We use uWSGI with nginx as our standard deployment environment on the server, but any of the options will work.)

Side note: Digital Ocean has a number of specific blog posts describing each of these in a more “prescriptive” form. See How To Serve Django Applications with Apache and mod_wsgi on Ubuntu 16.04 | DigitalOcean as one example. They have others for the other combinations.

I’m trying to get a handle on this, but it’s not working. I don’t see any of the documentation explaining the stuff I do not understand. Everything I’ve read explains how to get Django working under different environments. I can do that. What none if it explains is how to PORT a Django app from a development server to a production server. So I’ll try and make my question as clear as possible.

Suppose I have a development server, and I do the following:

Create a virtual environment:

virtualenv env1

Activate the venv:

source env1/bin/activate

Install Django:

pip3 install django

and all the MySQL stuff:

sudo apt-get install python3-dev default-libmysqlclient-dev build-essential pkg-config

sudo apt install libffi-dev

pip install mysqlclient

I start a Django project:

`` django-admin startproject testproject 1 .

Alter my settings.py as necessary.

Then I test it:

python manage.py runserver X.X.X.X :8000

and all is well with my test project.

Now on to the production server. I install and configure Apache, set up my database.

Then:

Create a virtual environment:

virtualenv env2

Activate the venv:

source env2/bin/activate

Install Django:

pip3 install django

and all the MySQL stuff:

sudo apt-get install python3-dev default-libmysqlclient-dev build-essential pkg-config

sudo apt install libffi-dev

pip install mysqlclient

I start a Django project:

`` django-admin startproject testproject 2 .

Alter my settings.py as necessary.

If I configure the apache files properly, it will serve my Django project. That’s all fine. And that brings us to where I’m stuck. What do I do now to port my testproject1 to the Apache server? It cannot be as simple as copying the testproject1 directory over, can it? What are the steps here? If the documentation explains it, I’ve missed it. Please elaborate.

So how do you have Apache configured? Are you going to use mod_wsgi, or are you going to proxy the request through to something like uwsgi or gunicorn?

Creating the virtual environment is fine - in many cases you will be able to use it - but the actual answer depends upon how you’re going to define how Apache is going to interact with your application.

However, there is no need or value to activating the virtual environment in your login shell. “You” are not going to be running your Django process.

This is where you typically do not do this in a production environment. As far as the project code itself is concerned, yes, you copy your entire project code to the appropriate directory on the server. (Again, what that “appropriate directory” is going to be will depend upon how you’re configuring Apache to work with your project.)

As far as moving the project code itself to the production server, yes, that’s it.

Or, in my case, my code is checked into git. On my server, I do a git pull.

The issues involved with deployment generally have very little to do with your code itself. You do have the settings issues to attend to - static files, media files (if necessary), database connection strings, and possibly a couple others. Beyond that, all the work is external to your project itself. Properly configuring Apache. Configuring the database, Configuring how your application is going to run, running migrate and collectstatic, starting the server processes.

Forgive the late reply, and thanks for the help.

I’m using mod_wsgi for Apache.

If I understand correctly, I can just copy my project to the server, change the settings in Apache accordingly, and it should work. OK. I think I have just one more hiccup first. What if when I set up my testproject on the Apache server, the Django version is different (newer) than my original testproject? Will this matter? If so, what is the correct way to fix it? Thanks again!

You’ll want to upgrade your test system to verify that it will work in the deployed environment. There are always new features being added and old features being changed or removed. You will want to find this out before deploying this to your live environment.

That’s perfect! Thanks for helping me get this all sorted out!