CentOS7初始化配置Python3+Django+Nginx+uwsgi+celery+MySQL+Redis

Linux初始化配置Python3+Django+Nginx+uwsgi+celery+MySQL+Redis—阿里云CentOS7

  • Linux初始化配置Python3+Django+Nginx+uwsgi+celery+MySQL+Redis—阿里云CentOS7
    • 0.修改主机名
    • 1. 安装Python3
    • 2. 安装MySQL
    • 3. 安装redis
    • 4. 配置git
    • 5. 安装相关的python包和celery
    • 6. 运行Django项目
      • 6.1 配置setting.py文件
      • 6.2 数据库迁移
      • 6.3 启动项目(celery和Django)
    • 7. 安装Nginx + uwsgi
      • 7.1 安装Nginx
      • 7.2 安装uwsgi
      • 7.3 配置uwsgi
      • 7.4 配置nginx
    • 8. Nginx+uwsgi启动项目
    • 9. 解决静态资源访问404
    • 10. 开机启动项目
    • 11. 关闭服务

Linux初始化配置Python3+Django+Nginx+uwsgi+celery+MySQL+Redis—阿里云CentOS7

0.修改主机名

  1. 暂时修改主机名:
    hostnamectl set-hostname 主机名
  2. 永久修改主机名:
    vim /etc/hostname   reboot即可

1. 安装Python3

# 创建文件夹保存下载的压缩文件
mkdir download
cd download

# 下载Python的tar包,此处使用淘宝镜像,因为官网下载太慢了
wget http://npm.taobao.org/mirrors/python/3.7.5/Python-3.7.5.tgz

# 编译安装依赖包
yum install gcc patch libffi-devel python-devel zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel -y
yum -y install gcc automake autoconf libtool make
yum install gcc gcc-c++

# 解压Python3压缩文件并进入解压目录
tar -xvf Python-3.7.5.tgz 
cd Python-3.7.5

# 创建文件夹,用于安装python3
mkdir /usr/local/python3.7.5

# 配置安装目录
./configure --prefix=/usr/local/python3.7.5

# 编译和安装
make && make install

# 查看原Python2的配置
[root@banana Python-3.7.5]# ll /usr/bin/ | grep python
lrwxrwxrwx  1 root root         7 Jun  9 14:13 python -> python2
lrwxrwxrwx  1 root root         9 Jun  9 14:13 python2 -> python2.7
-rwxr-xr-x  1 root root      7144 Apr  2 21:17 python2.7
-rwxr-xr-x  1 root root      1835 Apr  2 21:17 python2.7-config
lrwxrwxrwx  1 root root        16 Jun  9 14:13 python2-config -> python2.7-config
lrwxrwxrwx  1 root root        14 Jun  9 14:13 python-config -> python2-config

# 删除Python2的配置
rm /usr/bin/python
rm /usr/bin/python-config 

注意:删除的时候会有安全提示问你是否删除,输入y,回车就好。


# 配置改为Python3,并配置pip3
ln -s /usr/local/python3.7.5/bin/python3 /usr/bin/python3
ln -s /usr/bin/python3  /usr/bin/python
ln -s /usr/local/python3.7.5/bin/python3-config /usr/bin/python3-config
ln -s /usr/bin/python3-config /usr/bin/python-config
ln -s /usr/local/python3.7.5/bin/pip3 /usr/bin/pip3

# 查看配置
[root@banana ~]# ll /usr/bin/ | grep python
lrwxrwxrwx    1 root root         31 May 31 16:27 pip3 -> /usr/local/python3.7.5/bin/pip3
lrwxrwxrwx    1 root root         34 May 31 16:55 python -> /usr/local/python3.7.5/bin/python3
lrwxrwxrwx    1 root root          9 May 31 16:17 python2 -> python2.7
-rwxr-xr-x    1 root root       7144 Apr  2 21:17 python2.7
-rwxr-xr-x    1 root root       1835 Apr  2 21:17 python2.7-config
lrwxrwxrwx    1 root root         16 May 31 16:17 python2-config -> python2.7-config
lrwxrwxrwx    1 root root         34 May  9 14:58 python3 -> /usr/local/python3.7.5/bin/python3
lrwxrwxrwx    1 root root         41 May 31 17:02 python3-config -> /usr/local/python3.7.5/bin/python3-config
lrwxrwxrwx    1 root root         23 May 31 17:05 python-config -> /usr/bin/python3-config


注意:修改之后yum会不能用,因为yum采用python作为命令解释器,原来系统自带的python解释器为python2.7,所以需要修改yum配置文件。

