Python3 - Centos新服务器配置

Posted 韩俊强

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python3 - Centos新服务器配置相关的知识,希望对你有一定的参考价值。

文章目录

当项目开发完毕时, 需要部署到云服务器上, 通常Python项目会优先选择Linux服务器, 其便捷性和性能高度被认可, 那么使用Linux大多会选择相关发行版, 这里以Centos为例, 部署项目所做的一些准备工作, 包括项目虚拟环境隔离, 权限配置, 数据库, 缓存等准备工作.

0.测试服务器

查看centos版本, 以Centos 7.9 为例

cat /etc/redhat-release
CentOS Linux release 7.9.2009 (Core)

查看cpu信息:
cat /proc/cpuinfo

cpu个数:
cat /proc/cpuinfo | grep processor -c

服务器内存:
 free -k (kb)
 free -m (M)
 free -g (GB)
 free -m 空闲内存
               total        used        free      shared  buff/cache   available
Mem:            941         477          68           1         395         283
Swap:             0           0           0
vim ~/.vimrc

set nu
set ts=5
set expandtab
set autoindent
set nohls
set ruler
syntax on

1.添加一个用户

新增用户(用户名harry为例):
adduser harry

如何删除用户:
userdel 用户名

修改密码:
passwd harry

添加到sudo用户组:
gpasswd -a harry wheel

切换到harry用户:
sudo -iu harry

注意: 下方所有命令, 都是切换到harry这个用户进行的, 所以很多都带上了sudo

2.初始化环境

初始化仓库

sudo yum -y upgrade  只升级所有包,不升级软件和系统内核
sudo yum install epel-release

安装gcc和nginx:

sudo yum install gcc nginx

3.安装mysql

wget http://88995.test.upcdn.net/mysql-5.7.32-1.el7.x86_64.rpm-bundle.tar
以下命令sudo开头, 因为操作的是root权限:

MySQL安装:
        ~ rpm - redhat package manager
            tar -xvf mysql-5.7.32-1.el7.x86_64.rpm-bundle.tar  -C mysql
            rpm -ivh mysql-community-common-5.7.32-1.el7.x86_64.rpm
            yum list installed | grem maria  如果有先移除,不然依赖库冲突 : 
                yum erase -y mariadb-libs
                yum erase -y mariadb-libs-1:5.5.68-1.el7.x86_64
            rpm -ivh mysql-community-common-5.7.32-1.el7.x86_64.rpm
            rpm -ivh mysql-community-libs-5.7.32-1.el7.x86_64.rpm
            rpm -ivh mysql-community-libs-compat-5.7.32-1.el7.x86_64.rpm 库文件兼容包
            rpm -ivh mysql-community-devel-5.7.32-1.el7.x86_64.rpm 开发包
            rpm -ivh mysql-community-client-5.7.32-1.el7.x86_64.rpm 客户包
            yum install -y libaio libbaio-devel
            rpm -ivh mysql-community-server-5.7.32-1.el7.x86_64.rpm 服务包

    启动MySQL:systemctl start mysqld
    查看安装时随机生成的密码: cat /var/log/mysqld.log 
    
    修改密码:
        连接MySQL:mysql -u root -proot 系统生成的密码
        ALTER USER 'root'@'localhost' IDENTIFIED BY '新密码';
    远程连接数据库:mysql -uroot -p -h192.168.137.10 -P3306

4.安装Python3.9

以下命令sudo开头, 因为操作的是root权限:

安装Python3.9
    ~ 下载:wget https://www.python.org/ftp/python/3.9.0/Python-3.9.0.tar.xz
    ~ 认证:md5sum Python-3.9.0.tar.xz   打印:6ebfe157f6e88d9eabfbaf3fa92129f6
    ~ 解压缩:xz -d Python-3.9.0.tar.xz ---> Python-3.9.0.tar
        -d - decompress 解压
        -z - zip 压缩
    ~ 解归档:tar -xvf Python-3.9.0.tarz
    ~ 或许用到:yum -y groupinstall "Development tools"
    ~ 补充底层依赖项 yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel libdb4-devel libpcap-devel xz-devel libffi-devel
        其中CentOS.8.x有两个依赖项招不到,因为默认有替代项,可以忽略
    ~ 将权限赋给用户 chown -R harry /usr/local/  
    ~ 配置:./configure --prefix=/usr/local/python3
    [make clean]
    
    ~ 构建和安装: sudo make && make install (大约需要跑20分钟)

