4. Configure nginx + GunicornThe flask built-in web (the last command) can be used to run the web. However, after all, it is not a professional web server. We recommend that you use mainstream web servers. nginx is used here.
For other python deployment methods, refer to here: http://lutaf.com/141.htm
I. Download and install and configure nginxTo enable nginx to support the google Performance module, install google-perftools first.
Download link:
Bytes
Http: // https://github.com/gperftools/gperftools/releases#googlegoogle
Install libunwind(quokka-env1_1_root@linux2tmp1_1_tarzxflibunwind-1.1.tar.gz (quokka-env) [root @ linux2tmp] # cdlibunwind-1.1 (quokka-env) [root@linux2libunwind-1.1] # CFLAGS =-fPIC. /configure (quokka-env) [root@linux2libunwind-1.1] # makeCFLAGS =-fPIC (quokka-env) [root@linux2libunwind-1.1] # makeCFLAGS =-fPICinstall install google-perftools (quokka-env) [root @ linux2tmp] # wgethttps: // encrypt (quokka-env) [root@linux2gperftools-2.4] #. /configure (quokka-env) [root@linux2gperftools-2.4] # make & makeinstall
Add a thread directory for google-perftools
[root@localhosthome]#mkdir/tmp/tcmalloc[root@localhosthome]#chmod0777/tmp/tcmalloc
Install nginx
(Quokka-env) [root @ linux2nginx] # yuminstall-ygcc-c ++ gccautomakeautoconflibtoolmake (quokka-env) [root @ linux2tmp] # curl-Ohttp: // nginx.org/download/nginx-1.8.0.tar.gz#download the latest stable version of nginx restart (quokka-env) [root @ linux2tmp] # cdnginx-1.8.0 (quokka-env) [root@linux2nginx-1.8.0] # groupaddwww (quokka-env) [root@linux2nginx-1.8.0] # useradd-s/sbin/nologin-gwwwwww (quokka-env) [root@linux2nginx-1.8.0] #. /configure -- user = www -- group = www -- prefix =/usr/local/nginx -- with-http_stub_status_module -- with-http_ssl_module -- with-google_perftools_module -- with-http_gzip_static_module -- with-http_sub_module (quokka-env) [root@linux2nginx-1.8.0] # make & makeinstall
Nginx compilation module description:
-- With-http_stub_status_module: provides the ability to view server statistics
-- With-http_ssl_module: Support for HTTPS/SSL
-- With-http_gzip_static_module: provides pre-compressed static files
-- With-google_perftools_module: Support for Google Performance Tools
-- With-http_sub_module: can replace text in a page
More please refer to this blog: http://www.zicheng.net/article/42.htm
Configure nginx
(Quokka-env) [root @ linux2conf] # vimng.pdf. confworker_processes1; google_perftools_profiles/tmp/tcmalloc; # Add the thread directory events {worker_connections1024;} http {includemime. types; default_typeapplication/octet-stream; sendfileon; keepalive_timeout65; upstreamquokka {serverunix:/tmp/quokka. sockfail_timeout = 0;} server {listen80default_server; return444; # Return httpcode444} server {# listen80; server_name * When the request does not match server_name *. eason. wang; return301https:/eason. wang $ request_uri;} server {listen80; client_max_body_size4G; server_nameeason.wang; # servername is your domain name keepalive_timeout5; root/data/www/quokka; # Do not understand why you need to configure this path location/{try_files $ uri @ proxy_to_app;} location @ proxy_to_app {proxy_set_headerX-Forwarded-For $ response; proxy_set_headerHost $ http_host; proxy_redirectoff; proxy_passhttp: // quokka;} error_page500502503504/50x.html; location =/50x.html {roothtml ;}}}
Install and configure gunicorn
A python wsgi http Server. I don't know how to say it. For details, refer to the official website.
(Quokka-env) [root @ linux2logs] # pipinstallgunicorn (quokka-env) [root @ linux2quokka] # cd/data/www/quokka/(quokka-env) [root @ linux2quokka] # vimstart_gunicorn.sh # Start script #! /Bin/bashNAME = 'quokka '# Name of the application FLASKDIR =/data/www/quokka # SOCKFILE =/tmp/quokka in the django project directory. sock # use this sock to communicate with USER = www # USER NUM_WORKERS running this application = 3 # Number of worker processes used by gunicorn DJANGO_WSGI_MODULE = wsgi # wsgi module echo "starting $ NAMEas 'whoam' "# activate python virtual runtime environment cd $ FLASKDIRsource/root/quokka-env/bin/activate # Start flaskexec/root/quokka-env/bin/gunicorn \ -- workers $ NUM_WORKERS \- uwww \ -- bind = unix: $ SOCKFILE \ wsgi
Run
(quokka-env)[root@linux2quokka]#./start_gunicorn.sh&
Problems encountered
1. When pip is installed, it is successful after the python get-pip.py is executed, but there is no pip command.
I have repeatedly confirmed that there is no error in executing this installation command. I think it may be a problem with the Python version, because the default value of linux is python 2.6.6, I want to use virtualenv, so I am too lazy to make environment variables, so I use the absolute path method to specify the new version of python to reinstall the solution.
[root@linux2pip-7.1.2]#/usr/local/bin/python2.7get-pip.py--force-reinstall
2. When mongodb wants to delete a database, the DELETE command is
[Root @ linux2shell] #/usr/local/mongodb/bin/mongo127.0.0.1: 10101> usexxx # select the database to be deleted> db. dropDatabase () # Delete the database
3. When "cocould not find a version that satisfies the requirement..." is reported during pip installation, use the-v parameter to view the detailed cause and solve the problem. Example:
[root@linux2www]#pipinstallpyshorteners==0.5.5-vCollectingpyshorteners==0.5.5Gettingpagehttps://pypi.python.org/simple/pyshorteners/StartingnewHTTPSconnection(1):pypi.python.org"GET/simple/pyshorteners/HTTP/1.1"40428063CouldnotfetchURLhttps://pypi.python.org/simple/pyshorteners/:404ClientError:NotFound-skipping1location(s)tosearchforversionsofpyshorteners:*https://pypi.python.org/simple/pyshorteners/Gettingpagehttps://pypi.python.org/simple/pyshorteners/"GET/simple/pyshorteners/HTTP/1.1"40428063CouldnotfetchURLhttps://pypi.python.org/simple/pyshorteners/:404ClientError:NotFound-skippingCouldnotfindaversionthatsatisfiestherequirementpyshorteners==0.5.5(fromversions:)Cleaningup...Nomatchingdistributionfoundforpyshorteners==0.5.5Exceptioninformation:Traceback(mostrecentcalllast):File"/root/quokka-env/lib/python2.7/site-packages/pip/basecommand.py",line211,inmainstatus=self.run(options,args)File"/root/quokka-env/lib/python2.7/site-packages/pip/commands/install.py",line305,inrunwb.build(autobuilding=True)File"/root/quokka-env/lib/python2.7/site-packages/pip/wheel.py",line705,inbuildself.requirement_set.prepare_files(self.finder)File"/root/quokka-env/lib/python2.7/site-packages/pip/req/req_set.py",line334,inprepare_filesfunctools.partial(self._prepare_file,finder))File"/root/quokka-env/lib/python2.7/site-packages/pip/req/req_set.py",line321,in_walk_req_to_installmore_reqs=handler(req_to_install)File"/root/quokka-env/lib/python2.7/site-packages/pip/req/req_set.py",line461,in_prepare_filereq_to_install.populate_link(finder,self.upgrade)File"/root/quokka-env/lib/python2.7/site-packages/pip/req/req_install.py",line250,inpopulate_linkself.link=finder.find_requirement(self,upgrade)File"/root/quokka-env/lib/python2.7/site-packages/pip/index.py",line571,infind_requirement'Nomatchingdistributionfoundfor%s'%reqDistributionNotFound:Nomatchingdistributionfoundforpyshorteners==0.5.5
We can see from the above that the connection is 404, so we can verify whether there is a problem with 404 or pip. You can directly access the web or use curl for verification.
(quokka-env)[root@linux2www]#curl-Ihttps://pypi.python.org/simple/pyshorteners/HTTP/1.1404NotFound
Page 404 is a problem of the project owner adjusting the page or canceling it.
If you want to solve the problem as soon as possible, search for the project on github. Generally, how can I install it locally in README documents.
4. An error is reported when nginx is started.
Nginx: error while loading shared libraries: libprofiler. so.0: cannot open shared object file: No such file or directory
Nginx: error while loading shared libraries: libunwind. so.8: cannot open shared object file: No such file or directory
The preceding two errors are caused by the 64-bit operating system. The solution is to find two lib files and connect them to/usr/lib64.
(Quokka-env) [root @ linux2 ~] # Whereislibprofiler. sow.libunwind. so.8 # locate the two lib file locations libprofiler. so:/usr/lib64/libprofiler. so.0/usr/local/lib/libprofiler. so.0/usr/local/lib/libprofiler. solibunwind. so:/usr/lib64/libunwind. so.8/usr/local/lib/libunwind. so.8/usr/local/lib/libunwind. so (quokka-env) [root @ linux2 ~] # Ln-s/usr/local/lib/libprofiler. so.0/usr/lib64/(quokka-env) [root @ linux2 ~] #/Usr/local/nginx/sbin/nginx-V/usr/local/nginx/sbin/nginx: errorwhileloadingsharedlibraries: libunwind. so.8: cannotopen#dobjectfile: Nosuchfileordirectory (quokka-env) [root @ linux2 ~] # Ln-s/usr/local/lib/libunwind. so.8/usr/lib64/
5. solve the problem that the specified gunicorn-u has no permission
(quokka-env)[root@linux2quokka]#gunicorn-uwww--workers3--bindunix:/tmp/quokka.sock-m007wsgi[2015-09-2410:53:57+0000][28230][INFO]Startinggunicorn19.3.0[2015-09-2410:53:57+0000][28230][INFO]Listeningat:unix:/tmp/quokka.sock(28230)[2015-09-2410:53:57+0000][28230][INFO]Usingworker:sync[2015-09-2410:53:57+0000][28235][INFO]Bootingworkerwithpid:28235[2015-09-2410:53:57+0000][28235][ERROR]Exceptioninworkerprocess:Traceback(mostrecentcalllast):File"/root/quokka-env/lib/python2.7/site-packages/gunicorn/arbiter.py",line507,inspawn_workerFile"/root/quokka-env/lib/python2.7/site-packages/gunicorn/workers/base.py",line118,ininit_processFile"/root/quokka-env/lib/python2.7/site-packages/gunicorn/app/base.py",line67,inwsgiFile"/root/quokka-env/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py",line65,inloadFile"/root/quokka-env/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py",line52,inload_wsgiappFile"/root/quokka-env/lib/python2.7/site-packages/gunicorn/util.py",line355,inimport_appFile"/data/www/quokka/wsgi.py",line4,in<module>fromwerkzeug.servingimportrun_simpleImportError:Nomodulenamedwerkzeug.servingTraceback(mostrecentcalllast):File"/root/quokka-env/lib/python2.7/site-packages/gunicorn/arbiter.py",line507,inspawn_workerFile"/root/quokka-env/lib/python2.7/site-packages/gunicorn/workers/base.py",line118,ininit_processFile"/root/quokka-env/lib/python2.7/site-packages/gunicorn/app/base.py",line67,inwsgiFile"/root/quokka-env/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py",line65,inloadFile"/root/quokka-env/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py",line52,inload_wsgiappFile"/root/quokka-env/lib/python2.7/site-packages/gunicorn/util.py",line355,inimport_appFile"/data/www/quokka/wsgi.py",line4,in<module>fromwerkzeug.servingimportrun_simpleImportError:Nomodulenamedwerkzeug.serving[2015-09-2410:53:57+0000][28235][INFO]Workerexiting(pid:28235)Traceback(mostrecentcalllast):File"/root/quokka-env/bin/gunicorn",line11,in<module>sys.exit(run())File"/root/quokka-env/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py",line74,inrunWSGIApplication("%(prog)s[OPTIONS][APP_MODULE]").run()File"/root/quokka-env/lib/python2.7/site-packages/gunicorn/app/base.py",line189,inrunsuper(Application,self).run()File"/root/quokka-env/lib/python2.7/site-packages/gunicorn/app/base.py",line72,inrunArbiter(self).run()File"/root/quokka-env/lib/python2.7/site-packages/gunicorn/arbiter.py",line174,inrunself.manage_workers()File"/root/quokka-env/lib/python2.7/site-packages/gunicorn/arbiter.py",line477,inmanage_workersself.spawn_workers()File"/root/quokka-env/lib/python2.7/site-packages/gunicorn/arbiter.py",line541,inspawn_workerstime.sleep(0.1*random.random())File"/root/quokka-env/lib/python2.7/site-packages/gunicorn/arbiter.py",line214,inhandle_chldself.reap_workers()File"/root/quokka-env/lib/python2.7/site-packages/gunicorn/arbiter.py",line459,inreap_workersraiseHaltServer(reason,self.WORKER_BOOT_ERROR)gunicorn.errors.HaltServer:<HaltServer'Workerfailedtoboot.'3>
Based on the executed command and stack trace, basically two error messages are identified:
ImportError: No module named werkzeug.serving
Import module error. check whether it is a module problem.
Large AreaFile "/root ..."
In the env folder, check whether the www account has permissions under the/root account.
Check the two errors below:
(quokka-env)[root@linux2tmp]#pythonPython2.7.10(default,Sep212015,17:31:07)[GCC4.4.720120313(RedHat4.4.7-4)]onlinux2Type"help","copyright","credits"or"license"formoreinformation.>>>fromwerkzeug.servingimportrun_simple
No error reported. It seems that the module is correct.
(quokka-env)[root@linux2tmp]#gunicorn--workers3--bindunix:/tmp/quokka.sock-m007wsgi[2015-09-2411:36:19+0000][28350][INFO]Startinggunicorn19.3.0[2015-09-2411:36:19+0000][28350][INFO]Listeningat:unix:/tmp/quokka.sock(28350)[2015-09-2411:36:19+0000][28350][INFO]Usingworker:sync[2015-09-2411:36:19+0000][28355][INFO]Bootingworkerwithpid:28355[2015-09-2411:36:19+0000][28356][INFO]Bootingworkerwithpid:28356[2015-09-2411:36:19+0000][28357][INFO]Bootingworkerwithpid:28357
Remove-u www
It can be started normally, so it seems that this is a problem caused by permissions ~
Solution:
[Root @ linux2/] # ll... drwxr -----. 14rootroot4096 August 19: 50root... [root @ linux2/] # chmodg + xroot
The reason for adding the group execution permission is that the permission for executing the gunicorn command is the current user and the current user group (which can be changed through-g ), but we changed the starting user to www, so it will try to use www user permission. www does not have this permission, and it will attempt the current user group permission (both root group ), therefore, you must grant the root group the execution permission.