Flask部署

安装python3.6

yum install openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel gcc gcc-c++ openssl-devel

wget https://www.python.org/ftp/python/3.6.5/Python-3.6.5.tgz

tar -xzvf Python-3.6.5.tgz

cd Python-3.6.5/

./configure --prefix=/usr/local

make

make install

python3.6程序的执行文件:/usr/local/bin/python3.6

python3.6应用程序目录:/usr/local/lib/python3.6

pip3的执行文件:/usr/local/bin/pip3.6

更改/usr/bin/python链接

ln -s /usr/local/bin/python3.6 /usr/bin/python3

ln -s /usr/local/bin/pip3.6 /usr/bin/pip3

安装完成后python3环境下测试 import ssl是否报错

 

Mysql

ln -s /usr/local/mysql/bin/mysql /usr/bin    输入mysql命令,系统默认会查找/usr/bin下的命令,新建链接文件,映射链接到/usr/bin目录下
设置开机自启动
chmod +x /etc/init.d/mysqld

chkconfig --add mysqld    添加为服务

chkconfig --list    查看服务列表,看到3、4、5状态为开或者为on则表示成功

临时改变mysql模式
mysql> set sql_mode = 'STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
mysql配置文件中修改
vim /etc/mysql/mysql.conf.d/mysqld.cnf
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION    最后一行添加

远程连接配置:
mysql> GRANT ALL PRIVILEGES ON *.* TO root@"%" IDENTIFIED BY "123456";
mysql> flush privileges;
阿里云控制台打开3306端口

 

nginx安装

 

源码安装

    cd /usr/local
    wget -c https://nginx.org/download/nginx-1.13.7.tar.gz
    yum install gcc-c++
    yum install -y pcre pcre-devel
    yum install -y zlib zlib-devel
    yum install -y openssl openssl-devel
    tar -zxvf nginx-1.13.7.tar.gz
    cd nginx-1.13.7
    ./configure
    make
    make install 
    cd .. 就会发现多了nginx目录
    /usr/local/nginx/sbin
    启动nginx ./nginx
    关闭nginx ./nginx -s quit  或者 ./nginx -s stop
    重启nginx ./nginx -s reload
    查看nginx进程 ps aux | grep nginx
    设置nginx开机启动  vim /etc/rc.local  底部增加/usr/local/nginx/sbin/nginx

二进制安装

    sudo yum install epel-release

    sudo yum install nginx

 

 

redis安装

sudo yum -y install redis
sudo systemctl start redis
vi /etc/redis.conf    为了可以使Redis能被远程连接,需要修改配置文件
注释这一行:#bind 127.0.0.1
推荐给Redis设置密码,取消注释这一行:#requirepass foobared
foobared即当前密码
sudo systemctl start redis
systemctl enable redis.service    开机启动redis服务器
redis-cli    进入命令行

redis远程连接

阿里云服务器安全组6379端口设置
sudo apt-get install firewalld
firewall-cmd --zone=public --add-port=6379/tcp --permanent    # 永久开启 6379 端口
firewall-cmd --reload    # 重启端口

/etc/init.d/redis-server stop
/etc/init.d/redis-server start
/etc/init.d/redis-server restart
/etc/init.d/redis-server force-reload

 

阿里云apache关闭

cd /usr/local/apache/bin
./apachectl stop

 

apidoc文档

安装
    wget https://nodejs.org/dist/v12.13.0/node-v12.13.0-linux-x64.tar.xz
    tar -xvf node-v12.13.0-linux-x64.tar.xz
    mv node-v12.13.0-linux-x64 /usr/local/nodejs
    ln -s /usr/local/nodejs/bin/node /usr/local/bin
    ln -s /usr/local/nodejs/bin/npm /usr/local/bin​
    node -v
    npm install acpidoc -g
    ln -s /usr/local/nodejs/bin/apidoc /usr/local/bin
生成
    apidoc -i . -o api_doc/

 

