Write first:

I’m very sorry, because of the sudden outbreak, hadoop series off more, very afflictive, distributed environment at school, because I don’t take my notebook configuration and code to run up and I’m afraid to write a blog, and then the winter holiday at home to write code to also do not have what meaning, see the book very much, the meter network and operating system to learn again. Due to the need of internship, more of my skills may be in Python in the future, but I will try to keep the Java related content updated, and I will also publish more introductory tutorials to you. Thank you.

Background:

In the process of learning Django recently, I made a small demo with Django, so I will try to see if it is possible to deploy django on the server. By the way, I am familiar with the whole process of deploying Django, because I used uWSGI as a Python Web server when LEARNING flask. So this time I chose a combination of Nginx + UWSGi.

After buying the cloud server, I installed the pagoda. it was really easy to deploy PHP using pagoda. so I tried to see if Pagodacould deploy Django. Then I will try to highlight the potential pitfalls in the following process so that I can write fewer configuration files in the future if I need to blog or deploy again.

Environment:

System version: centos7.5

Python version: 3.6.5

Nginx version: 1.16.1

Uwsgi version: 2.0.18

If the environment is different, please refer to other information to configure.

Environment Preparation:

Before deploying, make sure you have python installed on your Linux server. If Python 3 is required, consult the documentation to upgrade and install it.

First, we need to install the Python virtual environment for better standalone deployment and to avoid interference with other projects:

sudo pip install virtualenv
sudo pip install virtualenvwrapper
Copy the code

Virtualenvwrapper is an extension to virtualEnv to consolidate all virtual environments in one directory.

Configure the virtual environment:

mkdir ~/.virtualenvs
Copy the code

Open. Bashrc:

sudo vim ~/.bashrc
Copy the code

Add the following to the end of.bashrc:

export WORKON_HOME=$HOME/.virtualenvs  All virtual environments store directories
source /usr/local/python3/bin/virtualenvwrapper.sh
Copy the code

Attention! : The/usr/local/bin/virtualenvwrapper. Sh is just for my current system environment of a place, not all servers are in this position, if you don’t know virtualenvwrapper. Where is the sh, You can search the file to find its location in the system, and modify.bashrc.

Enabling profiles:

source ~/.bashrc
Copy the code

The virtualenvwrapper.sh file is found incorrectly.

Create a virtual environment:

Create an env folder somewhere you think you can remember it:

cd /www
mkdir env
cd env Enter the env directory
Copy the code

Create a new virtual environment:

mkvirtualenv -p /usr/bin/python3 orange_env    # my_env is the name of the virtual environment
Copy the code

If /usr/bin/python3 does not exist, an error will be reported. If /usr/bin/python3 does not exist, you will need to create a new connection:

If an error is reported:

ln -s /usr/local/python3/bin/python3 /usr/bin/python3 Change the path to your own Python installation path
Copy the code

Then we can enter our virtual environment:

source /www/env/orange_env/bin/activate
Copy the code

After entering the virtual environment, the name of your virtual environment will appear in parentheses:

(orange_env) [root@iz2ze1cvux96riiwfh05qqz ~]# 
Copy the code

Installing UWSGi in a virtual environment:

pip install uwsgi
Copy the code

Exit the virtual environment:

deactivate
Copy the code

Install UWSGi in the main environment again:

pip install uwsgi
Copy the code

Note: If you have other dependencies, such as Django or MSQLClient, be sure to install PIP in the virtual environment.

Deployment process:

Create a new folder and upload your Django project to it:

Take me as an example:

cd /www
mkdir orange
Copy the code

The upload and unzip operations are omitted. Remember to upload the project root directory, which is the directory with manage.py directly.

Create a new USWGI configuration file. Uswgi supports several configuration file types, such as YAML, XML, JSON, ini, and I chose XML.

vim mysite.xml Remember to keep mysite.xml in the same directory as your project's manage.py.
Copy the code

Mysite.xml contains the following contents:

<uwsgi>    
   <socket>127.0.0.1:8080</socket><! -- Internal port, custom --> 
   <chdir>/www/orange/</chdir><! -- Project path -->            
   <module>orangeproject.wsgi</module> <! Project name + WSGI -->  
   <processes>4</processes> <! -- Number of processes -->     
   <daemonize>uwsgi.log</daemonize><! -- Log file -->
</uwsgi>
Copy the code

Install Nginx:

I have written a series of basic tutorials on Nginx before. If you are not familiar with the installation, start and restart of Nginx, please read this article:

Nginx at the backend

To view the nginx configuration file path:

nginx -t
Copy the code

Remember to back up the configuration file before nginx, and delete all the previous configuration, directly add the following:

worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; server { listen 8000; Server_name Your domain name; charset utf-8; location / { include uwsgi_params; Uwsgi_pass 127.0.0.1:8080; uwsgi_param UWSGI_SCRIPT orangeproject.wsgi; uwsgi_param UWSGI_CHDIR /www/orange/; } location /static/ { alias /www/orange/transfer/static/; }}}Copy the code

Note that the module name must be the same, and that the port on which your Nginx listens cannot be the same as the port on which you start Django. Otherwise, usWGI will fail to start because nginx occupies the port. alias /www/orange/transfer/static/; This is your static file address, CSS, IMG, etc.

Check whether nginx is configured successfully:

nginx -t
Copy the code

Restart the nginx:

nginx -s reload
Copy the code

After that, enter our virtual environment orange_env again and start our UWSGI server:

cd /www/orange
uwsgi -x mysite.xml
Copy the code

Then open our local browser, type: domain name: 8000, registered can change the nginx configuration file to 80.

Done and done: