How to use Python 3 and Django with Apache?

2019-03-13 08:45发布

问题:

My goal is to set up Python 3 with Apache. My biggest problem is actually acquiring mod_python.so. For the life of me I found only one site where it could be downloaded (http://www.modpython.org/) and what I got was a bunch of build and install files.

I can find no guide explaining how to set up Python 3 with Apache. So if someone could walk me through it now, the internet will finally contain the solution!

回答1:

Django 1.6+ and mod_wsgi 3.4+ are required to use Python 3 with Apache. For more detail refer to scot's answer.



回答2:

These answers are no longer true of Django 1.6 - it supports python3. The mod_wsgi page says version 3.4 supports python 3. https://code.google.com/p/modwsgi/

Don't know if it all works at this point though (I will return and edit when I find out)!

The answer is YES it works!

I have an AWS EC3 Ubuntu instance running Python3, Django 1.5.6, Apache2.2 and mod_wsgi 3.4

Python 3.3.4:

sudo add-apt-repository ppa:fkrull/deadsnakes
sudo apt-get install python3.3
sudo apt-get install python3.3-dev python3.3-doc idle-python3.3

ppa:fkrull/deadsnakes is a apt repo has has multiple python versions available - see https://launchpad.net/~fkrull/+archive/deadsnakes

Then I added pip using the instructions at the pip page; http://pip.readthedocs.org/en/latest/installing.html. (remember your python is probably on your path as 'python3.3' at this point, plain 'python' will point at python 2.x!)

After that, virtualenv. Then I virtualenv'd the python installation. Upon activation and adding the environment's bin/ directory to the $PATH I've now got a clean python3.

Then, after I activated the virtual env, I did 'pip Django' and all my other necessary packages (which were quite a few). I have Django version 1.6.2 (I've been developing on this and running under python 3.3.3 on my Mac no problem).

The most trouble I had was installing lxml because it requires libxml2 and libxslt to be installed with apt-get (it is a wrapper around the C code) and it took me a couple of attempts to realise that they were not already installed (lxml compilation fails).

After tossing about getting my RDS database instance up and running and available (postgresql, beware mysql under python3, you'll get plenty of python db driver pain! but most of my issues were caused by me trying to understand the AWS security configuration), it was relatively plain sailing:

sudo apt-get install apache2 apache2-threaded-dev

That installs apache - and you need the dev packages for the next bit.

And that point, I tried using the apt package for mod_wsgi but I decided that the best thing to do was to compile and install it myself, following the instructions here - https://code.google.com/p/modwsgi/wiki/InstallationInstructions

I had no problems with configure, make, or make install. Make sure you compile it in your virtualenv activated environment.

You have to manually add the configuration to Apache's configuration:

# wsgi module
LoadModule wsgi_module /usr/lib/apache2/modules/mod_wsgi.so
# now configure it
<Directory /my/app/path>
 <Files wsgi.py>
  Order deny,allow
  Allow from all
 </Files>
</Directory>
WSGIScriptAlias / /my/app/path/wsgi.py
WSGIPythonPath /my/app:/path/to/the/virtual/env/lib/python3.3/site-packages

And in the broadest possible way, this is all now working.



回答3:

You can't unser python3 with django. From Django FAQ: http://docs.djangoproject.com/en/dev/faq/install/#can-i-use-django-with-python-3

Can I use Django with Python 3?

Not at the moment. Python 3.0 introduced a number of backwards-incompatible changes to the Python language, and although these changes are generally a good thing for Python’s future, it will be a while before most Python software catches up and is able to run on Python 3.0. For larger Python-based software like Django, the transition is expected to take at least a year or two (since it involves dropping support for older Python releases and so must be done gradually).

In the meantime, Python 2.x releases will be supported and provided with bug fixes and security updates by the Python development team, so continuing to use a Python 2.x release during the transition should not present any risk.



回答4:

1) The first thing we should do with our newly created project files is adjust the settings. Open the settings file with your text editor:

gedit myproject/settings.py

At the bottom of the file, we will add a line to configure this directory. Django uses the STATIC_ROOT setting to determine the directory where these files should go. We'll use a bit of Python to tell it to use a directory called "static" in our project's main directory:

STATIC_ROOT = os.path.join(BASE_DIR, "static/")

2) We can collect all of the static content into the directory location we configured by typing:

./manage.py collectstatic

You will have to confirm the operation. The static files will be placed in a directory called static within your project directory.

3)You need mod-wsgi adapter to configure Django in Apache install wsgi lib like below.

