[Django] Launch WSGI in Apache Server

Django 是一個以 Python 為基底開發網站服務的框架,近年來越來越多開發人員使用 Python 語言,所以要切入網站或者是 API 的開發,Django 會是一個很好的選擇,之前檸檬爸初學 Django 寫過一篇初淺的介紹文,後來,真正在進入 Production 階段的時候遇到一些問題,本篇紀錄在部署 Django 到 Apache Server 上的時候遇到的挑戰。

部署 Django 建議使用 WSGI

在學習 Django 的時候,最快速實測的方法是利用 python3 manage.py runserver 的指令啟動 Django,但是一旦進入 Production,使用 Apache WSGI 模組部署 Django 將可以享受文章中提到的 Flexibility, Scalibility, Speed, Simple, Middleware Reuse 的好處。

前置作業:安裝 Apache 模組 mod_wsgi

在透過 Python Django 啟動網頁服務之前,在預計要安裝網頁的伺服器上,需要安裝 Apache 的 mod_wsgi 模組,以 CentOS 為例,可以利用以下的指令安裝:

yum install httpd-devel

如果沒有安裝就直接在 Python 內部安裝 mod-wsgi 套件的話,會收到以下的錯誤訊息:

[pipenv.exceptions.InstallError]: Collecting mod-wsgi==4.9.3 (from -r /tmp/pipenv-gjcpkpa8-requirements/pipenv-ga6ba_n2-hashed-reqs.txt (line 17))
[pipenv.exceptions.InstallError]:   Downloading mod_wsgi-4.9.3.tar.gz (497 kB)
[pipenv.exceptions.InstallError]:      ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 497.2/497.2 kB 39.9 MB/s eta 0:00:00
[pipenv.exceptions.InstallError]:   Preparing metadata (setup.py): started
[pipenv.exceptions.InstallError]:   Preparing metadata (setup.py): finished with status 'error'
[pipenv.exceptions.InstallError]: error: subprocess-exited-with-error
[pipenv.exceptions.InstallError]:
[pipenv.exceptions.InstallError]:   × python setup.py egg_info did not run successfully.
[pipenv.exceptions.InstallError]:   │ exit code: 1
[pipenv.exceptions.InstallError]:   ╰─> [6 lines of output]
[pipenv.exceptions.InstallError]:       Traceback (most recent call last):
[pipenv.exceptions.InstallError]:         File "<string>", line 2, in <module>
[pipenv.exceptions.InstallError]:         File "<pip-setuptools-caller>", line 34, in <module>
[pipenv.exceptions.InstallError]:         File "/tmp/pip-install-ydeem11k/mod-wsgi_f7d0a10c54d04a8d8b13a43f3d8e0c1f/setup.py", line 88, in <module>
[pipenv.exceptions.InstallError]:           raise RuntimeError('The %r command appears not to be installed or '
[pipenv.exceptions.InstallError]:       RuntimeError: The 'apxs' command appears not to be installed or is not executable. Please check the list of prerequisites in the documentation for this package and install any missing Apache httpd server packages.
[pipenv.exceptions.InstallError]:       [end of output]

安裝完 httpd-devel 還是會遇到以下的錯誤訊息,可能是 Python 在安裝的時候哪邊有問題,需要重新 Compile Python 的程式。

[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(abstract.o): relocation R_X86_64_32S against symbol `_Py_NotImplementedStruct' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(boolobject.o): relocation R_X86_64_32S against symbol `_Py_FalseStruct' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(bytearrayobject.o): relocation R_X86_64_32 against symbol `_Py_NoneStruct' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(bytesobject.o): relocation R_X86_64_32 against symbol `_Py_NoneStruct' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(call.o): relocation R_X86_64_32 against `.rodata.str1.1' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(capsule.o): relocation R_X86_64_32 against `.rodata.str1.1' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(exceptions.o): relocation R_X86_64_32S against symbol `_Py_NoneStruct' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(genericaliasobject.o): relocation R_X86_64_32 against `.rodata.str1.1' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(fileobject.o): relocation R_X86_64_32 against symbol `_Py_NoneStruct' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(floatobject.o): relocation R_X86_64_32 against `.rodata.str1.1' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(frameobject.o): relocation R_X86_64_32 against symbol `_Py_NoneStruct' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(iterobject.o): relocation R_X86_64_32 against symbol `_Py_NoneStruct' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(listobject.o): relocation R_X86_64_32 against symbol `_Py_NoneStruct' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(longobject.o): relocation R_X86_64_32S against `.rodata' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(dictobject.o): relocation R_X86_64_32S against `.text.hot' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(memoryobject.o): relocation R_X86_64_32 against `.rodata.str1.8' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(methodobject.o): relocation R_X86_64_32 against symbol `_Py_NoneStruct' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(moduleobject.o): relocation R_X86_64_32 against `.rodata.str1.8' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(object.o): relocation R_X86_64_32 against `.rodata.str1.1' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(obmalloc.o): relocation R_X86_64_32S against `.bss' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(picklebufobject.o): relocation R_X86_64_32 against symbol `_Py_NoneStruct' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(rangeobject.o): relocation R_X86_64_32 against symbol `_Py_NoneStruct' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(setobject.o): relocation R_X86_64_32S against `.data' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(sliceobject.o): relocation R_X86_64_32 against `.rodata.str1.1' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(structseq.o): relocation R_X86_64_32 against `.data' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(tupleobject.o): relocation R_X86_64_32 against `.rodata.str1.8' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(typeobject.o): relocation R_X86_64_32 against symbol `PyBaseObject_Type' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(unicodeobject.o): relocation R_X86_64_32 against `.rodata.str1.1' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(unicodectype.o): relocation R_X86_64_32S against `.rodata' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(weakrefobject.o): relocation R_X86_64_32 against `.rodata.str1.1' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(_warnings.o): relocation R_X86_64_32 against `.data' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(ceval.o): relocation R_X86_64_32 against `.rodata.str1.8' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(codecs.o): relocation R_X86_64_32 against `.rodata.str1.1' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(compile.o): relocation R_X86_64_32S against `.rodata' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(errors.o): relocation R_X86_64_32 against `.data' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(future.o): relocation R_X86_64_32 against `.rodata.str1.1' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(getargs.o): relocation R_X86_64_32 against `.rodata.str1.1' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(getversion.o): relocation R_X86_64_32 against `.rodata.str1.1' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(import.o): relocation R_X86_64_32S against symbol `PyDict_Type' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(importdl.o): relocation R_X86_64_32 against `.rodata.str1.1' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(initconfig.o): relocation R_X86_64_32 against `.rodata.str1.1' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(marshal.o): relocation R_X86_64_32 against `.rodata.str1.1' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(modsupport.o): relocation R_X86_64_32S against `.rodata' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(mystrtoul.o): relocation R_X86_64_32S against symbol `_Py_ctype_table' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(pathconfig.o): relocation R_X86_64_32S against `.rodata.str1.1' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(peephole.o): relocation R_X86_64_32S against `.rodata' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(preconfig.o): relocation R_X86_64_32S against `.rodata' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(pyhash.o): relocation R_X86_64_32S against `.rodata' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(pylifecycle.o): relocation R_X86_64_32 against `.rodata.str1.1' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(pymath.o): relocation R_X86_64_32S against `.rodata' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(pystate.o): relocation R_X86_64_32 against symbol `_Py_NoneStruct' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(pythonrun.o): relocation R_X86_64_32 against `.rodata.str1.1' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(pytime.o): relocation R_X86_64_32 against `.rodata.str1.8' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(bootstrap_hash.o): relocation R_X86_64_32 against `.rodata.str1.1' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(symtable.o): relocation R_X86_64_32 against `.rodata.str1.8' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(sysmodule.o): relocation R_X86_64_32 against `.bss' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(thread.o): relocation R_X86_64_32 against `.bss' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(traceback.o): relocation R_X86_64_32 against symbol `_Py_NoneStruct' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(getopt.o): relocation R_X86_64_32S against `.rodata.str4.4' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(pystrtod.o): relocation R_X86_64_32S against symbol `_Py_ctype_tolower' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(pystrhex.o): relocation R_X86_64_32 against `.rodata.str1.1' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(dtoa.o): relocation R_X86_64_32S against `.bss' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(formatter_unicode.o): relocation R_X86_64_32S against symbol `_Py_ctype_table' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(fileutils.o): relocation R_X86_64_32S against `.rodata.str1.1' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(dynload_shlib.o): relocation R_X86_64_32 against `.rodata.str1.1' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(getpath.o): relocation R_X86_64_32 against `.rodata' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(gcmodule.o): relocation R_X86_64_32 against symbol `_Py_FalseStruct' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(posixmodule.o): relocation R_X86_64_32S against symbol `_Py_NoneStruct' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(errnomodule.o): relocation R_X86_64_32 against `.rodata.str1.1' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(pwdmodule.o): relocation R_X86_64_32 against `.data' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(_sre.o): relocation R_X86_64_32S against `.rodata' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(_codecsmodule.o): relocation R_X86_64_32 against symbol `_Py_NoneStruct' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(_weakref.o): relocation R_X86_64_32 against symbol `_PyWeakref_RefType' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(_functoolsmodule.o): relocation R_X86_64_32 against `.data' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(_operator.o): relocation R_X86_64_32S against `.data' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(_collectionsmodule.o): relocation R_X86_64_32 against symbol `_Py_NoneStruct' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(_abc.o): relocation R_X86_64_32 against `.data' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(itertoolsmodule.o): relocation R_X86_64_32S against `.data' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(atexitmodule.o): relocation R_X86_64_32 against `.text' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(signalmodule.o): relocation R_X86_64_32S against `.bss' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(_stat.o): relocation R_X86_64_32 against `.rodata.str1.1' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(timemodule.o): relocation R_X86_64_32 against `.rodata.str1.1' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(_threadmodule.o): relocation R_X86_64_32 against `.rodata.str1.1' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(_localemodule.o): relocation R_X86_64_32 against `.rodata.str1.1' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(_iomodule.o): relocation R_X86_64_32 against `.data' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(iobase.o): relocation R_X86_64_32 against symbol `_Py_FalseStruct' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(fileio.o): relocation R_X86_64_32 against `.rodata.str1.1' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(bytesio.o): relocation R_X86_64_32 against `.rodata.str1.8' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(bufferedio.o): relocation R_X86_64_32 against `.data' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(textio.o): relocation R_X86_64_32 against symbol `_Py_NoneStruct' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(stringio.o): relocation R_X86_64_32 against `.rodata.str1.1' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(faulthandler.o): relocation R_X86_64_32 against `.rodata.str1.1' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(_tracemalloc.o): relocation R_X86_64_32 against `.text' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(_peg_parser.o): relocation R_X86_64_32 against `.data' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(symtablemodule.o): relocation R_X86_64_32 against `.rodata.str1.1' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(xxsubtype.o): relocation R_X86_64_32 against `.data' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(getbuildinfo.o): relocation R_X86_64_32 against `.rodata.str1.1' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(acceler.o): relocation R_X86_64_32 against `.rodata.str1.1' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(grammar1.o): relocation R_X86_64_32S against symbol `_PyParser_TokenNames' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(token.o): relocation R_X86_64_32S against `.rodata' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(peg_api.o): relocation R_X86_64_32 against `.rodata.str1.1' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(parsetok.o): relocation R_X86_64_32 against `.rodata.str1.1' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(tokenizer.o): relocation R_X86_64_32 against `.rodata.str1.1' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(accu.o): relocation R_X86_64_32 against `.rodata.str1.1' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(bytes_methods.o): relocation R_X86_64_32S against symbol `_Py_NoneStruct' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(cellobject.o): relocation R_X86_64_32 against `.rodata.str1.1' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(classobject.o): relocation R_X86_64_32 against `.rodata.str1.1' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(codeobject.o): relocation R_X86_64_32S against symbol `PyTuple_Type' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(complexobject.o): relocation R_X86_64_32 against `.rodata.str1.8' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(descrobject.o): relocation R_X86_64_32 against `.rodata.str1.8' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(enumobject.o): relocation R_X86_64_32 against `.data' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(genobject.o): relocation R_X86_64_32 against symbol `_Py_NoneStruct' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(funcobject.o): relocation R_X86_64_32S against symbol `_Py_NoneStruct' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(interpreteridobject.o): relocation R_X86_64_32 against `.data' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(odictobject.o): relocation R_X86_64_32 against `.rodata.str1.8' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(namespaceobject.o): relocation R_X86_64_32S against `.rodata.str1.1' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(Python-ast.o): relocation R_X86_64_32 against `.rodata.str1.1' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(ast.o): relocation R_X86_64_32S against `.rodata' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(ast_opt.o): relocation R_X86_64_32S against symbol `PyFrozenSet_Type' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(ast_unparse.o): relocation R_X86_64_32S against symbol `PyFloat_Type' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(bltinmodule.o): relocation R_X86_64_32S against symbol `PyFilter_Type' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(context.o): relocation R_X86_64_32 against `.rodata.str1.8' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(getcompiler.o): relocation R_X86_64_32 against `.rodata.str1.8' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(getcopyright.o): relocation R_X86_64_32 against `.rodata' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(getplatform.o): relocation R_X86_64_32 against `.rodata.str1.1' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(hamt.o): relocation R_X86_64_32S against symbol `_PyHamt_BitmapNode_Type' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(hashtable.o): relocation R_X86_64_32S against hidden symbol `_Py_hashtable_get_entry_generic' can not be used when making a shared object
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(structmember.o): relocation R_X86_64_32S against `.rodata' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(parser.o): relocation R_X86_64_32 against `.rodata.str1.8' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(pegen.o): relocation R_X86_64_32 against symbol `_Py_NoneStruct' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(parse.o): relocation R_X86_64_32 against `.text' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(parse_string.o): relocation R_X86_64_32 against `.rodata.str1.1' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: /usr/local/lib/libpython3.9.a(myreadline.o): relocation R_X86_64_32 against `.rodata.str1.1' can not be used when making a shared object; recompile with -fPIC
[pipenv.exceptions.InstallError]:       /usr/bin/ld: final link failed: Nonrepresentable section on output
[pipenv.exceptions.InstallError]:       collect2: error: ld returned 1 exit status
[pipenv.exceptions.InstallError]:       error: command '/usr/bin/gcc' failed with exit code 1
[pipenv.exceptions.InstallError]:       [end of output]
[pipenv.exceptions.InstallError]:
[pipenv.exceptions.InstallError]:   note: This error originates from a subprocess, and is likely not a problem with pip.
[pipenv.exceptions.InstallError]:   ERROR: Failed building wheel for mod-wsgi
[pipenv.exceptions.InstallError]: ERROR: Could not build wheels for mod-wsgi, which is required to install pyproject.toml-based projects

直接放上成功在 CentOS 7 部署 Django 的 Apache 設定:

httpd.conf 設定:

由於 WSGI 是透過 python3.6 安裝的,所以記得要找到相對應的 moduel 位置將其加入到 httpd.conf 中。 

LoadModule wsgi_module "/usr/lib64/httpd/modules/mod_wsgi-py36.cpython-36m-x86_64-linux-gnu.so"

vhosts 裡的 hostname.conf / hostname.ssl.conf 設定:

由於同一個 Apache Server 通常可以部署多個網站服務,所以我們在 /usr/local/apache/conf.d/vhosts 資料夾下面新增兩個檔案,鑑於安全性,我們只打算開放 https,因此在 hostname.conf 我們只填寫重新導向的設定如下:

<VirtualHost xxx.xxx.xxx.xxx:80>
	ServerName hostname
	Redirect / https://hostname/
</VirtualHost>

主要的設定都落在 hostname.ssl.conf 裡面:

<VirtualHost xxx.xxx.xxx.xxx:443>

    ServerName hostname
    ServerAlias www.hostname
    ServerAdmin xxx@gmail.com
    DocumentRoot /home/username/public_html/django-project
    UseCanonicalName Off

    CustomLog /usr/local/apache/domlogs/hostname.bytes bytes
    CustomLog /usr/local/apache/domlogs/hostname.log combined
    ErrorLog /usr/local/apache/domlogs/hostname.error.log

    WSGIDaemonProcess django-project python-path=/root/.local/share/virtualenvs/python36-PwlmGYJW/lib/python3.6/site-packages
    WSGIScriptAlias / /home/username/public_html/django-project/django-project/wsgi.py

    IncludeOptional "/usr/local/apache/conf/userdata/username/hostname/*.conf"

    SSLEngine on
    SSLCertificateFile /etc/pki/tls/certs/hostname.cert
    SSLCertificateKeyFile /etc/pki/tls/private/hostname.key
    SSLCertificateChainFile /etc/pki/tls/certs/hostname.bundle
    SetEnvIf User-Agent ".*MSIE.*" nokeepalive ssl-unclean-shutdown

    Alias /static /home/username/public_html/django-project/django-project/static/
    <Location "/static">
            SetHandler None
    </Location>

    WSGIProcessGroup %{GLOBAL}
    WSGIApplicationGroup %{GLOBAL}
    WSGIPassAuthorization On

    <Location "/secret">
        AuthType Basic
        AuthName "Top Secret"
        Require valid-user
        AuthBasicProvider wsgi
        WSGIAuthUserScript /home/username/public_html/django-project/django-project/wsgi.py
    </Location>

    <Directory "/home/username/public_html/django-project/django-project">
	<Files wsgi.py>
            Require all granted
        </Files>
    </Directory>

</VirtualHost>
備註(以上展示的範例有幾個假設前提):
  1. 使用者的 django project 是利用以下的指令創建在 /home/username/public_html 的路徑上面,如果利用 django-admin 創建的專案 wsgi.py 會被自動創建在 django-project/django-project 的路徑上。
    django-admin startproject django-project
  2. django 會用到的相關模組是利用 pipenv 安裝在 
  3. 有關 SSL 的檔案放在 /etc/pki/tls/certs/ 之下
WSGIPassAuthorization On 記得開啟 !!

檸檬爸一開始這個 property 沒有設定到,結果就是一直沒有辦法通過 Django 原生的認證機制認證,卡超久。