# 修改yum的配置文件
[root@banana ~]# vim /usr/bin/yum 
#!/usr/bin/python
修改为:
#!/usr/bin/python2.7

注意:升级python2为python3之后,yum安装某些包的时候可能会报错,按如下方式修改即可。


# 报错:
  File "/usr/libexec/urlgrabber-ext-down", line 28
    except OSError, e:
                  ^
SyntaxError: invalid syntax

# 通用解决办法:
[root@banana ~]# vim /usr/libexec/urlgrabber-ext-down 
#!/usr/bin/python
修改为:
#!/usr/bin/python2.7

2. 安装MySQL

# 下载MySQL官网的Yum源rpm安装包
wget http://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm

# 安装MySQL的Yum源
yum localinstall mysql57-community-release-el7-8.noarch.rpm

# 检查是否安装成功
yum repolist enabled | grep "mysql.*-community.*"

# 安装MySQL
yum install mysql-community-server

# 启动MySQL服务
systemctl start mysqld

# 设置开机启动MySQL
systemctl enable mysqld
systemctl daemon-reload

# 找到MySQL的默认密码,"root@localhost"后面就是
grep "root@localhost" /var/log/mysqld.log 

# 登录MySQL
mysql -uroot -p
>输入刚才找到的默认密码

# 重置MySQL的密码,注意:初始密码有安全级别,需要三种还是四种以上的混合
set password for 'root'@'localhost'=password('你的密码'); 


3. 安装redis

# 下载安装包
wget http://download.redis.io/releases/redis-stable.tar.gz

# 安装gcc套装
yum install cpp binutils glibc glibc-kernheaders glibc-commom glic-devel gcc make

# 升级gcc
yum -y install centos-release-scl

yum -y install devtoolset-9-gcc devtoolset-9-gcc-c++ devtoolset-9-binutils

scl enable devtoolset-9 bash


# 解压
tar -zxvf redis-stable.tar.gz 

# 进入
cd redis-stable

# 安装
make 
make test
make install


# 创建软链接
[root@banana redis-stable]# ln -s src/redis-server /usr/bin/redis-server

# 设置配置文件,运行后台启动redis
[root@banana redis-stable]# vim redis.conf 
修改:
    daemonize no
为:
    daemonize yes
    
# 后台启动
[root@banana redis-stable]# cd /root/download/redis-stable/
[root@banana redis-stable]# redis-server ./redis.conf 
14559:C 25 Jun 2020 23:43:31.167 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
14559:C 25 Jun 2020 23:43:31.167 # Redis version=6.0.5, bits=64, commit=00000000, modified=0, pid=14559, just started
14559:C 25 Jun 2020 23:43:31.167 # Configuration loaded

# 查看
[root@banana redis-stable]# ps -aux | grep redis
root     14560  0.0  0.4 162360  7860 ?        Ssl  23:43   0:00 redis-server 127.0.0.1:6379
root     14566  0.0  0.0 112816   968 pts/0    S+   23:43   0:00 grep --color=auto redis
[root@banana redis-stable]# netstat -unltp | grep redis
tcp        0      0 127.0.0.1:6379          0.0.0.0:*               LISTEN      14560/redis-server 

4. 配置git

# 安装git
yum install git

# 配置全局项
[root@banana ~]# git config --global user.name "H-tao"
[root@banana ~]# git config --global user.email [email protected]

# 查看配置
[root@banana ~]# git config --list
user.name=H-tao
[email protected]

[root@banana ~]# cat .gitconfig 
[user]
	name = H-tao
	email = [email protected]

# 生成密钥,-C "随意"
[root@banana ~]# ssh-keygen -t rsa -C "[email protected]"
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
...
The key's randomart image is:
+--[ RSA 2048]----+
|           .+ .  |
...
|     o...        |
+-----------------+


# 查看公钥
[root@banana ~]# ls -a
.              .bash_logout   .cache    .gitconfig      .pip              .ssh
..             .bash_profile  .cshrc    github          .pydistutils.cfg  .tcshrc
.bash_history  .bashrc        download  .mysql_history  shellprogram      .viminfo
[root@banana ~]# cd .ssh/
[root@banana .ssh]# ls
authorized_keys  id_rsa  id_rsa.pub

# 得到公钥,并复制粘贴到Github->Settings->SSH->New
[root@banana .ssh]# cat id_rsa.pub 
ssh-rsa ... [email protected]

5. 安装相关的python包和celery

# 安装依赖
yum install mysql-devel gcc gcc-devel python-devel