sudo apt-get install libapache2-mod-wsgi sudo a2enmod wsgi

4)At the bottom of file, /etc/apache2/sites-available/000-default.conf

WSGIPythonPath /var/www/html/myproject/ 
WSGIProcessGroup myproject
WSGIScriptAlias / /var/www/html/myproject/myproject/wsgi.py


  Alias /static /var/www/html/myproject/static/

<Directory /var/www/html/myproject/static>
    Require all granted
</Directory>

<Directory /var/www/html/myproject/myproject>
    <Files wsgi.py>
         Order deny,allow
        Require all granted
    </Files>
</Directory>

Alias /media/ /var/www/html/myproject/media/
<Directory /var/www/html/myproject/media>
Require all granted
</Directory> 
WSGIDaemonProcess myproject python-path=/var/www/html/myproject/

5)Restart the apache2 server:

sudo service apache2 restart

6)Now go to Localhost:

Bingo! You are all set for Django with apache.



回答5:

I faced a similar problem and here is how I solved it: To Install LAMP:

   sudo apt-get install lamp-server^
 // then do more from here:
   //https://help.ubuntu.com/community/ApacheMySQLPHP
 // If you will run python services then : 
       apt-get install python-mysqldb 
//and for python 3 install: 
   sudo apt-get install python3-pip python-dev build-essential
   pip3.4 install mysqlclient  // for mysql       connection 
   sudo apt-get install build-essential python-dev libmysqlclient-dev

   pip3 install virtualenvwrapper
   sudo nano ~/.bash_profile
  //Set location of virtualenvs by pasting this in above: 
   export WORKON_HOME=$HOME/.virtualenvs 
   source /usr/local/bin/virtualenvwrapper.sh
//Reload startup file:
  source ~/.bash_profile
   mkvirtualenv -p /usr/bin/python3 myprojectenv
   workon myprojectenv
//then you can install packages you want using pip
//and copy myproject here

To DEPLOY a Django 1.7 App on Ubuntu 14.04 LTS server with python3 and apache:

   sudo apt-get install libapache2-mod-wsgi-py3
 //then configure your apache virtualhosts( here our project is   //myproject    and environment is myprojectenv):

Configure your virtual host in Daemon mode which is recommended:

<VirtualHost *:80>
    ServerName yourdomain.com
    ServerAdmin youradmin@yourdomain.com

    DocumentRoot /home/user/.virtualenvs/myprojectenv/public_html


    <Directory /home/user/.virtualenvs/myprojectenv/myproject/myproject>

     <Files wsgi.py>
     Require all granted
    </Files>
    </Directory>

    ErrorLog /home/user/.virtualenvs/myprojectenv/logs/error.log
    CustomLog /home/user/.virtualenvs/myprojectenv/logs/access.log  combined
    WSGIScriptAlias /  /home/user/.virtualenvs/myprojectenv/myproject/myproject/wsgi.py

    WSGIDaemonProcess myproject python- path=/home/user/.virtualenvs/myprojectenv/prisec:/home/user/.virtualenvs/m yprojectenv/lib/python3.4/site-packages

    WSGIProcessGroup myproject
    Alias /static /home/user/.virtualenvs/myprojectenv/public_html/static

    <Directory /home/user/.virtualenvs/myprojectenv/public_html/static>
     Require all granted
    </Directory>
     Alias /media
  /home/user/.virtualenvs/myprojectenv/public_html/media
       <Directory        /home/user/.virtualenvs/myprojectenv/public_html/media>
     Require all granted
    </Directory>

Restart Apache . the directories in virtualhost configuration file must exist in the virtual environment created.