[PYTHON] Django Getting Started: 3_Apache integration

table of contents

Django Getting Started: 1_Environment Building Django Getting Started: 2_Project Creation Beginning with Django: 3_Apache integration Beginning with Django: 4_MySQL integration

Last time, I confirmed the startup of Django from the browser on the simple development server. Considering that it will be released later, it is necessary to cooperate with a web server such as apache.

Here, it is in the form of Apache + mod_wsgi + Django. This is the officially recommended method.

How to use Django with Apache and mod_wsgi?

However, I searched on various websites, but it didn't work out and it was a difficult place. I'm sure other people will be addicted to reading this article somewhere.

If it doesn't work, I recommend you to read the formula once. It's tough, but the primary information is the most reliable.

mod_wsgi Official

Then I will write it down as much as possible.

environment

A little commentary

What is Apache

It is a web server. Probably the most used. By default, it does not have the ability to interpret Python, so you will need to customize it separately.

What is mod_wsgi

It seems that wsgi is read as Wisgi. It's an Apache module that allows you to run Python.

Apache installation

If you don't have apache on your server, install it first.

yum install httpd httpd-devel
systemctl start httpd
systemctl enable httpd

Just in case, type the IP address in your browser and check if apache is running.

mod_wsgi installation

It is necessary to associate with Python at the time of installation. If you are using pyenv you have to tell it.

If you inadvertently install it easily with yum, it will be linked with the built-in Python and you will have to start over, so be careful.

Download and unzip the source.

wget https://github.com/GrahamDumpleton/mod_wsgi/archive/4.5.14.tar.gz
tar -zxvf 4.5.14.tar.gz
cd mod_wsgi-4.5.14/
./configure --with-python=/usr/local/bin/pyenv/versions/anaconda3-4.3.0/bin/python
make
make install

mod_wsgi installation error

You may get the following error.