安装完成提示:
     WARNING: The script easy_install-3.9 is installed in '/usr/local/python3/bin' which is not on PATH.
  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
  WARNING: The scripts pip3 and pip3.9 are installed in '/usr/local/python3/bin' which is not on PATH.
  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
大概意思是: /usr/local/python3/bin 目录,没有放在PATH变量底下;

    ~ 配置环境变量:
        /etc/bashrc      ---> 系统环境变量
        ~/.bash_profile  ---> 用户环境变量
        export PATH=$PATH:/usr/local/python3/bin
    vim: visual editor modefied
        vim .bash_profile
        a 编辑
        PATH=$PATH:$HOME/bin:/usr/local/python3/bin
        zz(大写)保存退出s
        
     起别名: vim ~/.bash_profile
     		PATH=$PATH:$HOME/bin:/usr/local/python3/bin
				export PATH=$PATH:/usr/local/python3/bin
				alias python=python3.9
				alias pip=pip3.9
				
		 ~ 创建符号链接(软连接):因为有上面的 alias , 所以无需再设置软连接
    		ln -s /usr/local/python3/bin/python3 /usr/bin/python3
        ln -s /usr/local/python3/bin/pip3 /usr/local/bin/pip3

pip切换镜像

mkdir ~/.pip
cd .pip 
vim pip.conf
[glbal]
index-url=http://pypi.douban.com/simple

[install]
trusted-host=pypi.douban.com

5.安装Node.js/tldr 帮助工具

5.1yum 安装

yum imstall -y nodejs 出现错误时:

Loaded plugins: fastestmirror
No such command: imstall. Please use /bin/yum --help
chown -R harry /etc/yum/pluginconf.d/fastestmirror.conf
vim  /etc/yum/pluginconf.d/fastestmirror.conf

修改 enable=1enable=0

5.2官网

官网下载
wget https://nodejs.org/dist/v16.14.0/node-v16.14.0-linux-x64.tar.xz
tar -xvf node-v16.14.0-linux-x64.tar.xz 
mv node-v16.14.0-linux-x64 /usr/local/nodejs

全局配置
vim ~/.bash_profile
export PATH=$PATH:/usr/local/nodejs/bin
source ~/.bash_profile

验证一下
node --version
npm --version

设置镜像
npm config set registry http://registry.npmmirror.com/  
npm config get registry 查看npm下载源
安装tldr帮助工具 
    ~ yum install -y tldr
    ~ tldr grep
    ~ tldr who
    
升级npm有可能用到
    chown -R harry /usr/lib/node_modules/

6.创建python虚拟环境

pip install virtualenv
pip install virtualenvwrapper

vim .bash_profile

export VIRTUALENVWRAPPER_PYTHON=/usr/local/python3/bin/python3.9
export WORKON_HOME=$HOME/.virtualenvs #指定虚拟环境存放目录
export VIRTUALENVWRAPPER_VIRTUALENV=~/.local/bin/virtualenv
source /home/harry/.local/bin/virtualenvwrapper.sh

cd 到项目, 创建虚拟环境

mkvirtualenv blogprojectenv

或者
virtualenv --python=/usr/local/python3/bin/python3.9  blogprojectenv

虚拟环境路径

/home/harry/.virtualenvs/blogprojectenvc

常用命令:

###  5.列出所有虚拟环境

```
lsvirtualenv -b
```

### 6.启动/切换虚拟环境

workon [虚拟环境名称]

```
workon env1
```

###  7.进入当前环境的目录:cdvirtualenv 

```
在环境里执行:cdvirtualenv
```

### 8.删除虚拟环境

rmvirtualenv [虚拟环境名称]

```
rmvirtualenv env1
```

### 9.cpvirtualenv复制环境

```
复制env1到env2
cpvirtualenv env1 env2
```

### 10.lssitepackages

列出当前环境中`site-packages`内容  (在环境中执行)

### 11.cdsitepackages 

清除环境内所有第三方包

7.同步代码及项目依赖

7.1安装uwsgi

yum install -y uwsgi-plugin-python36
pip3 install uwsgi

查看uwsgi的python版本
uwsgi --python-version

8.同步数据库

云端先初始化数据库, 然后用navicat同步表结构和数据

