基于LNMP部署flask项目

关于LNMP

LNMP是指一组通常一起使用来运行动态网站或者服务器的自由软件名称首字母缩写。L指Linux,N指Nginx,M一般指MySQL,也可以指MariaDB,P一般指PHP,也可以指Perl或Python。

一,安装python

1、安装python依赖环境

[root@izbp1467zz09wfeyw0eutuz ~]# yum -y install openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqline-devel

2、安装python

[root@izbp1467zz09wfeyw0eutuz ~]# cd /usr/local/
[root@izbp1467zz09wfeyw0eutuz local]# wget https://www.python.org/ftp/python/3.6.6/Python-3.6.6.tgz

3、解压python3.6

[root@izbp1467zz09wfeyw0eutuz local]#tar -zxvf Python-3.6.6.tgz
[root@izbp1467zz09wfeyw0eutuz local]# cd Python-3.6.6

4、配置并编译python3.6

[root@izbp1467zz09wfeyw0eutuz Python-3.6.6]# ./configure --prefix=/usr/local/pyhton3
[root@izbp1467zz09wfeyw0eutuz Python-3.6.6]# make
[root@izbp1467zz09wfeyw0eutuz Python-3.6.6]# make install

5、建立python3和pip3的软链接

[root@izbp1467zz09wfeyw0eutuz ~]# ln -s /usr/local/pyhton3/bin/python3.6 /usr/bin/python3
[root@izbp1467zz09wfeyw0eutuz ~]# ln -s /usr/local/pyhton3/bin/pip3 /usr/bin/pip3

6、启动python,检查是否安装成功

[root@izbp1467zz09wfeyw0eutuz ~]# python3
Python 3.6.6 (default, Oct  9 2019, 17:06:57)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()

7、检查pip3版本

[root@izbp1467zz09wfeyw0eutuz ~]# pip3 -V
pip 10.0.1 from /usr/local/pyhton3/lib/python3.6/site-packages/pip (python 3.6)

python和pip3安装成功后如下图所示
基于LNMP部署flask项目_第1张图片

二、安装mysql数据库(MariaDB数据库)

1、在安装mysql前先调整yum与系统python版本

[root@izbp1467zz09wfeyw0eutuz bin]# vim yum
[root@izbp1467zz09wfeyw0eutuz bin]# vim /usr/libexec/urlgrabber-ext-down

[root@izbp1467zz09wfeyw0eutuz bin]# whereis python
python: /usr/bin/python /usr/bin/python2.7 /usr/lib/python2.7 /usr/lib64/python2.7 /etc/python /usr/include/python2.7 /usr/share/man/man1/python.1.gz

2、安装mysql数据库

[root@izbp1467zz09wfeyw0eutuz bin]# yum -y install mariadb-server

3、启动数据库服务

[root@izbp1467zz09wfeyw0eutuz bin]# systemctl start mariadb.service

4、设置开机启动

[root@izbp1467zz09wfeyw0eutuz bin]# systemctl enable mariadb.service
Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.

5、设置用户名和密码

[root@izbp1467zz09wfeyw0eutuz bin]# mysqladmin -uroot password "root"
[root@izbp1467zz09wfeyw0eutuz bin]# mysql -uroot -proot

6、查看数据库编码格式

MariaDB [(none)]> \s

7、退出数据库

MariaDB [(none)]> \q

8、修改数据库编码格式

[root@izbp1467zz09wfeyw0eutuz bin]# vim /etc/my.cnf

添加character-set-server=utf8
在这里插入图片描述
9、重启服务

[root@izbp1467zz09wfeyw0eutuz bin]# systemctl restart mariadb.service

10、再次查看数据库编码

MariaDB [(none)]> \s

只要数据库的相应编码格式为utf8就设置完成了
基于LNMP部署flask项目_第2张图片

三、安装Nginx

1、下载Nginx

[root@izbp1467zz09wfeyw0eutuz home]# wget http://nginx.org/download/nginx-1.13.7.tar.gz

2、安装Nginx依赖环境

[root@izbp1467zz09wfeyw0eutuz ~]# yum -y install gcc gcc-c++ openssl-devel pcre-devel httpd-tools  

3、解压Nginx

[root@izbp1467zz09wfeyw0eutuz home]# tar -zxvf nginx-1.13.7.tar.gz

4、添加Nginx

[root@izbp1467zz09wfeyw0eutuz nginx-1.13.7]# useradd nginx

5、配置Nginx

[root@izbp1467zz09wfeyw0eutuz nginx-1.13.7]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_mp4_module --with-http_flv_module

6、编译Nginx

[root@izbp1467zz09wfeyw0eutuz nginx-1.13.7]# make && make install

7、建立Nginx软链接

[root@izbp1467zz09wfeyw0eutuz nginx-1.13.7]# ln -s /usr/local/nginx/sbin/nginx /usr/sbin/

8、启动Nginx

[root@izbp1467zz09wfeyw0eutuz ~]# nginx

9、查看Nginx服务端口

[root@izbp1467zz09wfeyw0eutuz ~]# netstat -anptu | grep nginx
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      19810/nginx: master
[root@izbp1467zz09wfeyw0eutuz ~]#

