Django server deployment (Nginx+uwsgi +Ubuntu)

overview

After developing one of your own Django projects locally, deploy it to the server for more people to access.

Preparation

Deploying a website requires a server and a domain name. The two are indispensable. The Alibaba Cloud ECS cloud server I use is also a domain name purchased on Alibaba Cloud. For the domain name, if there is no special use, you can buy it according to your own ideas, such as the name, the common suffixes of the domain name are .com (company), .org (organization), .gov (government), . edu (education), if it is your own blog, you can use a short domain name that is easier to remember, such as io, xyz. In short, the domain name depends on your own ideas and the needs of the project (the interface of the WeChat applet requires a .com domain name).

For the selection of servers, common servers can be divided into file servers, Web servers, database servers, mail servers, etc. If it is used to deploy personal Web projects, choose a Web server. Then consider the performance and compatibility of the server. Before purchasing, you must determine what to run and how much the load is, so that you can determine the approximate configuration of the server, including CPU, memory, hard disk, bandwidth, etc. If your website contains some relatively large If you want to store videos or materials, the bandwidth must be higher, or consider third-party services, such as Qiniu Cloud or Alibaba Cloud's object cloud storage, which can be quickly accessed and easily help us solve problems. In addition, the after-sales service of the server should also be considered. If there is any problem, the problem can be solved quickly through the after-sales service. I think Alibaba Cloud does a better job of providing 24-hour technical support. And the speed of solving problems is also relatively fast, so I chose Alibaba Cloud. Another important point is that if you are not familiar with the Linux server operating system, you should choose the Windows system honestly. It is more convenient for you to deploy and operate, although most servers use Linux. , It is very inconvenient when you are not familiar with using it. You need to check on Baidu for any commands.

Free deployment

Install Nginx. Install nginx via apt-get install nginx. After installing Nginx on Ubuntu, all configuration files of Nginx are under /etc/nginx, the startup program files are under /usr/sbin/nginx; the log files are under /var/log/nginx, and in /etc/ A startup script is created under init.d:

    sudo /etc/init.d/nginx start #启动
    sudo /etc/init.d/nginx stop #停止
    sudo /etc/init.d/nginx restart #重启
 

Install UWSGI. Install via apt-get install python-dev and pip install uwsgi. The general process here is: Nginx, as the front end of the server, is responsible for receiving all requests from the Client and managing them in a unified manner. Static requests are handled by Nginx itself. The non-static request is passed to Django through uwsgi, which is processed by Django to complete a web request.

Nginx+Uwsig+Django

 

Configure the uwsgi file. The uwsgi file uses many parameters when starting Uwsgi as a configuration file, and then you can start uwsgi through the configuration file without a long command to start (this file is placed at the same level as manage.py in the django project).

 

Among them, chdir refers to the full path of the Django project, socket refers to the port number where django runs, and module refers to the uwsgi.py when django was created, that is, it points to this file.

 

Configure Nginx. Open the /etc/nginx/sites-enabled directory and edit the nginx configuration file default

and configuration of static files

Then restart the Nginx service and the UWSGI service (uwsgi -d --ini uwsgi.ini) to run the UWSGI service in the background.

 

The above access refers to successful access through IP address access. If you want to configure domain name access, you also need to configure resolution on Alibaba Cloud or Tencent Cloud, including domain name resolution, security group configuration, port settings, etc. before you can pass The domain name is normally accessed. If HTTPS access is required, a CA certificate needs to be applied for, and then HTTPS related configurations must be configured. The deployment of the project is considered complete after completing these tasks.

 

Guess you like

Origin blog.csdn.net/qq_35363507/article/details/104418160