mysql -uroot -p

use mysql;

select host from user where user='root';

update user set host = '%' where user ='root';

flush privileges;

https://www.cnblogs.com/companionspace/p/10316083.html

9.部署

sudo nginx -s stop 快速关闭
							quit 优雅的关闭
							reload 重新加载配置
							
Nginx 不间断重启:
		kill -HUP [进程ID]
Nginx 配置文件
指令:
	nginx -t 不运行, 仅测试配置文件
	nginx -c configpath 从指定路径加载启动配置文件
	nginx -t -c configpath 测试指定配置文件

检查uwsgi 和 nginx 端口号一定要一致, 否则不成功!

顺序: 必须先nginx, 后uwsgi
sudo nohup nginx -c /home/harry/myproject/blogproject/nginx.conf &
nohup uwsgi --ini /home/harry/myproject/blogproject/uwsgi.ini &



强烈建议:
查询:  
			ps -aux | grep nginx
	 	  ps -aux | grep uwsgi
	 	  
	 	  或者端口号查询:
	 	  lsof -i tcp:80

停止nginx: 
			sudo nginx -s stop
			

uwsgi --ini uwsgi.ini             # 启动
uwsgi --reload uwsgi.pid          # 重启
uwsgi --stop uwsgi.pid            # 关闭

9.redis缓存的安装使用

本地使用的话: pip install redis
pip install flask-caching  项目使用这个缓存, 搭配redis使用
centos7 安装Redis服务
    ~ 下载:wget https://download.redis.io/releases/redis-5.0.14.tar.gz
    ~ 解缩:gunzip redis-5.0.14.tar.gz ---> redis-5.0.14.tar (归档文件)
    ~ 压缩:gzip redis-5.0.14.tar ---> redis-5.0.14.tar.gz
    ~ 解归档:tar -zxvf redis-5.0.14.tar  解压缩解归档
        -x - eXtract - 抽取(解归档)
        -v - verbose - 显示详细过程
        -f - file - 指定待解归档的文件的名字
    说明:上面两个操作(解压缩和解归档)如果想一步到位,可以使用下面的命令
        tar -xvf redis-5.0.14.tar.gz
     cd redis
     make && make install
服务器: 启动
redis-server

本地启动:redis-cli
远程启动:redis-cli -h host -p port -a password
systemctl start redis.service    # 启动Redis
systemctl restart redis.service    # 重启Redis
systemctl stop redis.service    # 结束Redis
systemctl enable redis.service    # 设置Redis开机自启

绑定内网地址:(可选)

1.查询内网的IP地址
ifconfig eth0  

2.把redis服务绑定在阿里云内网IP地址上,然后在后台启动(不加连接密码)
redis-server --bind 172.26.181.xx > redis.log &   

3.把redis服务绑定里云内网IP地址上,然后在后台启动(加连接密码)
redis-server --bind 172.26.181.xx --requirepass 1qaz2wsx > redis.log &  

查看所有配置

redis-cli
config get *
config get 键 获取值
config set 键 值 重新配置
redis.conf文件中bind 127.0.0.1没有注释掉,所以在外部访问时候会连接不上

关于修改密码
在redis.conf 找到这一行:# requirepass foobared(vi下按/requirepass foobared搜索)
将注释符号去掉,将后面修改成自己的密码,如:requirepass 123456
如果已运行,重启redis。我是先结束进程,然后再redis-server /usr/local/redis/redis.conf(我是编译安装的,所以我的配置文件是这一个)

常见错误: 
redis.exceptions.ResponseError: DENIED Redis is running in protected mode because protected mode is enabled, no bind address was specified, no authentication password is requested to clients. In this mode connections are only accepted from the loopback interface. If you want to connect from external computers to Redis you may adopt one of the following solutions: 1) Just disable protected mode sending the command 'CONFIG SET protected-mode no' from the loopback interface by connecting to Redis from the same host the server is running, however MAKE SURE Redis is not publicly accessible from internet if you do so. Use CONFIG REWRITE to make this change permanent. 2) Alternatively you can just disable the protected mode by editing the Redis configuration file, and setting the protected mode option to 'no', and then restarting the server. 3) If you started the server manually just for testing, restart it with the '--protected-mode no' option. 4) Setup a bind address or an authentication password. NOTE: You only need to do one of the above things in order for the server to start accepting connections from the outside.

