Ubuntu上安裝有python2.7,也安裝了python3.6,需要重新編譯mod_wsgi,與Python版本對應。
安裝主要參考一下文章:
https://blog.csdn.net/q_quanting/article/details/81940193
啟動apache2
sudo a2ensite 項目名稱.conf sudo service apache2 restart
查看錯誤:
cat /var/log/apache2/error.log
出錯
ImportError: No module named site
原因出在了mod_wsgi要與python的版本一致
系統中有兩個python版本,要指定所用的版本
python安裝位置指定,會出現以下錯誤:
Fatal Python error: Py_Initialize: Unable to get the locale encoding ImportError: No module named 'encodings'
具體錯誤的解決方法,可以參考下面頁面:
https://stackoverflow.com/questions/39171616/ubuntu-apache2-django-fatal-python-error-py-initialize-unable-to-get-the
WSGIPythonHome if being set should refer to the directory give by sys.prefix for the Python installation. Note that WSGIPythonHome cannot be used to refer to a Python installation or virtual environment which is a different version than what mod_wsgi was compiled for. Thus if mod_wsgi compiled for Python 2.7, you cannot use it to try and force use of Python 3.5.
https://stackoverflow.com/questions/46759730/django-mod-wsgi-fatal-python-error-py-initialize-no-module-named-encodings
https://github.com/GrahamDumpleton/mod_wsgi/issues/418
https://serverfault.com/questions/285229/python-django-wsgi-apache-importerror-no-module-named-site
https://www.pythonheidong.com/blog/article/178400/
https://modwsgi.readthedocs.io/en/develop/user-guides/quick-installation-guide.html
需要做的只有兩步:
第1步、按照不同的python版本,重新編譯mod_wsgi
Unpacking The Source Code
Source code tar balls can be obtained from:
下載mod_wsgi各種版本的位置:
After having downloaded the tar ball for the version you want to use, unpack it with the command:
tar xvfz mod_wsgi-X.Y.tar.gz
Replace ‘X.Y’ with the actual version number for that being used.
Configuring The Source Code
To setup the package ready for building run the “configure” script from within the source code directory:
./configure
The configure script will attempt to identify the Apache installation to use by searching in various standard locations for the Apache build tools included with your distribution called “apxs2” or “apxs”. If not found in any of these standard locations, your PATH will be searched.
Which Python installation to use will be determined by looking for the “python” executable in your PATH.
If these programs are not in a standard location, they cannot be found in your PATH, or you wish to use alternate versions to those found, the --with-apxs
and --with-python
options can be used in conjunction with the “configure” script:
./configure --with-apxs=/usr/local/apache/bin/apxs \
--with-python=/usr/local/bin/python
必須按照以上命令重新編譯,需要指定apxs2與python的位置,之后會自動生成mod_wsgi.so到apache2的安裝位置。
ubuntu apache apxs 安裝問題,可參考:
https://blog.csdn.net/shanliangliuxing/article/details/8730783?utm_source=blogxgwz7
sudo apt-get install apache2-dev
安裝完后查看安裝的路徑:
whereis apxs2
如果在安裝Mod_pythond的make過程出現這樣到錯誤:fatal error: Python.h: No such file or directory,可以安裝python2.7-dev(裝的是V2.7)解決,命令為:
sudo apt-get install python2.7-dev
Apxs的默認位置:usr/bin/apxs,但在ubuntu11版本中已經是usr/bin/apxs2
--with-apxs
option and specify either “/usr/sbin/apxs2-worker” or “/usr/sbin/apxs2-prefork”. This is necessary as the Linux distribtions allow installation of “dev” packages for both Apache MPM variants at the same time, whereas other Linux distributions do not.
If you have multiple versions of Python installed and you are not using that which is the default, you may have to organise that the PATH inherited by the Apache application when run will result in Apache finding the alternate version. Alternatively, the WSGIPythonHome directive should be used to specify the exact location of the Python installation corresponding to the version of Python compiled against. If this is not done, the version of Python running within Apache may attempt to use the Python modules from the wrong version of Python.
Building The Source Code
Once the package has been configured, it can be built by running:
make
If the mod_wsgi source code does not build successfully, see:
If successful, the only product of the build process that needs to be installed is the Apache module itself. There are no separate Python code files as everything is done within C code compiled into the Apache module.
To install the Apache module into the standard location for Apache modules as dictated by Apache for your installation, run:
make install
Installation should be done as the ‘root’ user or ‘sudo’ command if appropriate.
If you want to install the Apache module in a non standard location dictated by how your operating system distribution structures the configuration files and modules for Apache, you will need to copy the file manually into place.
If installing the Apache module by hand, the file is called ‘mod_wsgi.so’. The compiled Apache module can be found in the “.libs” subdirectory. The name of the file should be kept the same when copied into its appropriate location.
第2步、多個Python版本問題,告訴apache,mod_wsgi對應的python版本
Multiple Python Versions
Where there are multiple versions of Python installed on a system and it is necessary to ensure that a specific version is used, the --with-python
option can be supplied to ‘configure’ when installing mod_wsgi:
./configure --with-python=/usr/local/bin/python2.5
This may be necessary where for example the default Python version supplied with the system is an older version of Python. More specifically, it would be required where it isn’t possible to replace the older version of Python outright due to operating system management scripts being dependent on the older version of Python and not working with newer versions of Python.
Where multiple versions of Python are present and are installed under the same directory, this should generally be all that is required. If however the newer version of Python you wish to use is in a different location, for example under ‘/usr/local’, it is possible that when Apache is started that it will not be able find the Python library files for the version of Python you wish to use.
This can occur because the Python library when initialised determines where the Python installation resides by looking through directories specified in the ‘PATH’ environment variable for the ‘python’ executable and using that as base location for calculating installation prefix. Specifically, the directory above the directory containing the ‘python’ executable is taken as being the installation prefix.
When the Python which should be used is installed in a non standard location, then that ‘bin’ directory is unlikely to be in the ‘PATH’ used by Apache when it is started. As such, rather than find ‘/usr/local/bin/python’ it would instead find ‘/usr/bin/python’ and so use ‘/usr’ rather than the directory ‘/usr/local/’ as the installation prefix.
When this occurs, if under ‘/usr’ there was no Python installation of the same version number as Python which should be used, then normally:
'import site' failed; use -v for traceback
would appear in the Apache error log file when Python is first being initialised within Apache. Any attempt to make a request against a WSGI application would also result in errors as no modules at all except for inbuilt modules, would be able to be found when an attempt is made to import them.
Alternatively, if there was a Python installation of the same version, albeit not the desired installation, then there may be no obvious issues on startup, but at run time you may find modules cannot be found when being imported as they are installed into a different location than that which was being used. Even if equivalent module is found, it could fail at run time in subtle ways if the two Python installations are of same version but at the different locations are compiled in different ways, or if it is a third party module and they are different versions and so API is different.
In this situation it will be necessary to explicitly tell mod_wsgi where the Python executable for the version of Python which should be used, is located. This can be done using the WSGIPythonHome directive:
WSGIPythonHome /usr/local
The value given to the WSGIPythonHome directive should be a normalised path corresponding to that defined by the Python {{{sys.prefix}}} variable for the version of Python being used and passed to the --with-python
option when configuring mod_wsgi:
>>> import sys >>> sys.prefix '/usr/local'
在 項目名稱.conf 當中,加入WSGIPythonHome /usr/local,地址使用sys.prefix顯示的,這個非常關鍵。
An alternative, although less desirable way of achieving this is to set the ‘PATH’ environment variable in the startup scripts for Apache. For a standard Apache installation using ASF structure, this can be done by editing the ‘envvars’ file in same directory as the Apache executable and adding the alternate bin directory to the head of the ‘PATH’:
PATH=/usr/local/bin:$PATH
export PATH