四、部署flask项目

  上传项目到服务器之前需要把flask的app.debug = False模式改为False,把数据库的连接信息改为Linux环境下的连接信息app.config["SQLALCHEMY_DATABASE_URI"] = "mysql+pymysql://root:[email protected]:3306/数据库名"
1、上传flask项目,数据库sql文件,项目的依赖环境(项目的依赖环境可以在Pycharm的交互环境下用freeze > req.txt生成)解压flask项目后进入项目目录查看包含文件
  如果用xshell的话直接用rz命令就可以上传文件或目录

[root@izbp1467zz09wfeyw0eutuz movies]# ls
app  app.py  managa.py  nginx.conf  req.txt  venv

2、安装flask项目依赖环境

[root@izbp1467zz09wfeyw0eutuz home]# pip install -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com -r req.txt

3、进入数据库并创建一个项目所用的数据库

MariaDB [(none)]> create database move;
Query OK, 1 row affected (0.01 sec)

4、使用数据库

MariaDB [(none)]> use move
Database changed
MariaDB [move]>

5、导入项目所用的sql文件

MariaDB [move]> source /home/movie.sql

6、查看数据表

MariaDB [move]> show tables;

7、解压flask项目

[root@izbp1467zz09wfeyw0eutuz home]# tar -zxvf  movies.tar.gz

8、复制项目到/usr/local/nginx/html/

[root@izbp1467zz09wfeyw0eutuz home]# cp -r movies /usr/local/nginx/html/

9、查看项目文件内容

[root@izbp1467zz09wfeyw0eutuz movies]# ls
app  app.py  managa.py  nginx.conf  req.txt  venv

10、配置nginx.conf文件

[root@izbp1467zz09wfeyw0eutuz movies]# vim nginx.conf

输入如下内容

worker_processes 4;
events {
    worker_connections 262140;
}
http {
    include mime.types;
    default_type application/octet-stream;
    sendfile on;
    keepalive_timeout 65;
    limit_conn_zone $binary_remote_addr zone=addr:5m;
    upstream movies {
        server 127.0.0.1:5001;
        server 127.0.0.1:5002;
        server 127.0.0.1:5003;
        server 127.0.0.1:5004;
    }
    server {
        listen 80;
        server_name localhost;
        location / {
            root html;
            index index.html index.htm;
            proxy_pass http://movies;
        }
        location ~ \.flv$ {
            flv;
            limit_conn addr 4;
            limit_rate 1024k;
            rewrite ^/static/uploads/(.+?).flv$ /movies/app/static/uploads/$1.flv permanent;
        }
        location ~ \.mp4$ {
            mp4;
            limit_conn addr 4;
            limit_rate 1024k;
            rewrite ^/static/uploads/(.+?).mp4$ /movies/app/static/uploads/$1.mp4 permanent;
        }
        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
            root html;
        }
    }
}

11、进入Nginx所在的目录

[root@izbp1467zz09wfeyw0eutuz movies]# cd ..
[root@izbp1467zz09wfeyw0eutuz home]# ls
movies  movie.sql  movies.tar.gz  nginx  nginx-1.13.7  nginx-1.13.7.tar.gz  req.txt

12、复制项目里的nginx.conf到/usr/local/nginx/conf/目录下

[root@izbp1467zz09wfeyw0eutuz home]# cp movies/nginx.conf /usr/local/nginx/conf/
cp: overwrite ‘/usr/local/nginx/conf/nginx.conf’? y
[root@izbp1467zz09wfeyw0eutuz home]#

13、停止Nginx

[root@izbp1467zz09wfeyw0eutuz home]# nginx -s stop

然后重启Nginx

[root@izbp1467zz09wfeyw0eutuz home]# nginx
[root@izbp1467zz09wfeyw0eutuz home]# netstat -anptu | grep nginx

14、进入/usr/local/nginx/html/movies/目录下的项目目录

[root@izbp1467zz09wfeyw0eutuz home]# cd /usr/local/nginx/html/movies/
[root@izbp1467zz09wfeyw0eutuz movies]# ls
app  app.py  managa.py  req.txt  venv

15、启动项目

[root@izbp1467zz09wfeyw0eutuz movies]# nohup python managa.py runserver -h 127.0.0.1 -p 5001 &
[1] 20238
[root@izbp1467zz09wfeyw0eutuz movies]# nohup: ignoring input and appending output to ‘nohup.out’

16、查看启动的Nginx端口

[root@izbp1467zz09wfeyw0eutuz movies]# netstat -anptu | grep 500*
tcp        0      0 127.0.0.1:5001          0.0.0.0:*               LISTEN      20282/python3
tcp        0      0 127.0.0.1:5002          0.0.0.0:*               LISTEN      20285/python3
tcp        0      0 127.0.0.1:5003          0.0.0.0:*               LISTEN      20289/python3
[root@izbp1467zz09wfeyw0eutuz movies]#

17、接下来在自己的电脑输入本机的ip地址就能访问flask项目了
基于LNMP部署flask项目_第3张图片
部署结束

你可能感兴趣的:(python)