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.


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

Install Nginx

Install Nginx using homebrew:


1 $ 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:


1 $ 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:


1 2 3 4 5 6 7 8 9 10 11 12-13 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:


1 2 $ 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:


1 $ pip Install Flup

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


1 2 3 4 5 6 #!/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:


1 $ chmod +x flaskr.fcgi

To start the server manually:


1 2 $ screen $./flaskr.fcgi

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


1 $ 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:

Copy code code as follows:

User Wzy;

Nginx the error when starting:


1 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:

Copy code code as follows:

User Wzy wheel;

After starting Nginx again, everything is fine.

How to use the Nginx configuration item User:


1 2 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

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.