Deploy the uwsgi+nginx+Django project to the Alibaba Cloud server

Deploy the uwsgi+nginx+Django project to the Alibaba Cloud server

Hello friends, it’s another day to get new skills every day, get started with 0 basics, get in the car while it’s hot~~.
Today, let's take a look at how to use the uwsgi+nginx+Django project to deploy to the Alibaba Cloud server~~.

1. Environmental preparation

Aliyun cloud server ECS burst performance t5 (free to receive)

Cloud server image: Ubuntu18.04 64-bit (with Python 2.7.17, Python 3.6.9)

Django == 3.2.4

uWSGI == 2.0.19.1

nginx == 1.14.0

MySQL == 5.7.34

Tools: Xshell7, Xftp7, SQLyog

2. Configure the environment required for the project

1. Synchronize the source and update the source

root@localhost:~# sudo apt-get update
root@localhost:~# sudo apt-get upgrade
# 1、apt-get update是同步 /etc/apt/sources.list 和 /etc/apt/sources.list.d 中列出的源的索引,这样才能获取到最新的软件包。
# 2、apt-get update只是更新了apt的资源列表,没有真正的对系统执行更新。如果需要,要使用apt-get upgrade来更新。
# 3、最后,需要注意的一点是,每回更新之前,我们需要先运行update,然后才能运行upgrade和dist-upgrade,因为相当于update命令获取了包的一些信息,比如大小和版本号,然后再来运行upgrade去下载包,如果没有获取包的信息,那么upgrade就是无效的啦!

# 4、如果报错了操作如上面
其实错误信息已经很明确了,Unable to locate packet就是无法找到包嘛,
那还不赶紧sudo apt-get update下!

(1.1) Install python3-pip (basically no operation is required, because the python that comes with the system has pip)

root@localhost:~# apt-get install python3-pip
Reading package lists... Done
Building dependency tree       
Reading state information... Done
python3-pip is already the newest version (9.0.1-2.3~ubuntu1.18.04.5).
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
# 返回信息说明了Ubuntu18.04.5 已经有pip并且版本为9.0.1-2.3

(1.2) Be sure to update the pip command:

root@localhost:~# python3 -m pip install --upgrade pip
Collecting pip
  Downloading http://mirrors.cloud.aliyuncs.com/pypi/packages/ca/31/b88ef447d595963c01060998cb329251648acf4a067721b0452c45527eb8/pip-21.2.4-py3-none-any.whl (1.6MB)
    100% |████████████████████████████████| 1.6MB 35.9MB/s 
Installing collected packages: pip
  Found existing installation: pip 9.0.1
    Not uninstalling pip at /usr/lib/python3/dist-packages, outside environment /usr
Successfully installed pip-21.2.4

2. Install the virtual environment and virtual environment management package (basically comes with it, download it if you don’t have it)

root@localhost:~# /usr/local/lib/python3.6/dist-packages  # 查看是否有virtualenv 和 virtualenvwrapper 
# 没有的话就操作如下
root@localhost:~# pip3 install virtualenv  # 要先更新pip
root@localhost:~# pip3 install virtualenvwrapper  # 包管理工具

3. Modify the ./bashrc file, use the command: vi .bashrc

(3.1) View the installation directory

which python3

which virtualenvwrapper.sh

Tips: The files starting with . are all hidden files, you need the command: ls -al or ls -lha to see them.

Press i to enter edit mode, press esc in the upper left corner to exit edit mode, press shift+:wq to save and exit.

(3.2) Add content as shown below.

# 虚拟环境存放目录(无需自己创建)
export WORKON_HOME=~/Envs

# 激活virtualenvwrapper包管理
source /usr/local/bin/virtualenvwrapper.sh

# 虚拟环境指定python路径
VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3

(3.3) Refresh the configuration file

root@localhost:~# source .bashrc

4. Create a new virtual environment and its common commands

(4.1) Create a new virtual environment

# 新建了一个web的虚拟环境,并且进入了web这个环境。
root@localhost:~# mkvirtualenv web

(4.2) Exit the current virtual environment

(web)root@localhost:~# deactivate

(4.3) View all virtual environments

