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:
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:
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:
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:
To start the server manually:
Use screen to make the server run in the background, or:
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