[PYTHON] CentOS8 + Apache2.4 + pyenv + mod_wsgi + Django project deployment

I had a hard time so I made a note.

Premise

--Create a virtual CentOS8 environment using VMware and build a Web server there. --The Django project has been created. Use a shared folder or FTP to go to CentOS (this time / var / www / projectname /) together with the project folder.

Apache installation

$ dnf list | grep httpd

Besides httpd body

You can see that there are such things. Install these together with the main unit as you will need them in the future.

$ dnf -y install httpd httpd-tools httpd-devel httpd-manual
$ dnf list --installed | grep httpd  #Check if it is installed

$ systemctl start httpd
$ systemctl status httpd  #Check if Apache can be started
$ systemctl enable httpd  #Enable automatic startup

Set up the firewall

$ firewall-cmd --add-service=http --zone=public --permanent
$ firewall-cmd --reload
$ firewall-cmd --list-all  #Check if http is added to services

Operation check

First, access http: // localhost /. OK if the Apache test page is displayed. Next, place the HTML file and check if it is displayed.

$ touch /var/www/html/index.html
$ vi /var/www/html/index.html  #Write appropriate HTML

If you access http: // localhost / and HTML is displayed, it's OK.

Edit Apache config file

Try to specify the domain using / etc / hosts.

$ vi /etc/hosts  #127.0.0.Example on line 1.add com
$ mkdir /var/www/example.com
$ touch /var/www/example.com/index.html
$ vi /var/www/example.com/index.html  #Write appropriate HTML

Edit Apache conf. Since we plan to use SSL or use multiple domains later, set it as VirtualHost.

$ touch /etc/httpd/conf.d/virtual.conf
$ vi /etc/httpd/conf.d/virtual.conf
$ service httpd configtest  #You can check the syntax of the configuration file

conf:/etc/httpd/conf.d/virtual.conf


<VirtualHost *:80>
  DocumentRoot /var/www/example.com
  ServerName example.com
  <Directory "/var/www/example.com">
    Require all granted
  </Directory>
</VirtualHost>

At the time of config test

Could not reliably determine the server's fully qualified domain name, using localhost.localdomain. Set the 'ServerName' directive globally to suppress this message

When appears, specify the ServerName in httpd.conf as you were told.

/etc/httpd/conf/httpd.conf (around lines 98-99)


ServerName example.com:80

Finally restart Apache and check the operation

$ systemctl restart httpd

Now when you visit http: // localhost / or http://example.com you will see /var/www/example.com/index.html.

pyenv-Python installation

pyenv installation

$ dnf -y install git  #Install git to do a git clone
$ git clone https://github.com/pyenv/pyenv.git /var/www/pyenv
$ vi ~/.bash_profile  #Add the following

~/.bash_profile (additional note)


export PYENV_ROOT="/var/www/pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init -)"

Initially, the installation destination of pyenv was /root/.pyenv, but if it is below the root, it seems that it can be accessed only with root authority (Apache can not be accessed), so Apache has the authority to install/ Re-installed under var / www /.

source ~/.bash_profile  #Apply bash
pyenv --version  #Operation check

Python 3.x.x installation

From here, we will proceed while correcting the trajectory while issuing an error, so if you are going to practice this procedure, we recommend that you read it to the end and then try it.

First, try installing Python normally

$ pyenv install 3.x.x

Then I got an error like this.

zipimport.ZipImportError: can't decompress data; zlib not available

So install zlib.

$ dnf -y install zlib zlib-devel

Perhaps zlib is already installed. I have to install some packages in the future, but I also need to install -devel together with the main body.

I regain my mind and install Python, but I get an error again.

(Excerpt)


WARNING: The Python bz2 extension was not compiled. Missing the bzip2 lib?
WARNING: The Python readline extension was not compiled. Missing the GNU readline lib?
ERROR: The Python ssl extension was not compiled. Missing the OpenSSL lib?

There are ERROR and WARNING, but both need to be resolved in the end, so install the missing package.

python


$ dnf -y install bzip2 bzip2-devel readline readline-devel openssl openssl-devel

Install Python again. It succeeds, but a warning is given.

