Ubuntu+nginx+uwsgi+flask applications

Source: Internet
Author: User
Tags mongodb driver mongodb server file permissions nginx server virtualenv

Ubuntu 18.04,nginx 1.14.0,uwsgi 2.0.17.1,flask 1.0.2,python 3.6.5,

Multi-day Not updated blog, is busy to put their own flask application on Ubuntu run up, the amount is running in the production environment, so, need to use the UWSGI server, rather than flask the development with the server.

In addition, the way to learn Linux, such as user and user group management, environment variables configuration, various configuration file modification, process management, service random start configuration, but not proficient, but for the current deployment of their own flask application is enough.

In short, first on Ubuntu to run the program up!

This article shows the project architecture diagram that runs locally, as follows:

In the afternoon in the local environment to realize the boot automatically start the project required services, wherein the random start of the UWSGI server took a lot of their time, and their own before the installation of Uwsgi way, will not see Uwsgi log and Uwsgi configuration unfamiliar caused it!

Just finishing a flask project development, deployment of the whole process of the Fishbone diagram, for reference only (the approximate steps are right, the details need to be adjusted): Install software-Develop application (test)-Deploy application (test).

Of course, the following procedure is also suitable for other Python Web application development deployment, such as Django (others have not touched)!

P.S. Do you want to develop a Python Web application development deployment tool? One click to deploy the application on the corresponding Linux distribution, hey

P.S. Is there a way to deploy containers?

"Software Installation"

1.python3

Ubuntu 18 already has, Python3 is available;

2.pip3

Also already has, no words, please use the following command to install:

Apt-get Install Python3-pip

3.virtualenv

To install using PIP:

PIP3 Install Virtualenv

Then create a virtual working directory, which is described in front of the blog.

4.Flask

In the afternoon configuration Uwsgi load flask application randomly started to encounter problems, although the system's Python3 has been installed flask, but the error log hint can not find the Flask module, and try to configure Pythonpath will not solve the problem, finally, through Modules such as flask installed in the virtual working directory are configured.

Using PIP installation (virtual working directory, below):

Pip Install flask

5.MongoDB and Python's MongoDB driver Pymongo

5.1.MongoDB Installation

The latest version is 4.0, refer to the official Website installation method:

https://docs.mongodb.com/manual/tutorial/install-mongodb-on-ubuntu/

The recommended using. Deb Packages (Recommended) is selected and installed in future 4.0.

Of course, the configuration after installation is necessary, add roles, users, and so on.

5.2. Installing the Pymongo module

To install using PIP:

Pip Install Pymongo

6.Nginx Installation

Before installation, the Pcre, OpenSSL and other installation is good;

The use of the way from the source of the installation;

7.uWSGI Installation

There are two ways of doing this:

PIP installation;

Apt/apt-get installation;

I first used the PIP installation (but not installed in the virtual working directory), but in the configuration Uwsgi random start encountered a series of problems, finally abandoned and uninstalled, instead of using Apt/apt-get installation;

When you install with PIP, the Uwsgi command appears in the . Local of the current user's home directory , and when Apt/apt-get is installed, the UWSGI command appears in the public directory and is available to all users;

PIP installation, do not find the configuration, log and other storage location, and Apt/apt-get installation,/etc/under the Uwsgi configuration file,/var/log/has its log file-here, solitary see some random start configuration error message, Finally, the problem is solved along with the error message;

Of course, if the PIP installation is installed in the virtual working directory, the problem may also be resolved-to be verified.

Apt/apt-get installation, also install the PYTHON3-UWSGI plug-in, the installation command is as follows:

sudo apt install uwsgi uwsgi-plugin-python3

Reference links (There are a lot of documents you've seen that can't be recorded here):

How to properly use Uwsgi by park friend Iamroot

Linux under Nginx installation method (Pcre and OpenSSL)

Uwsgi Chinese documents-Options (config)

"Startup Configuration"

Configuration is divided into two steps, one is to manually start the software or service after configuration, and the second is to randomly start the software or service after configuration.

Of course, there is a third step, after the configuration, the service can be automatically restored after hanging off.

This is the second step, but there are a number of ways to improve the configuration of the second step.

