项目搭建

python虚拟环境安装

安装python环境

python安装参见博客: https://www.cnblogs.com/tiger666/articles/10312522.html

安装虚拟环境

python虚拟环境参见博客: https://www.cnblogs.com/tiger666/articles/10312546.html

安装项目环境

(1) 创建并进入虚拟环境

1 mkvirtualenv backup_platform_env
(2) 上传项目到/opt/目录下, 并进入/opt/目录
1 cd /opt/backup_platform/webadmins

(3) 安装requirements.txt

1 pip3 install -i https://pypi.douban.com/simple -r requirements.txt

Mysql安装

安装mariadb

mysql安装详见博客: https://www.cnblogs.com/tiger666/articles/10259269.html

创建数据库
1 create database backup_platform
导入数据库
use backup_platform
source /opt/backup_platform/

Django环境安装

安装django所依赖的系统的RPM包
1 yum -y groupinstall "Development Tools"
2 yum -y install python-devel sqlite-devel libffi-devel zlib zlib-devel openssl openssl-devel
3 pip3 install pysqlite
第一个django项目
django安装

方式一:

1 # pip方式安装
2 pip3 install django==1.9.13

方式二:

1 # 源码方式安装
2 # 1 下载源码包
3 # 2 解压源码包
4 tar -zxvf Django-1.9.13.tar.gz
5 # 3  安装
6 cd Django-1.9.13
7 python setup.py  instal
创建web项目
1 django-admin startproject web
创建app
1 django-admin startapp blog
项目目录结构及其说明
 1 ├── blog              # APP名
 2 │   ├── admin.py
 3 │   ├── apps.py
 4 │   ├── __init__.py
 5 │   ├── migrations
 6 │   │   └── __init__.py
 7 │   ├── models.py     # 定义数据库模型, 与数据库进⾏交互
 8 │   ├── tests.py
 9 │   └── views.py      # 后台服务端代码, 定义视图函数与url形成映射关系
10 ├── djclass           # 项⽬名
11 │   ├── __init__.py
12 │   ├── settings.py   # 项⽬的配置⽂件
13 │   ├── urls.py       # 定义URL与Python代码的映射关系, 映射关系称为⽹站路由
14 │   └── wsgi.py       # ⼀个web服务器兼容的⼊⼝以便运⾏这个项⽬
15 └── manage.py         # 与项⽬进⾏交互的命令⼯具
项目目录结构及其说明
启动项目
修改配置文件

django配置⽂件在项⽬⽬录中的settings.py⽂件中定义

修改 settings.py中的配置 ALLOWED_HOSTS = [] –> ALLOWED_HOSTS = [‘*’]

启动项目
1 python manage.py runserver 0.0.0.0:8600
验证

1565748420330

django配置文件详解

此处省略三千字……..

将Django项目跑起来

修改配置文件
1 vim /opt/backup_platform/webadmins/config/config.cfg
2 将里面的所有IP改为本机服务器的IP
mysql授权
1 MariaDB [(none)]> grant all privileges on backup_platform.* to root@"%" identified by "redhat";
2 MariaDB [(none)]> flush privileges;
启动项目
1 cd /opt/backup_platform/webadmins
2 python manage.py runserver 0.0.0.0:8000
3 效果: 能跑起来即可

安装uwsgi

安装uwsgi
1 pip3 install uwsgi
uwsgi的配置文件
 1  1 [uwsgi]
 2  2 http = 0.0.0.0:8600    # uwsgi的启动端口
 3  3 uid = root  # 启动用户
 4  4 gid = root  # 启动所属组
 5  5 vacuum=false  # 当服务停止的时候, 是否自动移除socket和pid文件 
 6  6 chdir = /opt/backup_platform/webadmins  # 项目的根目录
 7  7 home = /root/Envs/backup_platform_env  # 虚拟环境路径
 8  8 wsgi-file = webadmins/wsgi.py  # wsgi文件位置
 9  9 processes = 1  # 进程数,根据CPU核数来决定
10 10 threads = 2  # 线程数
11 11 socket=/opt/backup_platform/webadmins/uwsgi.sock  # 指定Socket路径,用来提供进程通信
12 12 chmod-socket=666  # socket权限设置
13 13 daemonize=/opt/backup_platform/webadmins/log/uwsgi.log  # 设置日志目录
uwsgi的配置文件

简洁配置

 1 [uwsgi]
 2 http = 0.0.0.0:8600
 3 uid = root
 4 gid = root
 5 vacuum=false
 6 chdir = /opt/backup_platform/webadmins
 7 home = /root/Envs/backup_platform_env
 8 wsgi-file = webadmins/wsgi.py
 9 processes = 1
10 threads = 2
11 socket=/opt/backup_platform/webadmins/uwsgi.sock
12 chmod-socket=666
13 daemonize=/opt/backup_platform/webadmins/log/uwsgi.log
启动uwsgi
1 (backup_platform_env) [root@localhost config]# pwd
2 /opt/backup_platform/webadmins/config
3 (backup_platform_env) [root@localhost config]# uwsgi uwsgi.ini 
4 [uWSGI] getting INI configuration from uwsgi.ini