nginx配置修改

vim /etc/nginx/nginx.conf

    user=root;
    ...
    server {
            listen       80 default_server;
            listen       [::]:80 default_server;
            server_name  127.0.0.1;                 #此处修改为阿里云服务器公网ip
            root         /usr/share/nginx/html;        #修改为:root /var/www/project;Nginx 需要配置静态资源的路径信息才能通过 url 正确访问到服务器上的静态资源

            # Load configuration files for the default server block.
            include /etc/nginx/default.d/*.conf;

            location / {                    #location中括号内容替换
              proxy_pass    http://127.0.0.1:5000;
              proxy_redirect    off;

              proxy_set_header   Host             $host;
                proxy_set_header   X-Real-IP        $remote_addr;
                proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
              proxy_set_header   X-Forwarded-Proto    $scheme;
        }
        ...
    }

service nginx restart
centos

 

server {
    listen      80;
    server_name 127.0.0.1;
    root /var/www/project;

    
    gzip on;
    gzip_vary on;
    gzip_types text/plain application/json application/javascript application/x-javascript text/css text/xml application/xml text/javascript application/xml+rss application/vnd.ms-fontobject application/x-httpd-php image/jpeg image/gif image/png application/x-font-ttf font/opentype font/ttf font/otf font/x-woff application/font-woff image/svg+xml image/x-icon application/octet-stream;
    gzip_min_length 1024;
    gzip_disable "MSIE [1-6]\.";

    gzip_proxied any;
    gzip_comp_level 1;

    location / {
        proxy_pass         "http://localhost:5000";
        proxy_redirect     off;
        proxy_set_header   Host $host;
        proxy_set_header   X-Real-IP $remote_addr;
        fastcgi_read_timeout 300s;
        proxy_read_timeout 300;
    }

    location /static {
        alias /var/www/project/static;
    }
}

./nginx -s reload
ubuntu

 

命令行部署

celery启动
    (venv)nohup celery -A celery_worker.celery worker -B -l info --logfile=logs/celery.log > /dev/null 2>&1 &

gunicorn启动
    (venv)nohup gunicorn -w 4 wsgi:app -b 127.0.0.1:5000 > logs/gunicorn.log 2>&1 &    

 

supervisor部署(在虚拟环境中部署)

pip install supervisor

echo_supervisord_conf > supervisord.conf

vi supervisord.conf
    文件结尾增加
        ;conf.d 配置表目录的文件夹
        [include]
        files = conf.d/*.conf

mkdir conf.d

cd conf.d

vim wsgi.conf
    [program:wsgi]
    user=root
    command=gunicorn wsgi:app -c gun.py    # gun.py gunicorn配置文件
    directory=/var/www/project
    autostart=true
    autorestart=true
    redirect_stderr=true

vim celery.conf
    [program:celery]
    command=celery -A celery_worker.celery worker -B -l info
    directory=/var/www/project
    autostart=true
    autorestart=true
    stdout_logfile=/var/www/project/logs/celery.access.log
    stderr_logfile=/var/www/project/logs/celery.error.log

cd ..

supervisord -c supervisord.conf        通过配置文件启动 supervisor
supervisorctl reload        修改配置文件后重新使其生效
supervisorctl stop wsgi
supervisorctl stop celery 

logrotate

vim /etc/logrotate.d/gunicorn
/var/www/project/logs/*.log {
        daily
        missingok
        rotate 14
        compress
        delaycompress
        dateext
       dateformat .%Y-%m-%d
        notifempty
        sharedscripts
        postrotate
                kill -USR1 $(cat /var/www/project/logs/gunicorn.pid)
        endscript
}

 

项目主页

http://47.110.155.241/auth/login.html

api文档

http://47.110.155.241/api_doc/index.html
url

 

 

安装mysql

sudo apt-get update
sudo apt-get install mysql-server
sudo apt install mysql-client
sudo apt install libmysqlclient-dev

测试是否安装成功
    sudo netstat -tap | grep mysql


设置远程访问
    vi /etc/mysql/mysql.conf.d/mysqld.cnf
    注释掉bind-address = 127.0.0.1

进入mysql服务,授权
    查看密码策略
        mysql> SHOW VARIABLES LIKE 'validate_password%';
    设置密码强度等级
        mysql> set global validate_password_policy=LOW;
    设置密码为6位密码
        mysql> set global validate_password_length=6;
    mysql> GRANT ALL PRIVILEGES ON *.* TO root@"%" IDENTIFIED BY "123456";
    mysql> flush privileges;
确认mysql 3306端口是否开放
    netstat -an | grep 3306
查看mysql账号
    mysql> use mysql;
    mysql> SELECT User, Host, authentication_string FROM mysql.user;
阿里云控制台添加安全组规则3306
重启mysql
    /etc/init.d/mysql restart


netstat -nltp | grep 3306
lsof -i:3306

开启22端口
    firewall-cmd --permanent --zone=public --add-port=22/tcp
扫描端口状态
    nmap -P0 116.62.21.67
查询端口是否打开
    firewall-cmd --query-port=80/tcp
查看端口占用
    lsof -i:80

redis安装
    sudo apt-get install -y redis-server
    service redis status
    redis-server


nginx安装
    sudo apt-get install nginx
    nginx -v

    /usr/sbin/nginx:主程序
    /etc/nginx:存放配置文件
    /usr/share/nginx:存放静态文件
    /var/log/nginx:存放日志

    Linux系统的配置文件一般放在/etc,日志一般放在/var/log,运行的程序一般放在/usr/sbin或者/usr/bin

    Nginx的配置文件 /etc/nginx/nginx.conf

    nginx的配置文件在 /etc/nginx/sites-available/里,默认的是default文件

测试语法错误
    nginx -t


/etc/nginx/sites-available# touch project
sudo ln -s /etc/nginx/sites-available/project /etc/nginx/sites-enabled/project
/etc/nginx/sites-available# vim project

server {
    listen      80;
    server_name 116.62.21.67;
    # 动态目录
    location / {
        proxy_pass         "http://localhost:5000";
        proxy_redirect     off;
        proxy_set_header   Host $host;
        proxy_set_header   X-Real-IP $remote_addr;
        fastcgi_read_timeout 300s;
        proxy_read_timeout 300;
    }
    # 静态目录
    location /static {
         alias /var/www/project/static;
    }
}


sudo nginx -t
sudo service nginx restart



Ubuntu下的Nginx的目录结构大致如下:

1. 所有的配置文件都在/etc/nginx下,每个虚拟主机已经安排在了/etc/nginx/sites-available目录下
2. 启动程序文件在/usr/sbin/nginx
3. 日志文件放在了/var/log/nginx中,分别是access.log和error.log
4. 在/etc/init.d/下创建了启动脚本nginx
5. 默认的虚拟主机的目录设置在了/usr/share/nginx/www

更改配置文件后重启服务/etc/init.d/nginx restart,或者service nginx restart



systemctl status firewalld


防火墙
    安装
         sudo apt-get install ufw
    启用
         sudo ufw enable
         sudo ufw default deny
    关闭
        sudo ufw disable 
    查看防火墙状态
        sudo ufw status


ubuntu环境下测试
    curl http://127.0.0.1:5000
ubuntu

 

ubuntu 不止自带python2.7版本,同样有python3的版本,不可轻易卸载,ubuntu依赖python环境。

 

卸载ubuntu自带python3.5.2版本后,mysql报错

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock'

重新安装python3, sudo apt-get install python3

sudo apt install -f

sudo apt-get install python3-minimal

sudo apt-get -f install ubuntu-minimal ubuntu-standard ubuntu-desktop

sudo apt-get install mysql-server 

 

posted @ 2019-04-30 14:12  许二哈哈哈  阅读(299)  评论(0编辑  收藏  举报