1.Nginx Configuration

When installing with Apt/apt-get, the configuration file exists in the/etc/nginx/directory:

At present, it is directly modifying the default file in the Sites-available directory to implement the request forwarded to the backend Uwsgi server.

With Apt/apt-get installation, the Nginx server is already randomly booted and its script is in the/etc/init.d/directory:

Nginx log files are stored in the/var/log/nginx/directory:

2.MongoDB Configuration

Version is the latest 4.0, the Mongod, MONGO command is available to all users after installation using the official. Deb method.

However, the service needs to execute its own commands (assuming the reader is already using MongoDB).

MongoDB will have a configuration file, but the orphan is currently only using command line startup mode, the command is as follows:

Mongod--dbpath/home/log/ws/mdb/data0725--logpath/home/log/ws/mdb/data0725/log--logappend--auth--directoryperdb

The default port, which requires authentication, can only be accessed natively.

To configure random startup:

To establish a startup script in the/etc/init.d/directory mongo01:

There is only one command, Note that the commands need to be added & symbols at the end .

To establish a mongo01 soft connection in the/etc/rc3.d/directory:

Yes, after restarting the computer, you can use MONGO to access the MongoDB server.

3.uWSGI Configuration

This is a serious problem ! It took a lot of time!

As mentioned earlier, the use of PIP installation mode, has been unable to configure the random start.

After you change to Apt/apt-get installation mode, the configuration file is found, and then the random boot is completed in step-by-step configuration.

In both installations, the UWSGI command can be used, but the PIP method is installed only for the user who installed it, while the Apt/apt-get mode is available to all users, and, more typically, even though the version is lower.

This article only describes the process of successfully configuring a random start in Apt/apt-get installation mode.

The configuration of the Uwsgi is present in the /etc/uwsgi/ :

Contains two directories--apps-available, apps-enabled (and Nginx-like), where the former stores a real configuration file, can be multiple--need to avoid conflict it? , which stores soft links to the former configuration file.

The most important problem is here: Lone thought to put the previously executed configuration files directly to the apps-available on it, but, does not work, and then, the time of the afternoon will be spent here!

Compare the following two manual vs automatic versions of the configuration files:

Compared to manual, more plugins, virtualenv two configuration items, in addition, the socket configuration with a socket file under the/run/uwsgi/.

It's unclear why plugins is Python, not python3, because the Python version of the virtual working directory is Python 3? Error message:

--Unavailable modifier requested:0--

Socket using/run/uwsgi/socket file, and permissions have a relationship, the specific is not clear. Error message due to log file permissions issue:

Error removing UNIX socket, unlink (): Permission denied [core/socket.c Line 198]

About the configuration virtualenv, there is no configuration, the following error will appear: Unexpectedly found flask (during the attempt to configure the Pythonpath, have failed, plus the previous read blog is configured virtualenv, so, will have this attempt, otherwise, It takes more time to finish, but it's time to look at the blog before it's too expensive.

From flask import flask, render_template
Modulenotfounderror:no module named ' Flask '

Fortunately, found the Uwsgi global log file, which exists in/var/log/uwsgi/, each app a log file (thanks to the log file):

The name of the log file corresponds to the soft link name in the apps-enabled directory in the configuration file.

Random start? Write a configuration file that Uwsgi loads the flask application, throw it under apps-available and create a soft link in the apps-enabled.

Other applications are the same, right?

Then, the others are responsible for UWSGI's startup script in the/etc/init.d/directory (quite long, and cannot be changed at this time):

Of course, there are other ways to configure random startup, and you need to learn. Why is the soft link of MongoDB's startup script built in the/etc/rc3.d/directory? This is related to the runlevel of Linux.

Finally, restart, the test is started randomly. Execute the following command after startup:

Ps-ef | Grep-e nginx-e mongod-e Uwsgi

Display, three servers are started.

Further, through the browser to access their own development of the flask application, the results show is also successful, otherwise, please check the various problems!

Postscript

What if the service is broken?

Notice?

Monitoring?

Planning tasks?

Systemd

The road is long ~

Ubuntu+nginx+uwsgi+flask applications

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.