DEV Community

Discussion on: Flask Deploy with Apache on CentOS - Minimal Setup

Collapse
 
williamium3000 profile image
william

I have followed your steps from the above tutorial and one of the problem is that i cannot specifying my python interpreter to be the vitual one, and apache just uses the default python2.7.5 initially installed in centos.how can i solve this
[Thu Apr 09 13:42:29.349530 2020] [lbmethod_heartbeat:notice] [pid 16236] AH02282: No slotmem from mod_heartmonitor
[Thu Apr 09 13:42:29.353249 2020] [mpm_prefork:notice] [pid 16236] AH00163: Apache/2.4.6 (CentOS) mod_wsgi/3.4 Python/2.7.5 configured -- resuming normal operations
[Thu Apr 09 13:42:29.353276 2020] [core:notice] [pid 16236] AH00094: Command line: '/usr/sbin/httpd -D FOREGROUND'
[Thu Apr 09 13:42:32.225160 2020] [:error] [pid 16242] [client 120.230.96.144:49562] mod_wsgi (pid=16242): Target WSGI script '/var/www/hitme/wsgi.py' cannot be loaded as Python module.
[Thu Apr 09 13:42:32.225201 2020] [:error] [pid 16242] [client 120.230.96.144:49562] mod_wsgi (pid=16242): Exception occurred processing WSGI script '/var/www/hitme/wsgi.py'.
[Thu Apr 09 13:42:32.225224 2020] [:error] [pid 16242] [client 120.230.96.144:49562] Traceback (most recent call last):
[Thu Apr 09 13:42:32.225243 2020] [:error] [pid 16242] [client 120.230.96.144:49562] File "/var/www/hitme/wsgi.py", line 13, in
[Thu Apr 09 13:42:32.225307 2020] [:error] [pid 16242] [client 120.230.96.144:49562] from app import app as application
[Thu Apr 09 13:42:32.225318 2020] [:error] [pid 16242] [client 120.230.96.144:49562] File "/var/www/hitme/app/init.py", line 2, in
[Thu Apr 09 13:42:32.225455 2020] [:error] [pid 16242] [client 120.230.96.144:49562] from flask import Flask
[Thu Apr 09 13:42:32.225469 2020] [:error] [pid 16242] [client 120.230.96.144:49562] File "/var/www/hitme/hitme/lib/python3.7/site-packages/flask/init.py", line 14, in
[Thu Apr 09 13:42:32.225511 2020] [:error] [pid 16242] [client 120.230.96.144:49562] from jinja2 import escape
[Thu Apr 09 13:42:32.225519 2020] [:error] [pid 16242] [client 120.230.96.144:49562] File "/var/www/hitme/hitme/lib/python3.7/site-packages/jinja2/init.py", line 9, in
[Thu Apr 09 13:42:32.225561 2020] [:error] [pid 16242] [client 120.230.96.144:49562] from .bccache import BytecodeCache
[Thu Apr 09 13:42:32.225570 2020] [:error] [pid 16242] [client 120.230.96.144:49562] File "/var/www/hitme/hitme/lib/python3.7/site-packages/jinja2/bccache.py", line 19, in
[Thu Apr 09 13:42:32.225649 2020] [:error] [pid 16242] [client 120.230.96.144:49562] from .compat import BytesIO
[Thu Apr 09 13:42:32.225658 2020] [:error] [pid 16242] [client 120.230.96.144:49562] File "/var/www/hitme/hitme/lib/python3.7/site-packages/jinja2/_compat.py", line 120, in
[Thu Apr 09 13:42:32.225701 2020] [:error] [pid 16242] [client 120.230.96.144:49562] from pathlib import PurePath
[Thu Apr 09 13:42:32.225709 2020] [:error] [pid 16242] [client 120.230.96.144:49562] File "/usr/local/Python-3.7.7/Lib/pathlib.py", line 9, in
[Thu Apr 09 13:42:32.226042 2020] [:error] [pid 16242] [client 120.230.96.144:49562] from _collections_abc import Sequence
[Thu Apr 09 13:42:32.226086 2020] [:error] [pid 16242] [client 120.230.96.144:49562] File "/usr/local/Python-3.7.7/Lib/_collections_abc.py", line 58
[Thu Apr 09 13:42:32.226092 2020] [:error] [pid 16242] [client 120.230.96.144:49562] async def _coro(): pass
[Thu Apr 09 13:42:32.226095 2020] [:error] [pid 16242] [client 120.230.96.144:49562] ^
[Thu Apr 09 13:42:32.226098 2020] [:error] [pid 16242] [client 120.230.96.144:49562] SyntaxError: invalid syntax
[Thu Apr 09 13:42:35.936716 2020] [:error] [pid 16243] [client 120.230.96.144:49564] mod_wsgi (pid=16243): Target WSGI script '/var/www/hitme/wsgi.py' cannot be loaded as Python module.
[Thu Apr 09 13:42:35.936795 2020] [:error] [pid 16243] [client 120.230.96.144:49564] mod_wsgi (pid=16243): Exception occurred processing WSGI script '/var/www/hitme/wsgi.py'.
[Thu Apr 09 13:42:35.936830 2020] [:error] [pid 16243] [client 120.230.96.144:49564] Traceback (most recent call last):
[Thu Apr 09 13:42:35.936861 2020] [:error] [pid 16243] [client 120.230.96.144:49564] File "/var/www/hitme/wsgi.py", line 13, in
[Thu Apr 09 13:42:35.936955 2020] [:error] [pid 16243] [client 120.230.96.144:49564] from app import app as application
[Thu Apr 09 13:42:35.936972 2020] [:error] [pid 16243] [client 120.230.96.144:49564] File "/var/www/hitme/app/
init.py", line 2, in
[Thu Apr 09 13:42:35.937159 2020] [:error] [pid 16243] [client 120.230.96.144:49564] from flask import Flask
[Thu Apr 09 13:42:35.937179 2020] [:error] [pid 16243] [client 120.230.96.144:49564] File "/var/www/hitme/hitme/lib/python3.7/site-packages/flask/
init.py", line 14, in
[Thu Apr 09 13:42:35.937246 2020] [:error] [pid 16243] [client 120.230.96.144:49564] from jinja2 import escape
[Thu Apr 09 13:42:35.937261 2020] [:error] [pid 16243] [client 120.230.96.144:49564] File "/var/www/hitme/hitme/lib/python3.7/site-packages/jinja2/
init_.py", line 9, in
[Thu Apr 09 13:42:35.937313 2020] [:error] [pid 16243] [client 120.230.96.144:49564] from .bccache import BytecodeCache
[Thu Apr 09 13:42:35.937361 2020] [:error] [pid 16243] [client 120.230.96.144:49564] File "/var/www/hitme/hitme/lib/python3.7/site-packages/jinja2/bccache.py", line 19, in
[Thu Apr 09 13:42:35.937490 2020] [:error] [pid 16243] [client 120.230.96.144:49564] from ._compat import BytesIO
[Thu Apr 09 13:42:35.937505 2020] [:error] [pid 16243] [client 120.230.96.144:49564] File "/var/www/hitme/hitme/lib/python3.7/site-packages/jinja2/_compat.py", line 120, in
[Thu Apr 09 13:42:35.937589 2020] [:error] [pid 16243] [client 120.230.96.144:49564] from pathlib import PurePath
[Thu Apr 09 13:42:35.937606 2020] [:error] [pid 16243] [client 120.230.96.144:49564] File "/usr/local/Python-3.7.7/Lib/pathlib.py", line 9, in
[Thu Apr 09 13:42:35.938027 2020] [:error] [pid 16243] [client 120.230.96.144:49564] from _collections_abc import Sequence
[Thu Apr 09 13:42:35.938093 2020] [:error] [pid 16243] [client 120.230.96.144:49564] File "/usr/local/Python-3.7.7/Lib/_collections_abc.py", line 58
[Thu Apr 09 13:42:35.938104 2020] [:error] [pid 16243] [client 120.230.96.144:49564] async def _coro(): pass
[Thu Apr 09 13:42:35.938109 2020] [:error] [pid 16243] [client 120.230.96.144:49564] ^
[Thu Apr 09 13:42:35.938114 2020] [:error] [pid 16243] [client 120.230.96.144:49564] SyntaxError: invalid syntax
this is the error caused by python2.x in my app