pip3 install django==2.2.7 django-redis pymysql mysqlclient pandas itsdangerous 

# 安装celery
pip3 install celery
ln -s /usr/local/python3.7.5/bin/celery /usr/bin/celery

6. 运行Django项目

6.1 配置setting.py文件

主要是配置数据库和邮件服务器,因为阿里云屏蔽了25端口。【Django】celery收到发送邮件任务没反应(无限等待)- 阿里云ECS-CentOS7

修改lagou_website/settings.py里的数据库配置和邮件服务器配置,为发送邮件做准备。

# 数据库配置
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'HOST': 'localhost',
        'PORT': 3306,
        'USER': 'root',
        'PASSWORD': '123456',
        'NAME': 'job_recruit',
        'CHARSET': 'utf8',
    },
    'MYSQL': {
        'HOST': 'localhost',
        'PORT': 3306,
        'USER': 'root',
        'PASSWORD': '123456',
        'NAME': 'job_recruit',
        'CHARSET': 'utf8',
    }
}

# 邮件服务器配置
EMAIL_HOST = 'smtp.163.com'
EMAIL_PORT = 465
EMAIL_USE_SSL = True

6.2 数据库迁移

数据库迁移是新的Django项目必须进行的,为运行Django做准备。

[root@banana JobRecruit_website]# mysql -uroot -p
mysql> create database job_recruit character set utf8;

[root@banana JobRecruit_website]# python manage.py makemigrations

[root@banana JobRecruit_website]# python manage.py migrate

6.3 启动项目(celery和Django)

记得开启阿里云的安全组实例80端口,和配置防火墙。

# 启动celery
celery -A celery_tasks.tasks worker -l info

# 运行
python manage.py runserver 0.0.0.0:80

7. 安装Nginx + uwsgi

7.1 安装Nginx

# 安装依赖
yum install -y make cmake gcc gcc-c++ pcre pcre-devel zlib zlib-devel unzip zip telnet vim

# 进入下载目录
cd ~/download/

# 下载安装包
wget http://nginx.org/download/nginx-1.19.0.tar.gz

# 解压
tar -zxvf nginx-1.19.0.tar.gz 

# 进入解压目录
cd nginx-1.19.0

# 安装
./configure --prefix=/usr/local/nginx 

# make install 
make && make install

# 配置软链接
ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx

# 启动Nginx
/usr/local/nginx/sbin/nginx     # 或者直接输入nginx启动

# 查看进程
netstat -unltp | grep nginx

# 本地访问
curl 127.0.0.1

7.2 安装uwsgi

# 检查python3和pip3
[root@banana ~]# ll /usr/bin/ | grep python
lrwxrwxrwx    1 root root         31 May 31 16:27 pip3 -> /usr/local/python3.7.5/bin/pip3
lrwxrwxrwx    1 root root         34 May 31 16:55 python -> /usr/local/python3.7.5/bin/python3
lrwxrwxrwx    1 root root          9 May 31 16:17 python2 -> python2.7
-rwxr-xr-x    1 root root       7144 Apr  2 21:17 python2.7
-rwxr-xr-x    1 root root       1835 Apr  2 21:17 python2.7-config
lrwxrwxrwx    1 root root         16 May 31 16:17 python2-config -> python2.7-config
lrwxrwxrwx    1 root root         34 May  9 14:58 python3 -> /usr/local/python3.7.5/bin/python3
lrwxrwxrwx    1 root root         41 May 31 17:02 python3-config -> /usr/local/python3.7.5/bin/python3-config
lrwxrwxrwx    1 root root         23 May 31 17:05 python-config -> /usr/bin/python3-config

# 安装uwsgi
[root@banana ~]# pip3 install uwsgi --upgrade
Looking in indexes: http://mirrors.cloud.aliyuncs.com/pypi/simple/
Collecting uwsgi
  Downloading http://mirrors.cloud.aliyuncs.com/pypi/packages/e7/1e/3dcca007f974fe4eb369bf1b8629d5e342bb3055e2001b2e5340aaefae7a/uwsgi-2.0.18.tar.gz (801kB)
     |████████████████████████████████| 808kB 56.1MB/s 
Installing collected packages: uwsgi
  Running setup.py install for uwsgi ... done
Successfully installed uwsgi-2.0.18

# 查看是否成功安装到python3中
[root@banana JobRecruit_website]# ll /usr/local/python3.7.5/bin/uwsgi 
-rwxr-xr-x 1 root root 15200968 Jun  8 10:09 /usr/local/python3.7.5/bin/uwsgi

