Linux项目部署发布

Linux项目部署发布

1.部署环境准备,准备python3和虚拟环境解释器,virtualenvwrapper
pip3 install -i https://pypi.douban.com/simple virtualenvwrapper

2.修改python3的环境变量,写入到/etc/profile中
PATH=/opt/python36/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/opt/ruby/bin/:/root/bin
3.修改~/.bashrc
写入变量

4.新建一个虚拟环境  s15vuedrf
mkvirtualenv  s15vuedrf

5.准备前后端代码
wget https://files.cnblogs.com/files/pyyu/luffy_boy.zip
wget https://files.cnblogs.com/files/pyyu/07-luffy_project_01.zip
如果代码在本地,传到服务器 使用 lrzsz 和xftp工具

(lfxc) [root@localhost lfxc]# yum install unzip


6.解压缩代码
unzip luffy_boy.zip
unzip 07-luffy_project_01.zip


7.从vue前端代码搞起
    1.准备node打包环境
    wget https://nodejs.org/download/release/v8.6.0/node-v8.6.0-linux-x64.tar.gz
    
    2.解压缩node包,配置环境变量,使用npm和node命令
    (lfxc) [root@localhost lfxc]# tar -zxvf node-v8.6.0-linux-x64.tar.gz
    
    进入node文件夹
    (lfxc) [root@localhost lfxc]# cd node-v8.6.0-linux-x64
    
    列出node文件夹内容
    (lfxc) [root@localhost node-v8.6.0-linux-x64]# ls
    bin  CHANGELOG.md  include  lib  LICENSE  README.md  share

    切换到bin目录下面
    (lfxc) [root@localhost node-v8.6.0-linux-x64]# cd bin
    (lfxc) [root@localhost bin]# ls
    node  npm  npx
    
    添加环境变量(看结尾处)
    vi /etc/profile
    PATH=/opt/python37/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/opt/enn/lfxc/node-v8.6.0-linux-x64/bin
    
    更新配置文件,让其生效
    source /etc/profile
    
    
    检测node和npm
    (lfxc) [root@localhost node-v8.6.0-linux-x64]# cd bin
    (lfxc) [root@localhost bin]# ls
    node  npm  npx
    (lfxc) [root@localhost bin]# node -v
    v8.6.0
    (lfxc) [root@localhost bin]# npm -v
    5.3.0

    
    4.安装vue项目所需的包
    切换到项目前台目录文件下
    cd /opt/s15vuedrf/07-luffy_project_01
    执行npm install  
    创建了文件夹node_modules
    执行npm run build
    创建了文件夹dist
    这两条都正确配置之后,就会生成一个 dist 静态文件目录,整个项目的前端内容和index.html都在这里了
    
    5.等待nginx加载这个 dist文件夹
    

    
    
    
    

    
8.部署后端代码所需的环境
1.激活虚拟环境
workon s15vuedrf

2.通过一条命令,导出本地的所有软件包依赖
pip3 freeze >  requirements.txt

3.将这个requirements.txt 传至到服务器,在服务器的新虚拟环境中,安装这个文件,就能安装所有的软件包了
pip3 install -r requirements.txt   
这个文件内容如下:项目所需的软件包都在这里了
[root@web02 opt]# cat requirements.txt
certifi==2018.11.29
chardet==3.0.4
crypto==1.4.1
Django==2.1.4
django-redis==4.10.0
django-rest-framework==0.1.0
djangorestframework==3.9.0
idna==2.8
Naked==0.1.31
pycrypto==2.6.1
pytz==2018.7
PyYAML==3.13
redis==3.0.1
requests==2.21.0
shellescape==3.4.1
urllib3==1.24.1
uWSGI==2.0.17.1
        
4.准备uwsgi 支持高并发的启动python项目(注意uwsgi不支持静态文件的解析,必须用nginx去处理静态文件)
1.安装uwsgi
pip3 install -i https://pypi.douban.com/simple uwsgi

2.学习uwsgi的使用方法
通过uwsgi启动一个python文件,例如:test.py
--http 指定http协议
--wsgi-file 指定一个python文件
uwsgi --http :8000 --wsgi-file test.py
        

通过uwsgi启动django项目,并且支持热加载项目,不重启项目,自动生效新的后端代码
--module 指定找到django项目的wsgi.py文件
uwsgi --http :8000 --module s15drf.wsgi --py-autoreload=1
        
5.使用uwsgi的配置文件,启动项目
1.创建一个uwsgi.ini配置文件,写入参数信息,这个文件建议和Django项目中的manage.py放在同一文件下,方便查找管理。
touch uwsgi.ini

(lfxc) [root@localhost myweb11]# tree
.
├── db.sqlite3
├── manage.py
├── myweb11
│   ├── __init__.py
│   ├── __pycache__
│   │   ├── __init__.cpython-37.pyc
│   │   ├── settings.cpython-37.pyc
│   │   ├── urls.cpython-37.pyc
│   │   └── wsgi.cpython-37.pyc
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
└── uwsgi.ini
2 directories, 11 files
                                
[uwsgi]
# Django-related settings
# 指定项目的绝对路径的第一层路径
chdir = /opt/enn/lfxc/myweb11

# 指定项目的 wsgi.py文件
# 写入相对路径即可,这个参数是以chdir参数为相对路径
module = myweb11.wsgi

# 写入虚拟环境解释器的绝对路径
# (lfxc) [root@localhost myweb11]# cdvirtualenv
# (lfxc) [root@localhost lfxc]# pwd
# /opt/enn/lfxc
home = /opt/enn/lfxc