Collapse
 
sm0ke profile image
Sm0ke • Edited

Oh .. , sounds like super fun. I see two options here:

  • Recompile the mod_wsgi module using Python3
  • Update the deployment schema to use Apache as a reverse proxy and start the app using Gunicorn

Let me know if this helps.

Collapse
 
williamium3000 profile image
william • Edited

I have gone with the first approach and encounters problems and would like to communicate with you on this a bit more.
so i uninstall mod_wsgi on python2, and tried install using zip pack from mod_wsgi website
./configure --with-python=/usr/local/bin/python3 this is the configure that i used to specify python interpreter
then make && make install
problem occurs(part of the error reports looks like the following)

/usr/bin/ld: /usr/local/python3/lib/libpython3.7m.a(Python-ast.o): relocation R_X86_64_32S against `.rodata' can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: /usr/local/python3/lib/libpython3.7m.a(ast.o): relocation R_X86_64_32 against `.rodata.str1.1' can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: /usr/local/python3/lib/libpython3.7m.a(ast_opt.o): relocation R_X86_64_32S against `.rodata' can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: /usr/local/python3/lib/libpython3.7m.a(ast_unparse.o): relocation R_X86_64_32 against `.rodata.str1.1' can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: /usr/local/python3/lib/libpython3.7m.a(getcompiler.o): relocation R_X86_64_32 against `.rodata.str1.8' can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: /usr/local/python3/lib/libpython3.7m.a(getcopyright.o): relocation R_X86_64_32 against `.rodata' can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: /usr/local/python3/lib/libpython3.7m.a(getplatform.o): relocation R_X86_64_32 against `.rodata.str1.1' can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: /usr/local/python3/lib/libpython3.7m.a(structmember.o): relocation R_X86_64_32S against `.rodata' can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: /usr/local/python3/lib/libpython3.7m.a(getopt.o): relocation R_X86_64_32S against `.rodata.str4.4' can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: /usr/local/python3/lib/libpython3.7m.a(parser.o): relocation R_X86_64_32 against `.rodata.str1.8' can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: /usr/local/python3/lib/libpython3.7m.a(node.o): relocation R_X86_64_PC32 against symbol `PyObject_Free' can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: final link failed: Bad value
collect2: error: ld returned 1 exit status
apxs:Error: Command failed with rc=65536
.
make: *** [src/server/mod_wsgi.la] Error 1