解决方法:设置protected-mode no
redis-cli
config get protected-mode
set protected-mode no
quit 退出

运行正常后, 常用的命令:

keys *   查看所有keys
del bane 删除键值对
select 3  redis 默认有16个数据库, 选择进入哪个数据库, 默认是select 0

客户端代码:

1. redis安装:
pip install redis
pip install flask-caching

2. redis项目配置:

exts
-----__init__.py ----> cache = Cache()  from flask_caching import Cache

apps
|---- __init__.py
			config = 
    		 'CACHE_TYPE': 'redis',
    		 'CACHE_REDIS_HOST': '39.101.244.154',
     		 'CACHE_REDIS_PORT': 6379
		   	
		   def create_app():
		   		...
		   		cache.init_app(app=app, config=config)
		   		
3. redis的使用:
    1.缓存键值对
        设置:
        cache.set(key, value, timeout=sencond)
        cache.set_many([(key, value),(key, value), (key, value), ...])
        获取:
        cache.get(key)
        cache.get_many(key1, key2, key3, ...)
        删除:
        cache.delete(key)
        cache_delete_many(key1, key2, key3, ...)
        cache.clear  清空缓存

    2.视图函数的缓存
        @user_bp1.route('/')
        @cache.cached(timeout=50) # 过期时间 秒
        def index():
             pass
             ....
ValueError: urls must start with a leading slash
url必须以斜杠/开头, 查看请求路径
(blogprojectenv) [harry@iZ8vbbip4bq97z0o60rq3uZ blogproject]$ uwsgi --ini uwsgi.ini 
[uWSGI] getting INI configuration from uwsgi.ini
*** Starting uWSGI 2.0.20 (64bit) on [Sat Mar 12 17:19:01 2022] ***
compiled with version: 4.8.5 20150623 (Red Hat 4.8.5-44) on 08 March 2022 06:43:28
os: Linux-3.10.0-1160.53.1.el7.x86_64 #1 SMP Fri Jan 14 13:59:45 UTC 2022
nodename: iZ8vbbip4bq97z0o60rq3uZ
machine: x86_64
clock source: unix
pcre jit disabled
detected number of CPU cores: 1
current working directory: /home/harry/myproject/blogproject
writing pidfile to uwsgi.pid
detected binary path: /home/harry/.virtualenvs/blogprojectenv/bin/uwsgi
chdir() to /home/harry/myproject/blogproject
your processes number limit is 3683
your memory page size is 4096 bytes
detected max file descriptor number: 65535
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
uwsgi socket 0 bound to TCP address 0.0.0.0:8000 fd 3
Python version: 3.9.0 (default, Mar  8 2022, 10:49:28)  [GCC 4.8.5 20150623 (Red Hat 4.8.5-44)]
Python main interpreter initialized at 0x17e62d0
python threads support enabled
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 3907070 bytes (3815 KB) for 40 cores
*** Operational MODE: preforking+threaded ***
.
WSGI app 0 (mountpoint='') ready in 0 seconds on interpreter 0x17e62d0 pid: 31433 (default app)
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI master process (pid: 31433)
spawned uWSGI worker 1 (pid: 31434, cores: 10)
spawned uWSGI worker 2 (pid: 31435, cores: 10)
spawned uWSGI worker 3 (pid: 31436, cores: 10)
spawned uWSGI worker 4 (pid: 31437, cores: 10)

10.supervisor的安装与使用

管理进程

sudo yum install supervisor
systemctl start supervisord.service     //启动supervisor并加载默认配置文件
systemctl enable supervisord.service    //将supervisor加入开机启动项

常用命令

supervisorctl status        //查看所有进程的状态
supervisorctl stop es       //停止es
supervisorctl start es      //启动es
supervisorctl restart       //重启es
supervisorctl update        //配置文件修改后使用该命令加载新的配置
supervisorctl reload        //重新启动配置中的所有程序

以上是关于Python3 - Centos新服务器配置的主要内容,如果未能解决你的问题,请参考以下文章

vpsCentos 7安装python3.8.5

CentOS 8 新特性

centos7+nginx+python3+django+uwsgi配置

CentOS7+Python3.6利用web.py库进行微信公众平台服务器简易配置,token验证

centos6.5升级python3并配置pip

阿里云服务器ECS(Centos8)下安装和配置python3.8