/bin/ld: /usr/local/bin/pyenv/versions/3.6.0/lib/libpython3.6m.a(abstract.o): relocation R_X86_64_32S against `_Py_NotImplementedStruct' can not be used when making a shared object; recompile with -fPIC

In this case you need to recompile Python.

CONFIGURE_OPTS="--enable-shared" CFLAGS="-fPIC" pyenv install X.X.X

Then install it again. Reset the settings once with make clean.

make clean
./configure CFLAGS=-fPIC --enable-shared
make
make install

Apache settings

Added to /etc/httpd/conf/httpd.conf,

The whole description looks like this. Please replace as appropriate. If you create a project called xxx under / home / django /.

LoadModule wsgi_module modules/mod_wsgi.so
WSGIScriptAlias / /home/django/xxx/xxx/wsgi.py
WSGIPythonHome /usr/local/bin/pyenv/versions/anaconda3-4.3.0/
WSGIPythonPath /home/django/xxx

<Directory /home/django/xxx/xxx>
  <Files wsgi.py>
    Require all granted
  </Files>
</Directory>

Alias /static/ /home/django/xxx/static/
<Directory /home/django/xxx/static>
  Require all granted
</Directory>

I will explain each item.

*Caution In Apache 2.4 series Require all granted However, in earlier versions, write:

Order allow,deny
Allow from all

Satisfy mod_wsgi.so dependencies

As it is, mod_wsgi.so needs python, but I can't find it. Check the details with the ldd command.

ldd /etc/httpd/modules/mod_wsgi.so
	linux-vdso.so.1 =>  (0x00007fff8c9fe000)
	libpython3.6m.so.1.0 => not found
	libpthread.so.0 => /lib64/libpthread.so.0 (0x00007f4c9e548000)
	libdl.so.2 => /lib64/libdl.so.2 (0x00007f4c9e344000)
	libutil.so.1 => /lib64/libutil.so.1 (0x00007f4c9e141000)
	librt.so.1 => /lib64/librt.so.1 (0x00007f4c9df38000)
	libm.so.6 => /lib64/libm.so.6 (0x00007f4c9dc36000)
	libc.so.6 => /lib64/libc.so.6 (0x00007f4c9d875000)
	/lib64/ld-linux-x86-64.so.2 (0x00007f4c9e9a8000)

I can't find libpython3.6m.so.1.0. Search under your Python directory and paste the symbolic link.

ls /usr/local/bin/pyenv/versions/anaconda3-4.3.0/lib/ | grep python
libpython3.6m.so
libpython3.6m.so.1.0
libpython3.so
python3.6
ln -s /usr/local/bin/pyenv/versions/anaconda3-4.3.0/lib/libpython3.6m.so.1.0 /lib64/

Apache startup confirmation => Error resolution

Once you reach this point, restart Apache once. systemctl restart httpd

Probably an error will occur, so access the browser while looking at the error log. tail -f /var/log/httpd/error_log

The cause is described for each error.

WSGIPythonHome settings are incorrect

[Thu Mar 09 16:32:51.315916 2017] [core:notice] [pid 9301] AH00094: Command line: '/usr/sbin/httpd -D FOREGROUND'
Could not find platform independent libraries <prefix>
Could not find platform dependent libraries <exec_prefix>
Consider setting $PYTHONHOME to <prefix>[:<exec_prefix>]
Fatal Python error: Py_Initialize: Unable to get the locale encoding
ModuleNotFoundError: No module named 'encodings'

Current thread 0x00007ff5dd6d8840 (most recent call first):
Could not find platform independent libraries <prefix>
Could not find platform dependent libraries <exec_prefix>
Consider setting $PYTHONHOME to <prefix>[:<exec_prefix>]
Fatal Python error: Py_Initialize: Unable to get the locale encoding
ModuleNotFoundError: No module named 'encodings'

Check the location of WSGIPythonHome. Make sure the Python you want to use is from pyenv, the system, and the version.

Not enough modules

[Thu Mar 09 15:53:52.797174 2017] [wsgi:error] [pid 9535] [client 192.168.10.1:65448] mod_wsgi (pid=9535): Target WSGI script '/home/django/plagiarism/plagiarism/wsgi.py' cannot be loaded as Python module.
[Thu Mar 09 15:53:52.797231 2017] [wsgi:error] [pid 9535] [client 192.168.10.1:65448] mod_wsgi (pid=9535): Exception occurred processing WSGI script '/home/django/plagiarism/plagiarism/wsgi.py'.
[Thu Mar 09 15:53:52.800163 2017] [wsgi:error] [pid 9535] [client 192.168.10.1:65448] Traceback (most recent call last):
[Thu Mar 09 15:53:52.800196 2017] [wsgi:error] [pid 9535] [client 192.168.10.1:65448]   File "/usr/local/bin/pyenv/versions/anaconda3-4.3.0/lib/python3.6/site-packages/django/db/backends/mysql/base.py", line 25, in <module>
[Thu Mar 09 15:53:52.800199 2017] [wsgi:error] [pid 9535] [client 192.168.10.1:65448]     import MySQLdb as Database
[Thu Mar 09 15:53:52.800213 2017] [wsgi:error] [pid 9535] [client 192.168.10.1:65448] ModuleNotFoundError: No module named 'MySQLdb'

You should import MySQLdb. Let's install it. Note that the name is not the same. pip install mysqlclient

Python cannot be specified

[Thu Mar 09 16:46:51.598855 2017] [mpm_prefork:notice] [pid 9476] AH00163: Apache/2.4.6 (CentOS) mod_wsgi/4.5.14 Python/xxx configured -- resuming normal operations

If the Python / xxx part is different from the version of Python you want to use, mod_wsgi and the Python you want to use are not linked. Let's reinstall mod_wsgi once.

Mod_wsgi.so dependency is not satisfactory

March 12 15:42:31 ip-172-31-32-130 systemd[1]: Starting The Apache HTTP Server...
March 12 15:42:31 ip-172-31-32-130 httpd[22078]: httpd: Syntax error on line 353 of /etc/httpd/conf/httpd.conf: Syntax error on line ...: Cannot load modules/mod_wsgi.so into server: libpython3.6m.so.1.0: cannot open shared object file: No such file or directory

mod_wsgi.so needs python, but I can't find it. Check the details with the ldd command.

ldd /etc/httpd/modules/mod_wsgi.so
	linux-vdso.so.1 =>  (0x00007fff8c9fe000)
	libpython3.6m.so.1.0 => not found
	libpthread.so.0 => /lib64/libpthread.so.0 (0x00007f4c9e548000)
	libdl.so.2 => /lib64/libdl.so.2 (0x00007f4c9e344000)
	libutil.so.1 => /lib64/libutil.so.1 (0x00007f4c9e141000)
	librt.so.1 => /lib64/librt.so.1 (0x00007f4c9df38000)
	libm.so.6 => /lib64/libm.so.6 (0x00007f4c9dc36000)
	libc.so.6 => /lib64/libc.so.6 (0x00007f4c9d875000)
	/lib64/ld-linux-x86-64.so.2 (0x00007f4c9e9a8000)

I can't find libpython3.6m.so.1.0. Search under your Python directory and paste the symbolic link.

ls /usr/local/bin/pyenv/versions/anaconda3-4.3.0/lib/ | grep python
libpython3.6m.so
libpython3.6m.so.1.0
libpython3.so
python3.6
ln -s /usr/local/bin/pyenv/versions/anaconda3-4.3.0/lib/libpython3.6m.so.1.0 /lib64/

Is the version of OpenSSL old?

If you access the Django page but get the following error:

You are linking against OpenSSL 1.0.0, which is no longer support by the OpenSSL project. You need to upgrade to a newer version of OpenSSL.

It seems that the display is different from the version confirmed by ʻopenssl version`.

