腾讯云 CentOS 8.2 64位 django部署(Tencent cloud CentOS 8.2 64 bit Django deployment)

CentOS 8.2 64位

1.先更新系统文件:yum update和yum upgrade的功能是一样的,都是将需要更新的package更新至软件源中的最新版。区别是 yum upgrade会删除旧版本的package,而yum update则会保留。因此如果软件依赖旧版本的package,最好是使用 yum update,确保不会出现兼容问题。

2.更新python3相关的依赖yum -y install python3-devel libevent-devel libjpeg-devel zlib-devel

3.更新pippython3 -m pip install –upgrade pip

查看linux系统当前安装哪几个python版本

运行以下命令查看当前linux系统已经安装了哪几个版本的python。

ll /usr/bin/pyth*

[root@VM-0-3-centos ~]# ll /usr/bin/pyth*lrwxrwxrwx 1 root root 25 Dec 10 2019 /usr/bin/python3 -> /etc/alternatives/python3lrwxrwxrwx 1 root root 31 May 21 02:33 /usr/bin/python3.6 -> /usr/libexec/platform-python3.6lrwxrwxrwx 1 root root 17 May 21 02:33 /usr/bin/python3.6-config -> python3.6m-configlrwxrwxrwx 1 root root 32 May 21 02:33 /usr/bin/python3.6m -> /usr/libexec/platform-python3.6mlrwxrwxrwx 1 root root 39 May 21 02:33 /usr/bin/python3.6m-config -> /usr/libexec/platform-python3.6m-configlrwxrwxrwx 1 root root 46 May 21 02:33 /usr/bin/python3.6m-x86_64-config -> /usr/libexec/platform-python3.6m-x86_64-configlrwxrwxrwx 1 root root 32 Nov 11 16:51 /usr/bin/python3-config -> /etc/alternatives/python3-config

改python默认版本操作步骤

查看版本:ll /usr/bin/python*删除有的python链接:rm /usr/bin/python设置新链接:ln -s /usr/bin/python3 /usr/bin/python

4.安装uwsgipip install uwsgi

5.测试uwsgi 根目录创建 test.py文件# test.pydef application(env, start_response): start_response(‘200 OK’, [(‘Content-Type’,’text/html’)]) return [b”Hello World”] # python3 #return [“Hello World”] # python2根目录运行 uwsgi –http :8000 –wsgi-file test.py

6.安装django(与本地环境一样) 也可以最后通过requirements.txt一同安装此处为了测试故先安装pip install Django==3.2.9

7.上传测试django项目注意django项目中的 settings.py文件中的 #ALLOWED_HOSTS = []ALLOWED_HOSTS = [‘*’]命令 cd 进入项目文件夹下(有 manage.py)的那个文件夹

命令:uwsgi mysite.ini

=================上面步骤uwsgi安装好了===========================下面开始安装MySQL 5.7查找是否有mysql是否有包:rpm -qa|grep mysql是否有文件:find / -name mysql1.正式开始安装:wget -i -c http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpmyum -y install mysql57-community-release-el7-10.noarch.rpmyum module disable mysqlyum -y install mysql-community-server安装完成 ,

2.启动MySQL:systemctl start mysqld.service

3.查看MySQL运行状态:systemctl status mysqld.service

