Linux 基础(三)

Linux 基础(三)

一、Linux中完整运行Django项目,前期准备

LNM+Python Django+uwsgi+redis项目
安装项目中需要的包:
pip3 install -i https://pypi.doubanio.com/simple/ -r re.txt

vim  re.txt

asn1crypto==0.24.0
beautifulsoup4==4.6.3
bs4==0.0.1
certifi==2018.4.16
cffi==1.11.5
chardet==3.0.4
Click==7.0
cryptography==2.3.1
Django==1.11.9
Flask==1.0.2
Flask-Cors==3.0.6
gevent==1.3.6
greenlet==0.4.15
idna==2.7
ItsDangerous==1.1.0
Jinja2==2.10
lxml==4.2.6
MarkupSafe==1.0
numpy==1.15.3
Pillow==5.3.0
pycparser==2.18
PyMySQL==0.9.2
pytz==2018.7
requests==2.19.1
selenium==3.141.0
six==1.11.0
urllib3==1.23
virtualenv==16.1.0
Werkzeug==0.14.1
wordcloud==1.5.0

二、数据库的处理

(1)上传 bbs.sql (项目的数据库sql文件)
(2)在mysql中创建bbs库,并导入数据库SQL脚本
create database bbs charset utf8mb4;
use bbs;
source /opt/bbs.sql;
drop database bbs;
(3)查看项目 settings.py 配置文件,修改以下两处
ALLOWED_HOSTS = ['*']

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'bbs',
        'HOST': "10.0.0.100", # 改成自己虚拟机的IP
        'USER': 'root',
        'PASSWORD': '123',
        'PORT': 3306,
    }
}
(4)MySQL用户的定义
  • USERNAME@'白名单'
  • 白名单:主机域IP地址
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

三、BBS项目部署

(1)配置 nginx
vim /etc/nginx/conf.d/py.conf

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;
}
}

(2)配置uwsgi
# 关闭所有已有的uwsgi进程
kill -9 `ps -ef |grep uwsgi|awk {'print $2'}`

# 配置uwsgi 
vim  uwsgi.ini

[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 在运维工作中的经典应用

-- ansible
(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
# 编辑配置文件
vim /etc/sysconfig/network-scripts/ifcfg-eth0
# 修改IP
IPADDR=10.0.0.200
# 将UUID这行删掉

vim /etc/hosts
# 添加以下一行 到 /etc/hosts
10.0.0.200 虚拟机名

systemctl restart network
补充:
Linux的 SSHD(22)
验证方式:
(1)用户+密码(PAM)
(2)秘钥验证(公钥:钥匙和私钥:锁)
    通过秘钥对实现,需要将公钥分发到各节点
(2)管理被控端,管理机先生成秘钥,然后推送公钥
ssh-keygen
ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]

# 批量推送公钥
for i in {1..12};do ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected].$i;done
(3)配置被管理的主机清单
vim /etc/ansible/hosts

[web]
10.0.0.100
10.0.0.200
(4)使用ansible的ad-hoc测试
ansible all -m ping
# ping以下,成功如下
10.0.0.12 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
10.0.0.11 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
(5)ansible playbook自动化安装nginx
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
(6)default.conf.template文件如下
vim default.conf.template 

server {
    listen       {{ http_port }};
    server_name  localhost;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }
}
(7)执行ansible-playbook
# 检查语法
ansible-playbook --syntax playbook_nginx.yml     

# 模拟执行
ansible-playbook -C playbook_nginx.yml 

# 执行
ansible-playbook playbook_nginx.yml

五、Docker 容器技术

(1)环境准备类
curl  http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -o /etc/yum.repos.d/docker-ce.repo
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 -y yum-utils device-mapper-persistent-data lvm2


yum list docker-ce.x86_64 --showduplicates | sort -r


yum install -y --setopt=obsoletes=0 \
docker-ce-17.03.2.ce-1.el7.centos.x86_64 \
docker-ce-selinux-17.03.2.ce-1.el7.centos.noarch


systemctl daemon-reload
systemctl restart docker

docker version
docker  info


# 配置镜像加速


# 阿里云Docker-hub

https://cr.console.aliyun.com/cn-hangzhou/mirrors

mkdir -p /etc/docker

tee /etc/docker/daemon.json <<-'EOF'
{
   "registry-mirrors": ["https://68rmyzg7.mirror.aliyuncs.com"]
}
EOF   
      
      
# 或者:

vim   /etc/docker/daemon.json

    {
         "registry-mirrors": ["https://68rmyzg7.mirror.aliyuncs.com"]
    }
(2)pull 常用镜像
docker pull  centos:6.9
docker pull  centos:7.5.1804
docker pull  nginx

你可能感兴趣的:(Linux 基础(三))