flask + Apache + mod_wsgi deployment pit and climb the Windows environment

Foreword

Deployed in Windows Flaskreally is a challenge, but with patience + careful, you are sure to succeed.

I encountered when deploying several pits in this recording, easy access later.

Install python

Download the installation package python-3.6.5-amd64.exe, simply double-click to open the installation step by step, very simple.

Virtualenv installation and virtualenvwrapper

Installation virtualenv:

pip install virtualenv

Installation virtualenvwrapper:

pip install virtualenvwrapper-win

Configuration environment variable:

Open system environment variables, adding: WORKON_HOME=C:\virtualenvs, note that this directory is a virtual environment storage directory

After configuring the environment variables must restart cmd window, otherwise the environment variable does not take effect.

Commonly used commands:

  • New Virtual Environment:mkvirtualenv test_env_36
  • See all virtual environments:workon
  • Enter the virtual environment:workon test_env_36
  • Exit the virtual environment: Go to the directory where the virtual environment, such as: C:\virtualenvs\LibraFlaskPy36\Scripts, enter:deactivate
  • Activate the virtual environment: Go to the directory where the virtual environment, such as: C:\virtualenvs\LibraFlaskPy36\ScriptsInput:activate.bat

PS: I activate the virtual environment is common practice: workon test_env_36

Install mod_wsgi

In this website https://www.lfd.uci.edu/~gohlke/pythonlibs/to find the compiled package, enter the path to the installation package, enter the following command to install

pip install mod_wsgi-4.5.24+ap24vc14-cp36-cp36m-win_amd64.whl

Installing Apache

  1. Download the installation package: official website
  2. Extract the installation package, open cmda terminal, enter the Apache/bindirectory, enter the command: httpd -k installno error indicates that the installation was successful
  3. Double-click ApacheMonitor.exeto open the service management UI interface, you can Apachemanage the service.
  4. You can also use the command Apacheto manage service

Commonly used commands:

httpd -k start  # 启动
httpd -k stop  # 停止
httpd -k restart  # 重启

If there is an error, you can view the log: Apache/logs/error.log.

Apache installation pit encountered

Arranged apacheat Apache24/conf/httpd.confthe end of the following configuration is added, use mod_wsgi-express module-config > myconfig.txt, can be mod_uwsgiarranged:

# mod_wsgi 配置
LoadFile "c:/python36/python36.dll"
LoadModule wsgi_module "c:/virtualenvs/libraflaskpy36/lib/site-packages/mod_wsgi/server/mod_wsgi.cp36-win_amd64.pyd"
WSGIPythonHome "c:/virtualenvs/libraflaskpy36"

# 参考:https://dormousehole.readthedocs.io/en/latest/deploying/mod_wsgi.html
<VirtualHost *>
    ServerName 192.168.6.27:80    # 这里我少写了80端口,坑死了
    WSGIScriptAlias / C:\tools\ZLflask\Libra.wsgi
    <Directory C:\tools\ZLflask>
            # Order deny,allow
            # Allow from all
            Require all granted
    </Directory>
</VirtualHost>

The middle section of the configuration have to be changed, where the dead pit, out for a long time:

<Directory />
    AllowOverride none
    Require all denied
</Directory>

Into this:


<Directory />
    AllowOverride All    # 为了让别人访问到这个IP地址
    Require all granted
</Directory>

Modify the configuration of the certificate, remove ssl certification, because it is an internal company use, and does not require binding domain authentication, comment out the following sentence:

LoadModule ssl_module modules/mod_ssl.so

New app.wsgifile, write the following code:

# 添加虚拟环境的路径
activate_this = 'C:\\virtualenvs\\LibraFlaskPy36\\Scripts\\activate_this.py'
with open(activate_this) as file_:
    exec(file_.read(), dict(__file__=activate_this))

In the highlights here, configuration time, has been given:

[Tue Oct 30 19:28:59.312419 2018] [wsgi:error] [pid 5972:tid 1148] [client 192.168.6.27:50286]   File "C:/tools/ZLflask/Libra.wsgi", line 5, in <module>\r, referer: http://192.168.6.27/index
[Tue Oct 30 19:28:59.312419 2018] [wsgi:error] [pid 5972:tid 1148] [client 192.168.6.27:50286]     from app import app as application\r, referer: http://192.168.6.27/index
[Tue Oct 30 19:28:59.312419 2018] [wsgi:error] [pid 5972:tid 1148] [client 192.168.6.27:50286] ModuleNotFoundError: No module named 'app'\r, referer: http://192.168.6.27/index

Later, for a long time, add the following two lines of code, a success:

import sys
sys.path.insert(0, "C:\\tools\\ZLflask")

app.wsgi Complete code file:

# 添加虚拟环境的路径
activate_this = 'C:\\virtualenvs\\LibraFlaskPy36\\Scripts\\activate_this.py'
with open(activate_this) as file_:
    exec(file_.read(), dict(__file__=activate_this))

# 将你的项目路径添加到系统
import sys
sys.path.insert(0, "C:\\tools\\ZLflask")

Installation mod_uwsgipit

The installation of this error has been reported:

Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "C:\Users\Dell\AppData\Local\Temp\pip-install-pnicet59\mod-wsgi\setup.py", line 158, in <module>
        raise RuntimeError('No Apache installation can be found. Set the '
    RuntimeError: No Apache installation can be found. Set the MOD_WSGI_APACHE_ROOTDIR environment to its location.

    ----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in C:\Users\Dell\AppData\Local\Temp\pip-install-pnicet59\mod-wsgi\

Solution:

  1. Open this website https://www.lfd.uci.edu/~gohlke/pythonlibs/
  2. search for:mod_wsgi
  3. Download and install:pip install mod_wsgi-4.5.24+ap24vc14-cp36-cp36m-win_amd64.whl
  4. Restart Apache: httpd -k restartOkay.

Enjoy your code, good luck.

Guess you like

Origin www.cnblogs.com/DeaconOne/p/12664431.html