Tutorial on deploying Nginx, FastCGI, and Flask frameworks on Mac OS, nginxflask
I am studying Flask recently. This article describes how to deploy the Flask application and how to use Nginx. This is just an experiment on Mac.
Application
The application used here is Flaskr provided in the official document.
Install Nginx
Install Nginx using HomeBrew:
$ brew install nginx
HomeBrew automatically installs Nginx and its dependent programs. Nginx 1.6.2 is installed on my computer. The path of the configuration file is/usr/local/etc/nginx. conf.
Run the following command to start Nginx:
$ nginx
The default port of Nginx is 8080. Open localhost: 8080 in a browser. The following page shows that Nginx is working.
Configure Nginx
Modify the Nginx configuration file:
server { listen 80; 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; }}
Restart Nginx:
$ nginx -s quit$ sudo nginx
Because port 80 is used, you must add sudo to start Nginx.
After the startup is complete, access localhost:
An error occurred while accessing because our application has not been started.
FastCGI Server
Nginx is a static WEB Server and cannot directly run our Python application. When Nginx receives the request, it will forward it to our application through FastCGI. The application runs on the FastCGI Server, this server receives Nginx requests and calls our program to return the results to Nginx. Nginx then returns the results to the user.
The FastCGI Server we want to use is flup. Installation Method:
$ pip install flup
Create a fcgi file in the application directory, for example, flaskr. fcgi:
#!/usr/bin/pythonfrom flup.server.fcgi import WSGIServerfrom flaskr import appif __name__ == '__main__': WSGIServer(app, bindAddress='/tmp/flaskr-fcgi.sock').run()
Execute the fcgi file with the following permissions:
$ chmod +x flaskr.fcgi
Manually start server:
$ screen$ ./flaskr.fcgi
Use screen to run the server in the background, or:
$ nohup ./flaskr.fcgi &
Access localhost again to see our application.
Problems encountered
Once FastCGI server is run for the first time, it cannot be accessed. After checking the Nginx log, it is found that the Nginx server has no permission to access the socket file. Modify nginx. conf to add the user Configuration:
Copy codeThe Code is as follows: user wzy;
When Nginx is started, the following error occurs:
nginx: [emerg] getgrnam("wzy") failed in /usr/local/etc/nginx/nginx.conf:2
After Google detects that a user group needs to be added, the change is as follows:
Copy codeThe Code is as follows: user wzy wheel;
After Nginx is started again, everything is normal.
How to Use Nginx configuration item user:
Syntax: user user [group];Default: user nobody nobody;
If the group is ignored, Nginx will use a user group with the same name as the user. For example, if I set user wzy, Nginx will go to the user group wzy when it is started. This user group is not found on my computer, therefore, Nginx reports an error.