4.获取root用户的密码:grep “password” /var/log/mysqld.log命令结果为:2021-11-10T05:52:57.942841Z 1 [Note] A temporary password is generated for root@localhost: Y45DZlApsb(a复制密码:Y45DZlApsb(a

5.进入数据库:mysql -uroot -p

进入数据库必须先改密码修改密码:(此时密码必须够复制,mysql有要求)mysql> ALTER USER ‘root’@’localhost’ IDENTIFIED BY ‘@Liyin3344520’;Query OK, 0 rows affected (0.00 sec)

6.设置密码复杂度查看选项:mysql> SHOW VARIABLES LIKE ‘validate_password%’;+————————————–+——–+| Variable_name | Value |+————————————–+——–+| validate_password_check_user_name | OFF || validate_password_dictionary_file | || validate_password_length | 8 || validate_password_mixed_case_count | 1 || validate_password_number_count | 1 || validate_password_policy | MEDIUM || validate_password_special_char_count | 1 |+————————————–+——–+7 rows in set (0.00 sec)

7.允许密码设置简单:mysql> set global validate_password_policy=0;Query OK, 0 rows affected (0.00 sec)

mysql> set global validate_password_length=1;Query OK, 0 rows affected (0.00 sec)

此时已经可以设置简单的密码了:ALTER USER ‘root’@’localhost’ IDENTIFIED BY ‘liyin’;

但此时还有一个问题,就是因为安装了Yum Repository,以后每次yum操作都会自动更新,需要把这个卸载掉:[root@localhost ~]# yum -y remove mysql57-community-release-el7-10.noarch

8、mysql忽略表大小写修改 /etc/my.cnf ;在[mysqld]下,加入一行:

lower_case_table_names=1

重启MySQL即可

systemctl enable mysqld.service —设置开启自启动

systemctl start mysqld.service —-开启此服务

systemctl stop mysqld.service —–关闭此服务

systemctl status mysqld.service —–查看服务状态

9.设置远程访问登录:mysql -uroot -p

mysql> grant all privileges on *.* to ‘root’@’%’ identified by ‘密码复杂度要高’ with grant option;Query OK, 0 rows affected, 1 warning (0.00 sec)

==============mysql5.7设置完成==========================

上传项目测试

django 设置

STATIC_ROOT=’/static/’

python manage.py collectstatic

生成模块导入文件pip freeze > requirements.txt导入模块文件pip install -r requirements.txt

===================================================

下面开始nginx设置

安装命令:sudo yum install nginx

停止服务sudo systemctl enable nginx启动服务sudo systemctl start nginx查看服务是否在运行sudo systemctl status nginx

ps:调整防火墙(Firewall)FirewallD是Centos 8中的默认防火墙解决方案。在安装过程中,Nginx使用预定义的规则创建防火墙服务文件,以允许访问HTTP(80)和HTTPS(443)端口。使用以下命令永久打开必要的端口:$ sudo firewall-cmd –permanent –zone=public –add-service=http$ sudo firewall-cmd –permanent –zone=public –add-service=https$ sudo firewall-cmd –reload

nginx配置

文件: /etc/nginx/nginx.conf

#在server节点下添加新的location项,指向uWSGI的ip与端口(即8000端口)。server { … location / { uwsgi_pass 127.0.0.1:8000; #将请求转发重定向到127.0.0.1的8000端口 include /etc/nginx/uwsgi_params;#将所有的参数转到uwsgi下 } …}

Nginx配置静态文件路径上面我们讲到 Nginx 可以实现动静分离,这个时候也需要配置,可以按照如下所示进行操作,可以在 /home/feng 录下新建一个 Book_static 目录与 static 目录,它们两者是前者包含后者的关系,然后在 settings.py 文件中进行如下配置:STATIC_ROOT=’/home/feng/Book_static/static’

配置完成成后在项目的 manage.py 文件下执行下述命令,收集静态文件:python3 manage.py collectstatic

收集完成后修改 Nginx 配置文件,在上述 server 中新添加 location /static 静态文件路由配置,重定向到指定的绝对路径:

server { … location /static { static /home/feng/Book_static; 文件夹所在绝对路径,示例如下: # 重定向,自动找到static目录 }

}

nginx 命令

接下来就是启动 Nginx 服务,使用如下命令进行启动:$ sudo /etc/init.d/nginx start|stop|restart|status# 或者$ sudo service nginx start|stop|restart|status

uwsgi 重启

修改后需要先停止 uWSGI 再重新启动它,如下所示:$ sudo uwsgi –stop uwsgi.pid$ sudo uwsgi –ini uwsgi.ini

# uwsgi.ini file[uwsgi]

# Django-related settings

# django项目运行的端口号socket = 127.0.0.1:8001

# django项目的根目录,同名目录的外层# the base directory (full path)# chdir = /home/mysite/mysitechdir = /root/mysite

# django项目同名目录内层自动生成的wsgi.py的路径,如果你的项目叫taobao,就填taobao.wsgi# Django s wsgi filemodule = mysite.wsgi

# 开启主进程# process-related settings# mastermaster = true

# 最大进程数量# maximum number of worker processesprocesses = 20

# 停止uwsgi时自动清理# … with appropriate permissions – may be needed# chmod-socket = 664# clear environment on exitvacuum = true

# 指定后台输出日志信息的文件,如果遇到不能正常使用,可以使用cat /root/mysite/log/uwsgi_log.log查看报错信息daemonize = /root/mysite/log/uwsgi_log.log

# 指定运行时候的pid文件,也可以用来停止进程, uwsgi –stop /root/mysite/log/uwsgi_pid.logpidfile = /root/mysite/log/uwsgi_pid.log

# 指定虚拟环境,如果没有使用虚拟环境可以不用指定;home = /usr/local/django2.2

static-map = /static=/root/mysite/statichttp = 0.0.0.0:8000

————————

CentOS 8.2 64位

1. Update system files first: the functions of Yum update and Yum upgrade are the same. Both update the package to be updated to the latest version in the software source. The difference is that Yum upgrade will delete the old package, while Yum update will keep it. Therefore, if the software relies on an older version of package, it is best to use Yum update to ensure that compatibility problems do not occur.

2.更新python3相关的依赖yum -y install python3-devel libevent-devel libjpeg-devel zlib-devel

3.更新pippython3 -m pip install –upgrade pip

Check which Python versions are currently installed on the Linux system

Run the following command to see which versions of Python have been installed on the current Linux system.

ll /usr/bin/pyth*

[root@VM-0-3-centos ~]# ll /usr/bin/pyth*lrwxrwxrwx 1 root root 25 Dec 10 2019 /usr/bin/python3 -> /etc/alternatives/python3lrwxrwxrwx 1 root root 31 May 21 02:33 /usr/bin/python3.6 -> /usr/libexec/platform-python3.6lrwxrwxrwx 1 root root 17 May 21 02:33 /usr/bin/python3.6-config -> python3.6m-configlrwxrwxrwx 1 root root 32 May 21 02:33 /usr/bin/python3.6m -> /usr/libexec/platform-python3.6mlrwxrwxrwx 1 root root 39 May 21 02:33 /usr/bin/python3.6m-config -> /usr/libexec/platform-python3.6m-configlrwxrwxrwx 1 root root 46 May 21 02:33 /usr/bin/python3.6m-x86_64-config -> /usr/libexec/platform-python3.6m-x86_64-configlrwxrwxrwx 1 root root 32 Nov 11 16:51 /usr/bin/python3-config -> /etc/alternatives/python3-config

Steps for changing the default version of Python

View version: ll / usr / bin / Python * delete existing Python links: RM / usr / bin / Python set new links: ln – S / usr / bin / Python 3 / usr / bin / Python

4.安装uwsgipip install uwsgi

5.测试uwsgi 根目录创建 test.py文件# test.pydef application(env, start_response): start_response(‘200 OK’, [(‘Content-Type’,’text/html’)]) return [b”Hello World”] # python3 #return [“Hello World”] # python2根目录运行 uwsgi –http :8000 –wsgi-file test.py

6. Django can also be installed (like the local environment) through requirements.txt. For testing, install PIP install Django = = 3.2.9 first

7. Upload the test Django project. Pay attention to the #allowed in the settings.py file in the Django project_ HOSTS = []ALLOWED_ Hosts = [‘*’] command CD to enter the folder under the project folder (with manage. Py)

命令:uwsgi mysite.ini

=================The above steps uwsgi are installed ==================================================================================================================== http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpmyum -y install mysql57-community-release-el7-10.noarch.rp Myum module disable MySQL Yum – y install MySQL community server installation is complete,

2.启动MySQL:systemctl start mysqld.service

3.查看MySQL运行状态:systemctl status mysqld.service

4.获取root用户的密码:grep “password” /var/log/mysqld.log命令结果为:2021-11-10T05:52:57.942841Z 1 [Note] A temporary password is generated for root@localhost: Y45DZlApsb(a复制密码:Y45DZlApsb(a

5. Enter the database: MySQL – uroot – P

Before entering the database, you must change the password and modify the password: (at this time, the password must be enough to copy, and MySQL has requirements) MySQL & gt; ALTER USER ‘root’@’localhost’ IDENTIFIED BY ‘@Liyin3344520’; Query OK, 0 rows affected (0.00 sec)

6.设置密码复杂度查看选项:mysql> SHOW VARIABLES LIKE ‘validate_password%’;+————————————–+——–+| Variable_name | Value |+————————————–+——–+| validate_password_check_user_name | OFF || validate_password_dictionary_file | || validate_password_length | 8 || validate_password_mixed_case_count | 1 || validate_password_number_count | 1 || validate_password_policy | MEDIUM || validate_password_special_char_count | 1 |+————————————–+——–+7 rows in set (0.00 sec)

7.允许密码设置简单:mysql> set global validate_password_policy=0;Query OK, 0 rows affected (0.00 sec)

mysql> set global validate_password_length=1;Query OK, 0 rows affected (0.00 sec)

At this point, you can set a simple password: alter user ‘root’ @’localhost ‘identified by’ Liyin ‘;

However, there is another problem at this time. Because Yum repository is installed, it will be updated automatically every time Yum operation is performed in the future. You need to uninstall this:[ root@localhost ~]# yum -y remove mysql57-community-release-el7-10.noarch

8. MySQL ignores table case and modifies / etc / my.cnf; Under [mysqld], add a line:

lower_case_table_names=1

Just restart mysql

systemctl enable mysqld.service —设置开启自启动

systemctl start mysqld.service —-开启此服务

systemctl stop mysqld.service —–关闭此服务

systemctl status mysqld.service —–查看服务状态

9. Set remote access login: MySQL – uroot – P

mysql> grant all privileges on *.* to ‘root’@’%’ identified by ‘密码复杂度要高’ with grant option;Query OK, 0 rows affected, 1 warning (0.00 sec)

==============Mysql5.7 setup completed==========================

Upload project test

Django settings

STATIC_ROOT=’/static/’

python manage.py collectstatic

生成模块导入文件pip freeze > requirements.txt导入模块文件pip install -r requirements.txt

===================================================

Let’s start nginx setup

安装命令:sudo yum install nginx

停止服务sudo systemctl enable nginx启动服务sudo systemctl start nginx查看服务是否在运行sudo systemctl status nginx

PS: adjust firewall firewalld is the default firewall solution in CentOS 8. During installation, nginx creates firewall service files using predefined rules to allow access to HTTP (80) and HTTPS (443) ports. Permanently open the necessary ports using the following command: $sudo firewall CMD — permanent — zone = public — add service = http $sudo firewall CMD — permanent — zone = public — add service = HTTPS $sudo firewall CMD — reload

Nginx configuration

文件: /etc/nginx/nginx.conf

#Add a new location item under the server node to point to the IP and port of uwsgi (i.e. port 8000). Server {… Location / {uwsgi_pass 127.0.0.1:8000; # redirect request forwarding to 8000 port 127.0.0.1, include / etc / nginx / uwsgi_params; # turn all parameters to uwsgi}…}

The static file path of nginx configuration. As mentioned above, nginx can realize dynamic and static separation. It also needs to be configured at this time. You can operate as follows. You can create a new book under the / home / Feng record_ Static directory and static directory are the former and contain the relationship between the latter. Then configure the following in the settings.py file: static_ ROOT=’/home/feng/Book_ static/static’

After the configuration is completed, execute the following command under the manage.py file of the project to collect the static file: python3 manage.py collectstatic

After the collection, modify the nginx configuration file, add a new location / static static static file routing configuration in the above server, and redirect to the specified absolute path:

server { … location /static { static /home/feng/Book_static; The absolute path of the folder, for example: # Redirect and automatically find the static directory}

}

Nginx command

The next step is to start the nginx service. Start it with the following command: $sudo / etc / init.d/nginx start|stop|restart|status# or $sudo service nginx start|stop|restart|status

Uwsgi restart

After modification, you need to stop uwsgi and restart it as follows: $sudo uwsgi — stop uwsgi.pid $sudo uwsgi — ini uwsgi.ini

# uwsgi.ini file[uwsgi]

# Django-related settings

#Port number of Django project running socket = 127.0.0.1:8001

# django项目的根目录,同名目录的外层# the base directory (full path)# chdir = /home/mysite/mysitechdir = /root/mysite

#The path of wsgi.py automatically generated by the inner layer of the directory with the same name of Django project. If your project is called Taobao, fill in taobao.wsgi# Django s WSGI filemodule = mysite.wsgi

# 开启主进程# process-related settings# mastermaster = true

# 最大进程数量# maximum number of worker processesprocesses = 20

# 停止uwsgi时自动清理# … with appropriate permissions – may be needed# chmod-socket = 664# clear environment on exitvacuum = true

#Specify the file for background output log information. If it cannot be used normally, you can use cat / root / MySite / log / uwsgi_ Log.log view the error message. Configure = / root / MySite / log / uwsgi_ log.log

#Specify the PID file at runtime, which can also be used to stop the process. Uwsgi — stop / root / MySite / log / uwsgi_ pid.logpidfile = /root/mysite/log/uwsgi_ pid.log

#Specify the virtual environment. If the virtual environment is not used, you can not specify it; home = /usr/local/django2.2

static-map = /static=/root/mysite/statichttp = 0.0.0.0:8000