# 这两个命令一般都是可以的。
(web)root@localhost:~# workon 
or 
(web)root@localhost:~# lsvirtualenv

(4.4) Delete the virtual environment

# 删除了env虚拟环境
(web)root@localhost:~# rmvirtualenv env

(4.5) Enter a virtual environment

# 即使没有退出当前虚拟环境,workon也是直接可以切换到其他虚拟环境的
(web)root@localhost:~# workon web

5. Migrate all packages in the virtual environment on the project to the Alibaba Cloud server

(5.1) Update pip in the virtual environment

(web)root@localhost:~# python -m pip install --upgrade pip

(5.2) Download the required packages for the project (be sure to enter the virtual environment)

(web)root@localhost:~# pip3 install 库名
requests
django3.2.4
pymysql
uwsgi

6. Install the database MySQL

(6.1) It is best to update the source before downloading

(web)root@localhost:~# apt-get update #更新源
(web)root@localhost:~# apt-get install mysql-server -y  # -y参数就是一路yes

(6.2) Two dependent packages

(web)root@localhost:~# apt-get install mysql-client

(web)root@localhost:~# apt-get install libmysqlclient-dev -y  # -y参数就是一路yes

(6.3) Initialization configuration (reference: https://blog.csdn.net/weixx3/article/details/80782479)

(web)root@localhost:~# mysql_secure_installation
  1. The Validate Password plugin can be used to test passwords and improve security. It checks the strength of passwords and allows users to set only those passwords that are sufficiently secure. Do you want to set up the Verify Password plugin?
    Select: n
    2.
    By default, mysql installation has an anonymous user, which allows anyone to log in to mysql without creating a user account for it. This is just for testing and to make the installation process smoother. You should remove them before going into production.
    Choose: y
    3.
    Normally, root should only allow connections from "localhost". This ensures that someone cannot guess the root password from the network.
    Choice: y
    4.
    By default, mysql has a database "test" that anyone can access. This is also just for testing and should be removed before moving to production. delete the test database and access it?
    Select: n
    5.
    Reloading the privilege table will ensure that all changes made so far will take effect immediately?
    select: y

(6.4) Modify the mysqld.cnf configuration file

(web)root@localhost:~# vim /etc/mysql/mysql.conf.d/mysqld.cnf

​ Skip password login (the author does not use it here) skip-grant-tables

​ Enable remote login (comment the code below or replace it with 0.0.0.0)

#bind-address = 127.0.0.1
bind-address = 0.0.0.0

​ View IP

(web)root@localhost:~# ifconfig(必须下载apt install net-tools,云服务器好像有了)
(web)root@localhost:~# ip addr(无需下载)

​ Check mysql service status

(web)root@localhost:~# systemctl status mysql.service 
(web)root@localhost:~# systemctl start mysql.service  # 开启MySQL服务

(6.5) By default, MySQL only allows local login, and the remote connection is enabled as follows:

(web)root@localhost:~# mysql -uroot -p  # 输入root密码

​ Create a database and specify a character set (create a database according to your own needs)

mysql>create database django_mysql charset=utf8;

​ MySQL remote connection authority, which authorizes all users to have all the authority of the database.

# '%':配置所有ip可以通过root:123456访问数据库(%换成你的公网ip也行,一定要重启服务)
mysql>GRANT ALL PRIVILEGES ON *.* TO root@'%' IDENTIFIED BY "123456";
mysql>flush privileges;  # 一定要刷新系统权限表

​ Check whether user permissions have changed

mysql>select User,authentication_string,Host from user; 

7. Migrating data

(7.1) First, you must go back to the Alibaba Cloud server, click More, select Network and Security Group,
see the configuration rules on the right, and add port 3306.
Be sure to restart the service

(7.2) Copy the Ubuntu database data to the MySQL database on the Alibaba Cloud server through the SQLyog tool

8. Migrate the Django project to the cloud server

Method 1: Here the author uses xShell7 and xFTP7 for project migration.

Method 2: You can also use the scp command to transfer

(web)root@localhost:~# scp -r /home/ubuntu/renjie/MyFirstWeb root@localhost:/root/renjie/MyFirstWeb  
  1. /home/ubuntu/renjie/MyFirstWeb is the root directory of the project;
  2. root is the user name of Alibaba Cloud server;
  3. localhost is the public IP of your Alibaba Cloud server;
  4. /root/renjie/MyFirstWeb is the file location on the Alibaba Cloud server;
  5. You will be prompted to enter your Alibaba Cloud server login password, and the transmission will start after the password is correct.

9. Configure uwsgi

  1. Enter the virtual environment workon web
(web)root@localhost:~# pip3 install uwsgi
  1. First enter the setting modification
DEBUG = False  # 关闭调试模式。
ALLOWED_HOSTS = ['*']  #后续会更改的,域名或者公网IP,允许所有主机访问。
  1. Create a configuration file myfirstweb.ini (more setting at the same level)
[uwsgi]

# uwsgi才使用
#http=127.0.0.1:8000

# 有了NGINX使用
socket=127.0.0.1:8000

# 项目的绝对路径
chdir=/root/renjie/MyFirstWeb

# wsgi.py相对路径
wsgi-file=myfirstweb/wsgi.py

# 进程数
process=4

# 线程数
threads=2

# 进程pid
pidfile=myfirstweb.pid

# 后台运行日志输出
daemonize=myfirstweb.log

# 主进程
master=True
  1. Test uwsgi (enter MyFirstWeb)

    4.1 Test method one

    ​ Start uwsgi

    (web)root@localhost:~# uwsgi --ini myfirstweb.ini
    

    ​ stop uwsgi

    (web)root@localhost:~# uwsgi --stop myfirstweb.pid
    

    ​ Process view (whether it is started or closed, you need to look at the process)

    ​ Any modification of the code in Django requires restarting uwsgi

    (web)root@localhost:~# ps aux|grep 'uwsgi'
    

    4.2 Test Method 2

    (web)root@localhost:~# uwsgi --http :8000 --module myfirstweb.wsgi
    

9. Install NGINX

  1. Install
(web)root@localhost:~# apt-get install nginx

​Verification: Enter the ip of the virtual machine directly in the browser and press Enter

  1. Configure NGINX
(web)root@localhost:~# vim /etc/nginx/sites-enabled/default
location / {
		#First attempt to serve request as file, then
		#as directory, then fall back to displaying a 404.
		#try_files $uri $uri/ =404;

		uwsgi_pass 127.0.0.1:8000;
		include /etc/nginx/uwsgi_params;
}
#项目静态文件的绝对路径。即:告诉nginx应该去哪里找静态文件。
location /static {
        root /root/renjie/MyFirstWeb_static;
}

​ Save and exit, restart the NGINX service

(web)root@localhost:~# /etc/init.d/nginx restart

​ After the restart is successful, the display is as follows:
​ [ ok ] Restarting nginx (via systemctl): nginx.service.
​ If it fails: (It will judge whether the syntax is wrong, and will give the line number)

(web)root@localhost:~# nginx -t
(web)root@localhost:~# vim /etc/nginx/nginx.conf
# 修改为 user root;

​ Process view (whether it is started or closed, you need to look at the process)

(web)root@localhost:~# ps aux|grep 'nginx'
  1. setting added
STATIC_ROOT = '/root/renjie/MyFirstWeb_static/static'
  1. Collect static files into STATIC_ROOT to execute commands
(web)root@localhost:~# python manage.py collectstatic

Start NGINX

/etc/init.d/nginx start
或者 service nginx start

stop-nginx

/etc/init.d/nginx stop
或者 service nginx stop

Restart NGINX

/etc/init.d/nginx restart
或者 service nginx restart

nginx log location

异常:/var/log/nginx/error.log
正常:/var/log/nginx/access.log

Zongsuke: So here~~ We have basically set up the environment on which the Django project depends.

If you have any questions, please contact the author email: [email protected]

Reference link:

Django learning video: https://www.bilibili.com/video/BV1vK4y1o7jH?from=search&seid=659747147674964351

Python virtual environment creation: https://3g.163.com/dy/article/FEFB3H4B05315PUD.html?from=history-back-list

MySQL initialization: https://blog.csdn.net/weixx3/article/details/80782479

Guess you like

Origin blog.csdn.net/weixin_49237144/article/details/118416410
Recommended