Tutorials for deploying Nginx and fastcgi and flask frameworks on Mac OS

Source: Internet
Author: User
Tags nginx server


This article focuses on the deployment of Nginx and fastcgi on Mac OS and the Flask framework, flask is a minimalist web open framework under Python, and friends can refer to



Recently in learning flask, this article describes how to deploy flask development of the application, but also learn about the use of Nginx, this is only an experiment on the Mac.



Application



The application used here is the flaskr given in the official documentation.



Install Nginx



Install Nginx using homebrew:




$ Brew Install Nginx


Homebrew automatically installs the Nginx and its dependent programs. The Nginx 1.6.2 is installed on my computer, and the path to the configuration file is/usr/local/etc/nginx/nginx.conf.



To start the Nginx command:




$ nginx


The default port for Nginx is 8080, open localhost:8080 in the browser, and display the page description shown below Nginx has already worked.



Configure Nginx



To modify a Nginx profile:




server {listen server_name localhost; charset utf-8; Location/{try_files $uri @flaskr;} location @flaskr {include fastcgi_params; Fastcgi_param path_info $fastcgi _script_ Name Fastcgi_param Script_name ""; Fastcgi_pass Unix:/tmp/flaskr-fcgi.sock; } }


Reboot Nginx:




$ nginx-s quit $ sudo nginx


Because 80 ports are used, you need to add sudo when starting the nginx.



After startup completes, access localhost:



There was an error during the visit because our application has not yet started.



FastCGI Server



Nginx is a static Web server that cannot run our Python application directly, and when Nginx receives the request, it is forwarded to our application via fastcgi, which runs on the fastcgi server, This server receives the NGINX request and invokes our program, returns the result to Nginx,nginx and returns the result to the user.



The fastcgi server that we want to use is Flup, installation method:




$ pip Install Flup


Create a fcgi file in the application directory, such as flaskr.fcgi:




#!/usr/bin/python from flup.server.fcgi import wsgiserver from Flaskr import app if __name__ = = ' __main__ ': Wsgiserver (A PP, bindaddress= '/tmp/flaskr-fcgi.sock '). Run ()


Permissions that can be given to fcgi files at the same time:




$ chmod +x flaskr.fcgi


To start the server manually:




$ screen $./flaskr.fcgi


Use screen to make the server run in the background, or:




$ nohup./flaskr.fcgi &


Visit localhost again to see our application.



Problems encountered



After you run FastCGI server for the first time, you will not be able to access it, after viewing the Nginx log, you find that the Nginx server does not have permission to access the socket file, modify the nginx.conf add User configuration:



code as follows:



User Wzy;



Nginx the error when starting:




Nginx: [Emerg] Getgrnam ("Wzy") failed In/usr/local/etc/nginx/nginx.conf:2


Google then found to add a user group to the line, changed to this:



code as follows:



User Wzy wheel;



After starting Nginx again, everything is fine.



How to use the Nginx configuration item User:




Syntax:user user [group]; Default:user nobody nobody;


If ignoring Group,nginx will use the same user group as the username, for example, I set up user Wzy, then nginx start to look up the group Wzy, my computer does not have this user group, so Nginx will complain.



Note < > : More Wonderful tutorials please focus on Triple programming


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.