欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

项目部署流程

程序员文章站 2022-06-11 13:29:03
...

项目部署流程

系统更新(centos6.8)

1:更新系统
    yum update -y
2:更新软件
    yum upgrade -y

配置免密登录

1:ssh-****** -t rsa # (本机)
2:vim ~/.ssh/id_rsa.pub # (复制本机公钥)
3:登录服务器  vim ~/.ssh/authorized_keys # (粘贴公钥,服务器)
4: 保存退出断开连接,重新连接

安装软件

安装常用依赖
1:编写脚本:
        install_software() {
            echo '正在安装系统组件...'
            BASIC='man gcc make sudo lsof ssh openssl tree vim language-pack-zh-hans'
            EXT='dnsutils iputils-ping net-tools psmisc sysstat'
            NETWORK='curl telnet traceroute wget'
            LIBS='libbz2-dev libpcre3 libpcre3-dev libreadline-dev libsqlite3-dev libssl-dev zlib1g-dev'
            SOFTWARE='git mysql-server zip p7zip apache2-utils sendmail'
            yum install -y $BASIC $EXT $NETWORK $LIBS $SOFTWARE

            echo '正在清理临时文件'
            yum autoremove
            yum autoclean

            echo '正在设置中文环境'
            locale-gen zh_CN.UTF-8
            export LC_ALL='zh_CN.utf8'
            echo "export LC_ALL='zh_CN.utf8'" >> /etc/bash.bashrc

            echo '正在启动邮件服务'
            service sendmail start

            echo -e '系统组件安装完毕.\n'
        }
安装mysql
1:yum list installed | grep mysql(查看是否安装了mysql)
2:yum -y remove mysql(删除已经安装的mysql,看到complete说明删除成功)
3:yum list | grep mysql(查看mysql版本)
4: yum -y install mysql mysql-server mysql-devel(安装mysql)
5:rpm -qi mysql-server(验证是否安装成功)
6:service mysqld start(启动mysql)
7:mysql -u root(进入mysql)
    如果出现(ERROR 2002 (HY000):# chown -R openscanner:openscanner /var/lib/mysql
8: use mysql;
    update user set password=password(123456) where user=‘root‘;
9:GRANT ALL PRIVILEGES ON *.* TO ‘root’@’%’ IDENTIFIED BY ‘你的密码’ WITH GRANT OPTION; 
FLUSH PRIVILEGES;
安装pyenv管理工具
1:安装:
    1:curl -L https://github.com/pyenv/pyenv-installer/raw/master/bin/pyenv-installer | bash
    2: export PATH="$HOME/.pyenv/bin:$PATH"
            eval "$(pyenv init -)"
            eval "$(pyenv virtualenv-init -)" 
    3:pyenv update    
2:配置pyenv:
    cat >> $HOME/.bashrc << EOF
    # PyenvConfig
    export PATH="\$HOME/.pyenv/bin:\$PATH"
    eval "\$(pyenv init -)"
    eval "\$(pyenv virtualenv-init -)"
    EOF
3:配置文件生效
    source $HOME/.bashrc
4:安装python
	pyenv install -v 3.6.7
    pyenv global 3.6.7
5:更新pip工具
    pip install -U pip
安装nginx
1:下载nginx(去官网拷贝地址)
    wget -P /tmp 'http://nginx.org/download/nginx-1.14.1.tar.gz'
2:解压:
	tar -xzf /tmp/nginx-1.14.1.tar.gz -C /tmp
3:安装:
	1:cd /tmp/nginx-1.14.1
    2./configure
    3:make && make install
    4:cd -
    5:rm -rf /tmp/nginx*
    6:ln -s /usr/local/nginx/sbin/nginx /usr/local/bin/nginx
    
安装redis
1:下载nginx
    wget -P /tmp/ 'http://download.redis.io/releases/redis-5.0.0.tar.gz'
2:解压:
    tar -xzf /tmp/redis-5.0.0.tar.gz -C /tmp
3:安装:
	1:cd /tmp/redis-5.0.0
    2:make && make install
    3:cd -
    4:rm -rf /tmp/redis*

上传代码

1:服务器:
    1:cd /opt/
    2:mkdir -p swiper/logs
2本机:
	1:cd到项目下:
	2:rsync -crvP --exclude={.venv,.git,__pycache__,logs} ./[email protected].X.X.X:/opt/swiper/

创建虚拟环境,安装包

1:创建虚拟环境,cd到项目下
    cd /opt/swiper/
    python -m venv
2:**环境:(注意虚拟环境路径)
    source ./venv/bin/activate
3:按住包:
    pip freeze install -r requirement.txt

项目测试

1:启动项目
    gunicorn -c swiper/gunicorn-config.py swiper.wsgi
2:查看是否启动
	ps aux | grep gunicorn
3:测试:连接状态
    curl http://127.0.0.1:gunicorn配置端口号/请求路径
            

配置nginx(反向代理gunicorn服务器)

user root;
worker_processes  4;
pid /run/nginx.pid;

events {
    use epoll;
    worker_connections  10240;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    log_format main '$time_local $remote_addr $status $request_time '
                    '$request [$body_bytes_sent/$bytes_sent]   '
                    '"$http_user_agent" "$http_referer"';

    sendfile            on;
    tcp_nopush          on;
    keepalive_timeout   65;
    gzip                on;

    upstream app_server {
        server 127.0.0.1:9000    weight=10; # nginx高可用配置
    }

    server {
        listen       80;  # 端口号
        server_name  swiper.seamile.org;  # 域名

        access_log  /opt/swiper/logs/access.log  main;
        error_log   /opt/swiper/logs/error.log;

        location = /favicon.ico  {      # title图标
            empty_gif;
            access_log off;
        }

        # location /statics/ {
        #     root   /opt/swiper/;
        #     expires 30d;
        #     access_log off;
        # }
		#反向代理gunicorn服务器,
        location / {
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
            proxy_set_header Host $http_host;
            proxy_redirect off;
            proxy_pass http://app_server;   
        }
    }
}

配置nginx(反向代理uWSGI)


#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;
	upstream dailyfresh {
		server 127.0.0.1:8080;
		server 127.0.0.1:8081;
	}

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        #location / {
        #    root   html;
        #    index  index.html index.htm;
        #}
		# uWSGI配置
		location / {
			# 包含uwsgi请求的参数
			include uwsgi_params;
			# 转交请求给uwsgi
			#uwsgi_pass 127.0.0.1:8080;
			uwsgi_pass dailyfresh;
		}
		
		location /static {
			# 指定静态文件存放的目录
			alias /var/www/dailyfresh/static/;	
		}
		
		location = / {
			proxy_pass http://172.16.179.131;	
		}
        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

测试

1:访问公网ip

压力测试