I've searched a lot, but have you used Anaconda's openssl in the same situation? [About pyenv's support for Anaconda / Miniconda](http://qiita.com/yyuu@github/items/7de957838437eaecdc8a#pyenv-%E3%81%AB%E3%81%8A%E3%81%91%E3%82 % 8B-anacondaminiconda-% E5% AF% BE% E5% BF% 9C% E3% 81% AE% E5% 95% 8F% E9% A1% 8C% E7% 82% B9)

pip install -U cryptography Solved with.

Publishing static files

Not Found: /static/admin/css/base.css, referer: http://xxx.xxx.xxx.xxx/

If your development server uses templates provided by Django, When publishing on a web server, you need to set it again. Let's set this even if there is no error.

You can copy the Django files under your project directory or put a symbolic link on them, but Django has its own commands, so use that.

Decide where to put the static files in settings.py in the project directory as follows. STATIC_ROOT = '/home/django/xxx/static/' It will copy the static file to the directory specified by the following command. python manage.py collectstatic


Error resolution is a dot tired, but you can not publish unless you overcome this. Just remembering it made me painful.


mod_wsgi uninstall

If you want to change the version of Python to use, you need to uninstall mod_wsgi once and follow the above steps again. To uninstall it, just delete mod_wsgi.so.

reference

vhosts Eventually you will get a domain and use apache vhosts. Make a note of only the settings that worked for the time being.

httpd.conf


LoadModule wsgi_module modules/mod_wsgi.so
WSGIPythonHome /usr/local/bin/pyenv/versions/anaconda3-4.3.0/

vhosts.conf


<VirtualHost *:80>
  ServerName  MY_DOMAIN
  WSGIDaemonProcess PROCESS_GROUP user=apache group=apache python-path=/path/to/project
  WSGIProcessGroup PROCESS_GROUP
  WSGIScriptAlias / /path/to/project/app/wsgi.py process-group=PROCESS_GROUP

  <Directory /path/to/project/app/>
    <Files wsgi.py>
      Require all granted
    </Files>
  </Directory>

  Alias /static/ /path/to/project/static/
  <Directory /path/to/project/static/>
    Require all granted
  </Directory>
  CustomLog  /path/to/project/log/access_log common
  ErrorLog   /path/to/project/log/error_log

</VirtualHost>

Read as appropriate according to the environment.

Recommended Posts

Django Getting Started: 3_Apache integration
Django Getting Started: 4_MySQL Integration
Getting started with apache2
Getting Started with Django 1
Getting Started with Django 2
Django Getting Started: 2_ Project Creation
Django Getting Started: 1_Environment Building
Getting Started with Python Django (1)
Getting Started with Python Django (4)
Getting Started with Python Django (3)
Getting Started with Python Django (6)
Getting Started with Django with PyCharm
Getting Started with Python Django (5)
Getting started on Docker Apache Hadoop
Grails getting started
Django Getting Started Part 2 with eclipse Plugin (PyDev)
Django Getting Started Part 3 about Python3 & MySQL Connector
Django 1.11 started with Python3.6
Getting started with Android!
1.1 Getting Started with Python
Getting Started with Golang 2
Getting Started with Golang 1
Getting Started with Python
Getting Started with Optimization
Getting Started with Golang 3
Getting Started with Numpy
Getting started with Spark
Getting Started with Python
Getting Started with Pydantic
Getting Started with Golang 4
Getting Started with Jython
Materials to read when getting started with Apache Beam
Translate Getting Started With TensorFlow
Getting Started with Python Functions
Getting Started with Tkinter 2: Buttons
Get started with Django! ~ Tutorial ⑤ ~
[Linux] [Initial Settings] Getting Started
Getting Started with PKI with Golang ―― 4
Python3 + Django ~ Mac ~ with Apache
Get started with Django! ~ Tutorial ④ ~
Getting started and using sample class-based generic views in Django
Get started with Django! ~ Tutorial ⑥ ~
Django --Apache mod_wsgi virtualhost deployment
Python3 | Getting Started with numpy
Getting Started with Heroku-Viewing Hello World in Python Django with Raspberry PI 3
Getting Started with Python responder v2
Getting Started with Git (1) History Storage
Getting started with Sphinx. Generate docstring with Sphinx
Getting Started with Python Web Applications
Getting Started with Python for PHPer-Classes
Getting Started with Sparse Matrix with scipy.sparse
Getting Started with Julia for Pythonista
Getting Started with Python Basics of Python
How to get started with Django
Getting Started with Cisco Spark REST-API
Getting started with USD on Windows
Getting Started with Python Genetic Algorithms
Getting started with Python 3.8 on Windows
Getting Started with Python for PHPer-Functions
Getting Started with CPU Steal Time