# process-related settings
# master
master = true

# 指定uwsgi启动的进程个数                
processes = 1

# socket指的是,uwsgi启动一个socket连接,当你使用nginx+uwsgi的时候,使用socket参数.
# 没用Nginx时,使用socket参数会报错,错误如下:
# invalid request block size: 21573 (max 4096)...skip
socket = 0.0.0.0:8000
# 这个参数是uwsgi启动一个http连接,当你不用nginx只用uwsgi时,使用这个参数.
#http = 0.0.0.0:8000

# ... with appropriate permissions - may be needed
# chmod-socket = 664
# clear environment on exit
vacuum = true
        
6.使用uwsgi配置文件启动项目
uwsgi --ini  uwsgi.ini
        














        
supervisor进程管理工具

1.将linux进程运行在后台的方法有哪些
第一个,命令后面加上 &  符号
python  manage.py  runserver &
第二个 使用nohup命令
第三个使用进程管理工具


2.安装supervisor,使用python2的包管理工具 easy_install. 注意,此时要退出虚拟环境.
执行命令安装easy_install
yum install python-setuptools
执行命令安装supervisor
easy_install supervisor


3.通过命令,生成一个配置文件,这个文件就是写入你要管理的进程任务
echo_supervisord_conf > /etc/supervisor.conf


4.编辑这个配置文件,写入操作  django项目的 命令
vim /etc/supervisor.conf  
直接到最底行,写入以下配置
[program:s15luffy]
command=/opt/enn/lfxc/bin/uwsgi --ini /opt/enn/lfxc/luffy_boy/uwsgi.ini
/opt/enn/lfxc/bin/uwsgi


5.启动supervisord服务端,指定配置文件启动
5.1检查服务端是否启动
[root@localhost ~]# ps -ef |grep super
root       1646   1444  0 19:52 pts/0    00:00:00 grep --color=auto super
5.2启动服务端
[root@localhost ~]# supervisord -c /etc/supervisor.conf
Unlinking stale socket /tmp/supervisor.sock


6.通过supervisorctl管理任务
[root@localhost ~]# supervisorctl -c /etc/supervisor.conf
s15luffy    RUNNING    pid 1833, uptime 0:01:42


7.supervisor管理django进程的命令如下
supervisorctl直接输入命令会进入交互式的操作界面
supervisor> status s15luffy
s15luffy    STOPPED   Jan 05 08:16 PM

supervisor> start s15luffy
s15luffy: started

supervisor> status s15luffy
s15luffy    RUNNING   pid 1871, uptime 0:00:06

supervisor> stop all
s15luffy: stopped

supervisor>



8.启动luffy的后端代码




配置nginx步骤如下
# 查看uwsgi/supervisor进程是否有在运行
[root@localhost myweb11]# ps -ef |grep uwsgi
root  1951  1783  0  20:32 pts/0  00:00:00 grep --color=auto uwsgi
[root@localhost myweb11]# ps -ef |grep super
root  1953    1783  0  20:32 pts/0  00:00:00 grep --color=auto super


# 启动supervisord服务端,指定配置文件启动
[root@localhost myweb11]# supervisord -c /etc/supervisor.conf
Unlinking stale socket /tmp/supervisor.sock


# 启动supervisorctl管理任务,指定配置文件启动
[root@localhost myweb11]# supervisorctl -c /etc/supervisor.conf
s15luffy    RUNNING   pid 1984, uptime 0:00:35
supervisor>


# 网络工具没有安装,会有以下报错!!!
[root@localhost myweb11]# netstat -tunlp
-bash: netstat: command not found
[root@localhost myweb11]# yum install net-tools


# 检查uwsgi是否运行在9000端口上
[root@localhost myweb11]# netstat -tunlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:9000            0.0.0.0:*               LISTEN      2067/uwsgi          
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      961/sshd            
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1182/master         
tcp6       0      0 :::22                   :::*                    LISTEN      961/sshd            
tcp6       0      0 ::1:25                  :::*                    LISTEN      1182/master         
udp        0      0 127.0.0.1:323           0.0.0.0:*                           698/chronyd         
udp        0      0 0.0.0.0:68              0.0.0.0:*                           766/dhclient        
udp6       0      0 ::1:323                 :::*                                698/chronyd   



1.编译安装nginx
2.nginx.conf配置如下

#第一个server虚拟主机是为了找到vue的dist文件, 找到路飞的index.html
server {
    listen 80;
    server_name 192.168.142.133;
        #当请求来自于 192.168.13.79/的时候,直接进入以下location,然后找到vue的dist/index.html
        location / {
            root   /opt/enn/lfxc/07-luffy_project_01/dist;
            index  index.html;
        }
    }
    
#由于vue发送的接口数据地址是192.168.142.133:8000我们还得再准备一个入口server
server {
    listen 8000;
    server_name  192.168.142.133;
    #当接收到接口数据时,请求url是192.168.142.133:8000就进入如下location
    location /  {
        #这里是nginx将请求转发给uwsgi启动的9000端口
        uwsgi_pass 192.168.142.133:9000;
        # include就是一个“引入的作用”,就是将外部一个文件的参数,导入到当前的nginx.conf中生效
        include /opt/nginx1616/conf/uwsgi_params;
    }
}

3.启动nginx
./sbin/nginx  直接启动
此时可以访问 192.168.13.79  查看页面结果

猜你喜欢

转载自www.cnblogs.com/apollo1616/p/10227244.html