一、创建新linux用户
1、创建用户
命令:sudo useradd -m 用户名
sudo --- 给用户增加sudo权限
-m --- 自动建立用户的登入目录
2、设置密码
命令:sudo passwd 用户名
给指定用户设置密码,两次输入同一个密码即可
3、在/etc/sudoers中给用户赋予权限
4、在/etc/passwd中设置命令解释器
二、安装python 3.10.0
1、安装依赖包
yum install -y zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel xz-devel libffi-devel
2、将下载的python源码包解压
3、切换到python源码目录做安装操作
./configure #可加--prefix=/your path 参数来指定安装目录
make
make test # 可测可不测
sudo make install
4、执行python3 -V命令查看版本
[root@tvtrt0fin18s1gj1 redis-stable]# python3 -V
Python 3.10.0
[root@tvtrt0fin18s1gj1 redis-stable]#
三、将supervisor制作成系统服务
Supervisor是用Python开发的进程管理工具,可以很方便的用来启动、重启、关闭进程(不仅仅是 Python 进程)。能将一个普通的命令行进程变为后台daemon,并监控进程状态,异常退出时能自动重启。除了对单个进程的控制,还可以同时启动、关闭多个进程,比如很不幸的服务器出问题导致所有应用程序都被杀死,此时可以用 supervisor 同时启动所有应用程序而不是一个一个地敲命令启动。
它是通过 fork/exec 的方式把这些被管理的进程当作supervisor的子进程来启动,这样只要在supervisor的配置文件中,把要管理的进程的可执行文件的路径写进去即可。也实现当子进程挂掉的时候,父进程可以准确获取子进程挂掉的信息的,可以选择是否自己启动和报警。supervisor还提供了一个功能,可以为supervisord或者每个子进程,设置一个非root的user,这个user就可以管理它对应的进程
1、安装supervisor
pip3 install supervisor
2、生成配置文件
# pip 安装了supervisor之后自然就有echo_supervisord_conf命令了
echo_supervisord_conf > supervisor.conf
3、修改supervisor.conf
[unix_http_server]
;1. socket 文件,改为自己的目录
file=/var/run/supervisor.sock ; the path to the socket file
;chmod=0700 ; socket file mode (default 0700)
;chown=nobody:nogroup ; socket file uid:gid owner
;username=user ; default is no username (open server)
;password=123 ; default is no password (open server)
... ; 注释
;[inet_http_server] ; inet (TCP) server disabled by default
;2.端口、用户名和密码
port=127.0.0.1:9001 ; ip_address:port specifier, *:port for all iface
username=root ; default is no username (open server)
password=jwJW489355 ; default is no password (open server)
[supervisord]
;3.日志文件目录
logfile=/var/log/supervisord.log ; main log file; default $CWD/supervisord.log
logfile_maxbytes=50MB ; max main logfile bytes b4 rotation; default 50MB
logfile_backups=10 ; # of main logfile backups; 0 means none, default 10
loglevel=info ; log level; default info; others: debug,warn,trace
;4.pid文件目录
pidfile=/var/run/supervisord.pid ; supervisord pidfile; default supervisord.pid
nodaemon=false ; start in foreground if true; default false
silent=false ; no logs to stdout if true; default false
minfds=1024 ; min. avail startup file descriptors; default 1024
minprocs=200 ; min. avail process descriptors;default 200
;umask=022 ; process file creation umask; default 022
;user=supervisord ; setuid to this UNIX account at startup; recommended if root
;identifier=supervisor ; supervisord identifier, default is 'supervisor'
;directory=/tmp ; default is not to cd during start
;nocleanup=true ; don't clean up tempfiles at start; default false
;childlogdir=/tmp ; 'AUTO' child log dir, default $TEMP
;environment=KEY="value" ; key value pairs to add to environment
;strip_ansi=false ; strip ansi escape codes in logs; def. false
; The rpcinterface:supervisor section must remain in the config file for
; RPC (supervisorctl/web interface) to work. Additional interfaces may be
; added by defining them in separate [rpcinterface:x] sections.
[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
; The supervisorctl section configures how supervisorctl will connect to
; supervisord. configure it match the settings in either the unix_http_server
; or inet_http_server section.
[supervisorctl]
;5.改成和第一处的目录一样就行了
serverurl=unix:///var/run/supervisor.sock ; use a unix:// URL for a unix socket
;serverurl=http://127.0.0.1:9001 ; use an http:// url to specify an inet socket
;username=chris ; should be same as in [*_http_server] if set
;password=123 ; should be same as in [*_http_server] if set
;prompt=mysupervisor ; cmd line prompt (default "supervisor")
;history_file=~/.sc_history ; use readline history if available
; The sample program section below shows all possible program subsection values.
; Create one or more 'real' program: sections to be able to control them under
; supervisor.
... ; 很多注释太长了
;[include]
;6.supervisor管理的进程配置文件所在位置,每个ini文件对应supervisor管理的一个进程
files = /etc/supervisord.d/*.ini
将supervisor.conf文件拷贝到/etc下,并且在/etc下创建supervisord.d目录
4、创建supervisord.service文件
文件为:/usr/lib/systemd/system/supervisord.service
填入以下内容:
[Unit]
Description=Process Monitoring and Control Daemon
After=rc-local.service nss-user-lookup.target
[Service]
Type=forking
PIDFile=/var/run/supervisord.pid
ExecStart=/usr/local/bin/supervisord -c /etc/supervisord.conf
ExecStop=/usr/local/bin/supervisorctl shutdown
ExecReload=/usr/local/bin/supervisorctl reload
KillMode=process
Restart=on-failure
RestartSec=22s
[Install]
WantedBy=multi-user.target
注意:有个并非是文件与配置不对的错误执行一下 sudo unlink /var/run/supervisor.sock 就行了。
这样就可以通过systemctl命令来操作supervisord服务了。
四、安装redis
1、下载redis源码
2、编译安装redis
make USE_SYSTEMD=yes
make install
3、修改配置文件redis.conf
修改如下配置即可:
dir /usr/local/redis/data #redis数据目录
logfile "/var/log/redis.log" #日志文件
supervised systemd #服务方式
daemonize yes #后台服务方式运行
bind 0.0.0.0 -::1 #外部可访问
将redis.conf转移到/etc下
4、将utils/systemd-redis_server.service转移到/usr/lib/systemd/system/redis.service并修改
# 第一行注掉
#ExecStart=/usr/local/bin/redis-server --supervised systemd --daemonize no
## Alternatively, have redis-server load a configuration file:
#开启并修改配置文件目录
ExecStart=/usr/local/bin/redis-server /etc/redis.conf
然后就可以用systemctl开操作redis了
五、安装mysql8.0
1、下载mysql80-community-release
2、使用yum安装
[root@tvtrt0fin18s1gj1 software]# yum install mysql80-community-release-el8-4.noarch.rpm
Last metadata expiration check: 0:39:54 ago on Thu 03 Nov 2022 01:00:12 PM CST.
Dependencies resolved.
===============================================================================================================================================================================================
Package Architecture Version Repository Size
===============================================================================================================================================================================================
Installing:
mysql80-community-release noarch el8-4 @commandline 14 k
Transaction Summary
===============================================================================================================================================================================================
......
3、安装mysql-community-server
[root@tvtrt0fin18s1gj1 software]# yum install mysql-community-server
MySQL 8.0 Community Server 234 kB/s | 2.5 MB 00:11
MySQL Connectors Community 18 kB/s | 84 kB 00:04
MySQL Tools Community 105 kB/s | 583 kB 00:05
Last metadata expiration check: 0:00:04 ago on Thu 03 Nov 2022 01:41:02 PM CST.
第一次没有安装成功,报错了
All matches were filtered out by modular filtering for argument: mysql-community-server
Error: Unable to find a match: mysql-community-server
检测发现MySQL源已经都安装成功
[root@tvtrt0fin18s1gj1 software]# yum repolist enabled | grep "mysql.*.community.*"
mysql-connectors-community MySQL Connectors Community
mysql-tools-community MySQL Tools Community
mysql80-community MySQL 8.0 Community Server
禁用MySQL模块
[root@tvtrt0fin18s1gj1 software]# yum module disable mysql
Last metadata expiration check: 0:02:16 ago on Thu 03 Nov 2022 01:41:02 PM CST.
Dependencies resolved.
===============================================================================================================================================================================================
Package Architecture Version Repository Size
===============================================================================================================================================================================================
Disabling modules:
mysql
Transaction Summary
===============================================================================================================================================================================================
Is this ok [y/N]: y
Complete!
重新安装就通过了
[root@tvtrt0fin18s1gj1 software]# yum install mysql-community-server
Last metadata expiration check: 0:02:28 ago on Thu 03 Nov 2022 01:41:02 PM CST.
Dependencies resolved.
安装成功之后就可以用systemctl操作mysqld了。
4、初次登录mysql
可以在日志中查找初始密码
[root@tvtrt0fin18s1gj1 mysql]# grep 'password' /var/log/mysqld.log
2022-11-03T05:48:43.444480Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: ycaBhDo5Tn,u
[root@tvtrt0fin18s1gj1 mysql]#
也可以修改/etc/my.cnf跳过验证以无密码登录
vim /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
skip-grant-tables # 用以跳过密码验证
5、修改密码
mysql> use mysql;
mysql> alter user 'root'@'localhost' identified by '123456';
## 这里报错了,需要把my.cnf里的skip-grant-tables去掉
ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
mysql> alter user 'root'@'localhost' identified by '123456';
Query OK, 0 rows affected (0.00 sec)
mysql>
6、创建新用户
一般不把root对外开放,就需要创建一个新用户用于外部访问mysql
## 选择mysql数据库
mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
## 创建新用户mytest,密码123456(%表示允许外部访问,localhost就是只能本机访问)
mysql> create user 'mytest'@'%' identified by '123456';
Query OK, 0 rows affected (0.01 sec)
## 可以看到刚刚创建的用户
mysql> select host,user from mysql.user;
+-----------+------------------+
| host | user |
+-----------+------------------+
| % | mytest |
| localhost | mysql.infoschema |
| localhost | mysql.session |
| localhost | mysql.sys |
| localhost | root |
+-----------+------------------+
5 rows in set (0.00 sec)
## 给mytest用户赋予权限
mysql> grant all privileges on *.* to 'mytest'@'%';
Query OK, 0 rows affected (0.01 sec)
# 在不重启mysql的情况下让刚才的操作生效
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
mysql>
六、使用uWsgi部署django项目
1、安装uWsgi
pip install uwsgi
2、创建uWsgi配置文件
[uwsgi]
# http or socket: http用于uwsgi独自运行,socket用于作为nginx的后端
http=:6789
# socket=:6789
# project dir, same with manage.py
chdir=/django工作目录
# is launch the master process to manage other processes
master=true
# load a WSGI module, here is the wsgi.py
# usually is the: <django project>/<project name>/wsgi.py
# but if chdir has appointed the project absolute dir, then only need <project name>/wsgi.py
module=django_app.wsgi:application
# process number, same with CPU's
processes=4
# 指定工作进程中的线程数
threads=2
# wsgi文件
wsgi-file=/django工作目录/wsgi.py
log-maxsize=102400
# buffer-size用于uwsgi包解析的内部缓存区大小,默认是4k。这个值可以设置到64k用于解决报头过大的错误
buffer-size=65535
#max requests per process
max_requests=5000
# log file
daemonize=/django工作目录/uwsgi_config/uwsgi.log
vacuum=true
profiler=true
memory-report=true
# 由于GIL的存在,uwsgi默认不支持多线程,不对GIL进行初始化。但如果希望程序中的线程发挥作用,需要加入
enable-threads=true
logdate=true
limit-as=6048
# python解释器路径
pythonpath=/usr/local/bin/python3.10
# 进程id文件
pidfile=/django工作目录/uwsgi_config/uwsgi.pid
# 这个配置会使在平滑地重启工作子进程中,如果工作进程结束时间超过了 8秒就会被强行结束(忽略之前已经接收到的请求而直接结束)
reload-mercy=8
3、把uWsgi制作成systemctl服务
[Unit]
Description=uwsgi
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/bin/uwsgi --ini /django工作目录/uwsgi_config/uwsgi.ini
ExecReload=/usr/local/bin/uwsgi --reload /django工作目录/uwsgi_config/uwsgi.pid
ExecStop=/usr/local/bin/uwsgi --stop /django工作目录/uwsgi_config/uwsgi.pid
[Install]
WantedBy=multi-user.target
七、安装nginx并部署nginx+uwsgi
1、安装nginx
官网方法:
创建 /etc/yum.repos.d/nginx.repo文件,内容:
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
然后执行:yum install nginx
安装之后的nginx自动配置了systemctl服务。
2、配置uwsgi服务
在上一章的uwsgi配置中,把http=:6789改成socket=:6789
3、新增配置文件/etc/nginx/conf.d/django.conf
server {
listen 8890;
server_name mytest.com www.mytest.com;
#access_log /var/log/nginx/host.access.log main;
location / {
include uwsgi_params;
uwsgi_pass 127.0.0.1:6789; # uwsgi的端口
client_max_body_size 35m;
}
}
重启uwsgi服务和nginx服务。
4、开启负载均衡
首先拷贝uwsgi.ini得到uwsgi2.ini文件
cp uwsgi.ini uwsgi2.ini
然后将端口、pid文件、log文件都改一下,然后再启一个uwsgi服务
uwsgi -d --ini uwsgi2.ini
在/etc/nginx/nginx.conf中增加如下配置:
upstream djangoapp {
server 127.0.0.1:6789; # uwsgi服务1
server 127.0.0.1:6790; # uwsgi服务2
}
对上一节的django.conf做如下修改:
server {
listen 8890;
server_name mytest.com www.mytest.com;
#access_log /var/log/nginx/host.access.log main;
location / {
include uwsgi_params;
# 不再pass到6789端口,而是pass到djangoapp变量
#uwsgi_pass 127.0.0.1:6789;
uwsgi_pass djangoapp;
client_max_body_size 35m;
}
}
重启nginx服务
八、nginx部署vue
1、vue打包
// vue.config.js
module.exports = {
// 公共路径
publicPath: '/admin/',
...
}
// router/index.js
export default new Router({
mode: 'history',
base: '/admin/', // ngnix 访问路径
scrollBehavior: () => ({ y: 0 }),
routes: constantRouterMap
})
通过nginx默认配置文件找到html目录:
将dist重命名为admin后拷贝到 /usr/share/nginx/html下
2、配置nginx
其实还是扩展上一章的nginx配置
server {
listen 8890;
server_name mytest.com www.mytest.com;
#access_log /var/log/nginx/host.access.log main;
location /api {
include uwsgi_params;
#uwsgi_pass 127.0.0.1:6789;
uwsgi_pass djangoapp;
client_max_body_size 35m;
}
location ^~ /admin {
root /usr/share/nginx/html;
try_files $uri $uri/ /admin/index.html;
}
}
重启nginx。
这样我们的一整套linux下nginx+vue+uwsgi+django+mysql+redis的python web项目运行环境就搭建完毕了。