# 配置软链接并查看
[root@banana JobRecruit_website]# ln -s /usr/local/python3.7.5/bin/uwsgi /usr/bin/uwsgi
# 配置软链接成功
[root@banana JobRecruit_website]# ll /usr/bin/ | grep uwsgi
lrwxrwxrwx    1 root root         32 Jun  8 10:27 uwsgi -> /usr/local/python3.7.5/bin/uwsgi


[root@banana ~]# vim test.py
# 添加如下内容:
def application(env, start_response):
    start_response('200 OK', [('Content-Type','text/html')])
    return [b"Hello World!"]

# 测试uwsgi
[root@banana ~]# uwsgi --http :8001 --wsgi-file test.py 
*** Starting uWSGI 2.0.18 (64bit) on [Mon Jun  8 10:31:48 2020] ***
......

# 打开另一个终端连接:
[root@banana ~]# curl 127.0.0.1:8001
Hello World!

7.3 配置uwsgi

[root@banana JobRecruit_website]# vim myproject.xml
# 添加如下内容:

    :8090
        /root/github/Python-/JobRecruit_website
            lagou_website.wsgi
                4
    uwsgi.log




    :8090
        /root/github/Python-/JobRecruit_website
            lagou_website.wsgi
                4 
    uwsgi.log

~             

解释:
socket:端口与在nginx里配置一样
/root/github/Python-/JobRecruit_website:项目路径
lagou_website.wsgi:lagou_website/wsgi.py (lagou_website是我项目的app路径,wsgi代表wsgi.py文件)

7.4 配置nginx

[root@banana JobRecruit_website]# vim /usr/local/nginx/conf/nginx.conf
修改如下内容:

    server {
        listen       80;  # web服务监听的端口
        server_name  localhost;

        charset utf-8;

        location / {
           # root   html;
           # index  index.html index.htm;
            include /root/download/nginx-1.19.0/conf/uwsgi_params;
            uwsgi_pass 127.0.0.1:8090;
            uwsgi_read_timeout 30;
        }

8. Nginx+uwsgi启动项目

# 后台启动reids
[root@banana JobRecruit_website]# redis-server /root/download/redis-stable/redis.conf

# 后台启动celery
[root@banana JobRecruit_website]# nohup celery -A celery_tasks.handler worker -l info > celery.log 2>&1 &

# 启动nginx
[root@banana JobRecruit_website]# nginx         # /usr/local/nginx2/sbin/nginx 

# 启动uwsgi
[root@banana JobRecruit_website]# uwsgi -x myproject.xml

9. 解决静态资源访问404

  1. 配置Django的settings.py
DEBUG = False


STATIC_URL = '/static/'          # 访问静态资源的前缀
STATIC_ROOT = '/root/var/job_recruit/static/'    # 静态资源具体存放路径(nginx配置中需要用到此路径)

# 设置静态文件查找目录,在终端使用命令收集到项目的静态文件后,再去配置nginx服务器寻找静态文件的路径
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
)
  1. 使用django manage收集项目静态文件 (需要进入manage.py同级目录下使用命令)
python manage.py collectstatic
  1. 修改nginx配置文件
  • 首行use nobody 改为use root
  • 新增静态资源location配置
[root@banana]# vim /usr/local/nginx/conf/nginx.conf
# 修改如下内容:
user  root;
worker_processes  1;
...

    server {
        listen       80;
        server_name  localhost;
        
        main;

        location / {
            include /root/download/nginx-1.19.0/conf/uwsgi_params;
            uwsgi_pass 127.0.0.1:8090;
            uwsgi_read_timeout 30;
        }

        location /static {
            alias /root/var/job_recruit/static;
        }
    }
    
  1. 重启Nginx
/usr/local/nginx/sbin/nginx -s reload

10. 开机启动项目

  1. 编辑shell脚本
[root@banana ~]# vim job_recruit.sh
# 添加如下内容:
#!/bin/bash

# 进入目录
cd /root/github/Python-/JobRecruit_website/

# 后台启动reids
redis-server /root/download/redis-stable/redis.conf

# 后台启动celery
nohup celery -A celery_tasks.handler worker -l info > celery.log 2>&1 &

# 启动nginx
nginx         # /usr/local/nginx2/sbin/nginx 

# 启动uwsgi
uwsgi -x myproject.xml
  1. 执行脚本
[root@banana ~]# sh -x job_recruit.sh 

11. 关闭服务

# 关闭uwsgi
killall -9 uwsgi

你可能感兴趣的:(Python,Linux,运维,linux,mysql,django,nginx,centos)