在服务器上部署上线项目
Linux数据库处理
首先我们需要在mysql中创建bbs库,并导入数据库SQL脚本(就是原本运行在我们项目中的数据库)
前提:需要进入mysql中
mysql> create database bbs charset utf8mb4;
mysql> use bbs
mysql> source /opt/bbs.sql
mysql> drop database bbs;
同时我们需要在Django项目中的setting.py配置文件,修改一下两处
ALLOWED_HOSTS = ['*']
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'bbs',
'HOST': "10.0.0.100",
'USER': 'root',
'PASSWORD': '123',
'PORT': 3306,
}
并且我们需要在MYSQL中定义用户
白名单: 主机域IP地址
只允许本机访问mysql
root@'localhost'
root@'10.0.0.110'
root@'10.0.0.%'
root@'10.0.0.0/255.255.240.0'
root@'10.0.0.5%'
root@'%'
grant all
grant select,update,insert
项目部署
首先需要配置Nginx
vim /etc/nginx/conf.d/py.conf
配置Nginx配置文件
server {
listen 80;
server_name 10.0.0.100;
client_max_body_size 100M;
开设一个外网访问的接口
location /static {
alias /opt/BBS/static/;
}
开设一个外网访问的接口
location /media {
alias /opt/BBS/media;
}
location / {
index index.html;
include uwsgi_params;
uwsgi_pass 127.0.0.1:9090;
uwsgi_param UWSGI_SCRIPT BBS.wsgi;
uwsgi_param UWSGI_CHDIR /opt/BBS;
}
}
配置uwsgi
关闭所有已有的uwsgi进程
kill -9 `ps -ef |grep uwsgi|awk {'print $2'}`
vim uwsgi.ini
配置uwsgi的设置
[uwsgi]
socket = 127.0.0.1:9090
master = true
workers = 2
reload-mercy = 10
vacuum = true
max-requests = 1000
limit-as = 512
buffer-size = 30000
启动uwsgi
uwsgi --ini uwsgi.ini &
重启nginx
systemctl restart nginx
Python 在运维工作中的经典应用
1.安装ansible
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
yum install ansible -y
克隆虚拟机
hostnamectl set-hostname standby
设置其地址,并且删除UUID行
vim /etc/sysconfig/network-scripts/ifcfg-eth0
IPADDR=10.0.0.200
UUID行删掉
在host文件中增加一行
vim /etc/hosts
10.0.0.200 standby
同时重启network
systemctl restart network
Linux的 SSHD(22)
验证方式:
(1)用户+密码(PAM)
(2)秘钥验证(公钥:钥匙和私钥:锁)
通过秘钥对实现,需要将公钥分发到各节点
接着我们需要管理被控端,管理机先生成秘钥,然后推送公钥(注意要对自己的机器也进行推送)
ssh-keygen
ssh-copy-id -i ~/.ssh/id_rsa.pub root@10.0.0.200
for i in {1..12};do ssh-copy-id -i ~/.ssh/id_rsa.pub root@10.0.0.$i;done
配置一些被管理的主机清单
vim /etc/ansible/hosts
写入
[web]
10.0.0.100
10.0.0.200
在尝试跑剧本时,我们现需要进行测试
ansible all -m ping
ansible all -m shell -a "df -h"
然后我们再把剧本写好(关于自动化安装nginx)
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
vim playbook_nginx.yml 写入 - hosts: web remote_user: root vars: http_port: 80 tasks: - name: Add Nginx Yum Repository yum_repository: name: nginx description: Nginx Repository baseurl: http://nginx.org/packages/centos/7/$basearch/ gpgcheck: no - name: Install Nginx Server yum: name=nginx state=present - name: Configure Nginx Server template: src=./default.conf.template dest=/etc/nginx/conf.d/default.conf notify: Restart Nginx Server - name: Start Nginx Server service: name=nginx state=started enabled=yes handlers: - name: Restart Nginx Server service: name=nginx state=restarted
接着配置一下默认设置模板文件
vim default.conf.template
写入
server {
listen {{ http_port }};
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
}
最后执行即可
ansible-playbook