WARNING: The Python sqlite3 extension was not compiled. Missing the SQLite3 lib?

This will also be a problem later, so install it.

$ dnf -y install sqlite sqlite-devel

Now Python is finally installed.

pyenv versions  #3.x.Make sure x is installed
pyenv global 3.x.x  #Apply Python version to the whole
python --version  #3.x.Confirm that x works

#Also update and check pip
pip install --upgrade pip
pip --version

Django installation

pip install django

It's easy to forget.

Introducing mod_wsgi

mod_wsgi is obtained by wget from Download files of PyPI. It seems that there are various ways to obtain mod_wsgi, but for the time being, I have succeeded in this method, so I will describe it.

$ cd /var/www/
$ wget https://files.pythonhosted.org/packages/...Abbreviation.../mod_wsgi-x.x.x.tar.gz
$ tar -zxvf mod_wsgi-x.x.x.tar.gz 
$ cd mod_wsgi-x.x.x/
$ ./configure --with-python=/var/www/pyenv/versions/3.x.x/bin/python

When I try to type the make command here, I get the following error:

error: /usr/lib/rpm/redhat/redhat-hardened-cc1:There is no such file or directory

So install redhat-rpm-config again.

$ dnf -y install redhat-rpm-config

When I run make`` make install again, I get the following error

'xxx' can not be used when making a shared object; recompile with -fPIC

This error can be resolved by adding CFLAGS =" -fPIC " when installing Python. So I reinstalled Python.

$ CFLAGS="-fPIC" pyenv install 3.x.x
$ make
$ make install

This will generate /etc/httpd/modules/mod_wsgi.so. Finally, the WSGI installation is complete, so put the project file as / var / www / projectname, edit the Apache settings again, and restart.

conf:/etc/httpd/conf.d/virtual.conf


LoadModule wsgi_module /etc/httpd/modules/mod_wsgi.so
WSGIPythonHome /var/www/pyenv/versions/3.x.x
WSGIPythonPath /var/www/pyenv/versions/3.x.x/lib/python3.x/site-packages
WSGIPythonPath /var/www/projectname

<VirtualHost *:80>
  DocumentRoot /var/www/projectname
  ServerName example.com

  WSGIScriptAlias / /var/www/projectname/projectname/wsgi.py
  <Directory "/var/www/projectname">
    Require all granted
  </Directory>

  #Collect static on the django project side
  Alias /static/ /var/www/projectname/static/
  <Directory "/var/www/projectname/static">
    Require all granted
  </Directory>
</VirtualHost>

WSGIPythonHome is a Python script that sys.prefix points to. One of the WSGIPythonPath is the sys.path that ends with site-packages. The other is the home directory of the Django project, which seems to need to be described both.

From here, proceed while checking the error below.

tail /etc/httpd/logs/error_log

The following error will appear immediately, so take action.

failed to map segment from shared object
Unable to configure formatter 'django.server'

These can be resolved by disabling SELinux and making the owner and group of files under / var / www / apache.

setenforce 0
chown -R apache:apache /var/www/

After restarting Apache, you should see your Django project at http: // localhost. When I access http://example.com, I get a DisallowedHost error on the Django side, but as the error message says, I can see it by adding ʻexample.com to ʻALLOWED_HOSTS in settings.py.

Recommended Posts

CentOS8 + Apache2.4 + pyenv + mod_wsgi + Django project deployment
Django --Apache mod_wsgi virtualhost deployment
CentOS 6.4 with Python 2.7.3 with Apache with mod_wsgi and Django
Django / Apache / mod_wsgi: No module named importlib
Django + Apache with mod_wsgi on Windows Server 2016
CentOS 7: Enable pyenv with Apache (httpd) CGI
Run django applications on Windows + Apache + mod_wsgi + services.
CentOS8 --Play --Django
CentOS8 --Install --Django
Django Project Baseline
pyenv + pyenv-virtualenv (CentOS7)
Publish your Django app on Amazon Linux + Apache + mod_wsgi
heroku deployment memo (Django)
Django project environment construction
Django --start project without start project
[Django] Rename the project
Start a Django project