安装Redis

下载redis源码
1 wget http://download.redis.io/releases/redis-5.0.0.tar.gz
解压缩
1 tar -zxvf redis-5.0.0.tar.gz
编译安装
1 cd redis-5.0.0
2 make && make install
创建redis配置文件
1 mkdir -p /opt/redis_conf
2 touch /opt/redis_conf/redis-6379.conf
3 mkdir -p /data/6379  # 创建redis的日志与pid文件目录

打开/opt/redis_config/redis-6379.conf, 添加以下内容:

1 port 6379
2 daemonize yes
3 pidfile /data/6379/redis.pid
4 loglevel notice
5 logfile "/data/6379/redis.log"
6 dir /data/6379
7 protected-mode no
启动redis
1 redis-server /opt/redis_conf/redis-6379.conf

Centos7源码安装Nginx

准备工作
1 # 安装编译工具
2 yum -y install gcc automake autoconf libtool make gcc-c++
3 yum -y groupinstall "Development tools
安装pcre
1 [root@tigerlee nginx]# tar -zxvf pcre-8.36.tar.gz -C /opt/tools/
2 [root@tigerlee nginx]# cd /opt/tools/pcre-8.36/
3 [root@tigerlee pcre-8.36]# ./configure
4 [root@tigerlee pcre-8.36]# make && make instal
安装zlib
1 [root@tigerlee nginx]# tar -zxvf zlib-1.2.8.tar.gz -C /opt/tools/
2 [root@tigerlee nginx]# cd /opt/tools/zlib-1.2.8/
3 [root@tigerlee zlib-1.2.8]# ./configure
4 [root@tigerlee zlib-1.2.8]# make && make install
安装openssl

1 [root@tigerlee nginx]# tar -zxvf openssl-1.0.1j.tar.gz -C /opt/tools/
2 
3 (backup_platform_env) [root@localhost nginx]# mv openssl-1.0.1j /opt/tools/
4 (backup_platform_env) [root@localhost nginx]# mv zlib-1.2.8 /opt/tools/
5 (backup_platform_env) [root@localhost nginx]# mv pcre-8.36 /opt/tools/
安装nginx

(1) 下载源码包

1 wget -c https://nginx.org/download/nginx-1.16.0.tar.gz

(2) 解压缩源码

1 tar -zxvf nginx-1.16.0.tar.gz
2 cd nginx-1.16.0

(3) 配置,编译安装

1 [root@tigerlee nginx-1.16.1]# ./configure --prefix=/opt/nginx116 --with-http_ssl_module --with-pcre=/opt/tools/pcre-8.36 --with-zlib=/opt/tools/zlib-1.2.8 --with-openssl=/opt/tools/openssl-1.0.1j

1565751753808

(4) 安装

1 make && make install

(5) 查看nginx安装文件夹

1565752460428

(6) 启动nginx并验证

1 # 启动nginx,进入sbin目录,找到nginx启动命令
2 cd /opt/nginx116/sbin
3 ./nginx #启动
4 ./nginx -s stop #关闭
5 ./nginx -s reload # 平滑重启 ,修改了nginx.conf之后,可以不重启服务,加载新的配置

1565752189327

配置nginx支持uwsgi

修改nginx.conf配置文件, 将location代码块更改为:

server {
    listen 80;
    server_name  10.0.0.9;
    location / {
        include uwsgi_params;
        uwsgi_pass unix:/opt/backup_platform/webadmins/uwsgi.sock;
    }
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /usr/share/nginx/html;
    }
}

使用脚本自动启动nginx uwsgi redis服务

修改项目下config/config.cfg配置文件
 1 [base]
 2 address=10.0.0.5  # 服务器运行的IP
 3 proj_home=/opt/backup_platform/webadmins  # 项目所在目录
 4 python_home=/root/Envs/backup_platform_env  # 虚拟环境所在目录
 5 
 6 [db]
 7 host=10.0.0.5
 8 port=3306
 9 user=root
10 password=redhat
11 database=backup_platform
12 
13 [redis]
14 home=/usr  # redis-server运行目录
15 config=/opt/redis_conf/redis-6379.conf  # 配置文件目录
16 
17 [nginx]
18 home=/opt/nginx116  # nginx安装目录
19 
20 [uwsgi]
21 address=10.0.0.5
22 port=8600
23 
24 [backup-agent]
25 rsync=rsync-3.1.3
26 sersync=sersync2.5.4_64bit_binary_stable_final
27 xtrabackup=percona-xtrabackup-2.4.9-Linux-x86_64
28 
29 [localhost]
30 address=10.0.0.5
配置redis软链接
# 创建软链接是由于run.py脚本启动redis需要使用/usr/local/bin目录
which redis-server
/usr/local/bin/redis-server
ln -s /usr/local/bin/redis-server /usr/bin

使用run.py脚本启动uwsgi、nginx、redis服务
需要先关掉redis、uwsgi服务。
1 cd /opt/backup_platform/webadmins/
2 python run.py  # 查看帮助信息
3 python run.py start/stop/status
谢谢大家
 
原文地址:https://www.cnblogs.com/wpnr/p/12158275.html