Deploy flask cms in CentOS

Source: Internet
Author: User
Tags install mongodb virtualenv

Deploy flask cms in CentOS

Recently, I have learned python, but I have never had any programming experience and can't practice it. So I am going to build my own python framework website to play it and consolidate my knowledge ~
Based on the comments from beginners on the internet, I finally selected a web framework suitable for beginners for flask.
So I found this project: https://github.com/quokkaproject/quokka.
Okay, start building.

1. Runtime Environment

My system environment

System: CentOSrelease6.4 (Final) kernel: 2.6.32-504.23.4.el6.x86 _ 64 Memory: 997 MB
I. download and install the new Python version.
[root@linux2tmp]#wgethttps://www.python.org/ftp/python/2.7.10/Python-2.7.10.tgz...[root@linux2tmp]#tarzxfPython-2.7.10.tgz[root@linux2tmp]#cdPython-2.7.10[root@linux2Python-2.7.10]#./configure[root@linux2Python-2.7.10]#make&&makeinstall
Ii install python-pip and virtualenv

Reference: https://pip.pypa.io/en/stable/installing/

[Root @ linux2tmp] # wgethttps: // response
Iii update git

The git version in linux is too low, and the command parameters used later are not supported. Therefore, update the git command here.

[root@linux2pip-7.1.2]#yumcleanall[root@linux2pip-7.1.2]#rpm--importhttp://apt.sw.be/RPM-GPG-KEY.dag.txt[root@linux2pip-7.1.2]#rpm-ihttp://pkgs.repoforge.org/rpmforge-release/rpmforge-release-0.5.3-1.el6.rf.x86_64.rpm[root@linux2pip-7.1.2]#yum--enablerepo=rpmforge-extrasupdategit
2. Install mongodb
[Root @ linux2tmp] # curl-Ohttps: // response

Note: The mongo database operation writes data to create a database, so you do not need to create a database here. As long as a program writes data to the database, it will create a new database.

3. install and configure quokka
[Root @ linux2 ~] # Virtualenv-p/usr/local/bin/python2.7quokka-env # specify the absolute path of the python version [root @ linux2 ~] # Sourcequokka-env/bin/activate # To exit the vir environment, run the deactivate command (without parameters) (quokka-env) [root @ linux2www] # gitclonehttps: // quit, (quokka-env) [root @ linux2www] # cdquokka/quokka/(quokka-env) [root @ linux2quokka] # cpexample. local_settings.pylocal_settings.py (quokka-env) [root @ linux2quokka] # vimlocal_settings.py # configure the mongodb database... MONGODB_DB = "quokka_db" MONGODB_HOST = 'localhost' MONGODB _ PORT = 10101MONGODB_USERNAME = None # Here my database is local, so I can leave the password MONGODB_PASSWORD = None... (quokka-env) [root @ linux2quokka] # cd .. (quokka-env) [root @ linux2quokka] # pipinstall-rrequirements/requirements.txt read the list of installation packages in txt.
Create an administrator and run quokka

ICreate a super Administrator (You need to log on to the Management Interface)

$pythonmanage.pyaccounts_createsuperuseryou@email.comP4$$W0Rd

IiFill in sample data (optional, If you want sample data)

$pythonmanage.pypopulate

Credentials for/admin will be email: admin@example.com passwd: admin

IiiRun the test

$pythonmanage.pyrunserver--host0.0.0.0--port5000
  • Site on http: // localhost: 5000

  • Admin on http: // localhost: 5000/admin

For more installation methods, refer to here: https://github.com/quokkaproject/quokka
So far, quokka has been installed successfully ~ In case of other page abnormal display problems, you can ask the project owner: https://github.com/quokkaproject/quokka/issues
Next, install the http server

4. Configure nginx + Gunicorn

The 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 nginx

To 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.servingImport 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 wwwIt 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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.