my server is a centos 7 with gcc and python2 installed already. i install python3 using zip pack from its home webside and set --enable-shared to make sure that its library can be shared.
another way i tried is through pip3 install mod_wsgi
problems:

/usr/bin/ld: /usr/local/python3/lib/libpython3.7m.a(Python-ast.o): relocation R_X86_64_32S against `.rodata' can not be used when making a shared object; recompile with -fPIC
    /usr/bin/ld: /usr/local/python3/lib/libpython3.7m.a(ast.o): relocation R_X86_64_32 against `.rodata.str1.1' can not be used when making a shared object; recompile with -fPIC
    /usr/bin/ld: /usr/local/python3/lib/libpython3.7m.a(ast_opt.o): relocation R_X86_64_32S against `.rodata' can not be used when making a shared object; recompile with -fPIC
    /usr/bin/ld: /usr/local/python3/lib/libpython3.7m.a(ast_unparse.o): relocation R_X86_64_32 against `.rodata.str1.1' can not be used when making a shared object; recompile with -fPIC
    /usr/bin/ld: /usr/local/python3/lib/libpython3.7m.a(getcompiler.o): relocation R_X86_64_32 against `.rodata.str1.8' can not be used when making a shared object; recompile with -fPIC
    /usr/bin/ld: /usr/local/python3/lib/libpython3.7m.a(getcopyright.o): relocation R_X86_64_32 against `.rodata' can not be used when making a shared object; recompile with -fPIC
    /usr/bin/ld: /usr/local/python3/lib/libpython3.7m.a(getplatform.o): relocation R_X86_64_32 against `.rodata.str1.1' can not be used when making a shared object; recompile with -fPIC
    /usr/bin/ld: /usr/local/python3/lib/libpython3.7m.a(structmember.o): relocation R_X86_64_32S against `.rodata' can not be used when making a shared object; recompile with -fPIC
    /usr/bin/ld: /usr/local/python3/lib/libpython3.7m.a(getopt.o): relocation R_X86_64_32S against `.rodata.str4.4' can not be used when making a shared object; recompile with -fPIC
    /usr/bin/ld: /usr/local/python3/lib/libpython3.7m.a(parser.o): relocation R_X86_64_32 against `.rodata.str1.8' can not be used when making a shared object; recompile with -fPIC
    /usr/bin/ld: /usr/local/python3/lib/libpython3.7m.a(node.o): relocation R_X86_64_PC32 against symbol `PyObject_Free' can not be used when making a shared object; recompile with -fPIC
    /usr/bin/ld: final link failed: Bad value
    collect2: error: ld returned 1 exit status
    error: command 'gcc' failed with exit status 1
    ----------------------------------------
ERROR: Command errored out with exit status 1: /usr/local/python3/bin/python3.7 -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-313gyz5m/mod-wsgi/setup.py'"'"'; __file__='"'"'/tmp/pip-install-313gyz5m/mod-wsgi/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /tmp/pip-record-a4z7qsiz/install-record.txt --single-version-externally-managed --compile --install-headers /usr/local/python3/include/python3.7m/mod-wsgi Check the logs for full command output.

i have stuck with this for a few days and would really appreciated if you can help
thx sincerely.

Thread Thread
 
sm0ke profile image
Sm0ke • Edited

Hello,
That's a super heavy error. I've googled a little, nothing relevant found. Apache mod_wsgi is super buggy on CentOS. I had many issues in the past and skip over that setup.

I'm using on CentOS a simple setup that uses uWSGI to start the Flask/Django app and Apache as a reverse proxy.
uWSGI is fast but is not handle the SSL layer (that's why you need Apache in front). Feel free to use Gunicorn instead of uWSGI, in case you are familiar with it.

I'm not super free with my time, but we can chat LIVE on Discord. I'm Sm0ke on that server (the moderator).