centos7下Flask+ supervisord+ uWSGI+Nginx在的部署
开始把测试环境开发好的项目正式上线到服务器上啦,大概需要以下几步
1.安装lnmp环境,Linux+ nginx+MySQL+python3
2.安装uWSGI web服务器
3.安装supervisord 进程管理工具
4.安装Redis
一、安装lnmp环境
1.安装依赖环境
yum -y install openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel gcc gcc-c++
2.下载Python3.6 进行安装
wget https://www.python.org/ftp/python/3.6.1/Python-3.6.1.tgz /configure --prefix=/usr/local/ make&& make altinstall ln -s /usr/local/bin/python3.6 /usr/bin/python ln -s /usr/local/bin/python3.6 /usr/bin/python3
/usr/bin/yum
/usr/bin/yum-builddep
/usr/bin/yum-config-manager
/usr/bin/yum-debug-dump
/usr/bin/yum-debug-restore
/usr/bin/yumdownloader
/usr/bin/yum-groups-manager
/usr/libexec/urlgrabber-ext-down
#安装virtualenv pip install virtualenv #创建独立的Python运行环境 #virtualenv --no-site-packages venv 进入虚拟环境: source venv/bin/activate #导出程序依赖环境 pip freeze > requirements.txt #安装依赖环境 pip install -i https://pypi.tuna.tsinghua.edu.cn/simple/ --trusted-host pypi.tuna.tsinghua.edu.cn -r requirements.txt
4.安装Mariadb-server
闲置已久的空间环境配置忘得差不多了,今天得空整理,重置了磁盘重新搭建环境,首先在CentOS 7.0安装MariaDB的数据库,在这里记录下安装过程,以便以后查看。 1、安装MariaDB 安装命令 yum -y install mariadb mariadb-server 安装完成MariaDB,首先启动MariaDB systemctl start mariadb 设置开机启动 systemctl enable mariadb 接下来进行MariaDB的相关简单配置 mysql_secure_installation 首先是设置密码,会提示先输入密码 Enter current password for root (enter for none):<–初次运行直接回车 设置密码 Set root password? [Y/n] <– 是否设置root用户密码,输入y并回车或直接回车 New password: <– 设置root用户的密码 Re-enter new password: <– 再输入一次你设置的密码 其他配置 Remove anonymous users? [Y/n] <– 是否删除匿名用户,回车 Disallow root login remotely? [Y/n] <–是否禁止root远程登录,回车, Remove test database and access to it? [Y/n] <– 是否删除test数据库,回车 Reload privilege tables now? [Y/n] <– 是否重新加载权限表,回车 初始化MariaDB完成,接下来测试登录 mysql -uroot -ppassword 完成。 2、配置MariaDB的字符集 文件/etc/my.cnf vi /etc/my.cnf 在[mysqld]标签下添加 init_connect='SET collation_connection = utf8_unicode_ci' init_connect='SET NAMES utf8' character-set-server=utf8 collation-server=utf8_unicode_ci skip-character-set-client-handshake 文件/etc/my.cnf.d/client.cnf vi /etc/my.cnf.d/client.cnf 在[client]中添加 default-character-set=utf8 文件/etc/my.cnf.d/mysql-clients.cnf vi /etc/my.cnf.d/mysql-clients.cnf 在[mysql]中添加 default-character-set=utf8 全部配置完成,重启mariadb systemctl restart mariadb 之后进入MariaDB查看字符集 mysql> show variables like "%character%";show variables like "%collation%"; 显示为 +--------------------------+----------------------------+ | Variable_name | Value | +--------------------------+----------------------------+ | character_set_client | utf8 | | character_set_connection | utf8 | | character_set_database | utf8 | | character_set_filesystem | binary | | character_set_results | utf8 | | character_set_server | utf8 | | character_set_system | utf8 | | character_sets_dir | /usr/share/mysql/charsets/ | +--------------------------+----------------------------+ 8 rows in set (0.00 sec) +----------------------+-----------------+ | Variable_name | Value | +----------------------+-----------------+ | collation_connection | utf8_unicode_ci | | collation_database | utf8_unicode_ci | | collation_server | utf8_unicode_ci | +----------------------+-----------------+ 3 rows in set (0.00 sec) 字符集配置完成。 3、添加用户,设置权限 创建用户命令 mysql>create user username@localhost identified by 'password'; 直接创建用户并授权的命令 mysql>grant all on *.* to username@localhost indentified by 'password'; 授予外网登陆权限 mysql>grant all privileges on *.* to username@'%' identified by 'password'; 授予权限并且可以授权 mysql>grant all privileges on *.* to username@'hostname' identified by 'password' with grant option; 简单的用户和权限配置基本就这样了。 其中只授予部分权限把 其中 all privileges或者all改为select,insert,update,delete,create,drop,index,alter,grant,references,reload,shutdown,process,file其中一部分。
5.安装NGINX
yum -y install gcc gcc-c++ openssl-devel pcre-devel httpd-tools wget http://101.96.10.64/nginx.org/download/nginx-1.14.0.tar.gz useradd nginx ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_mp4_module --with-http_flv_module make &&make install
nginx.conf 配置文件
location / { include uwsgi_params; uwsgi_pass 127.0.0.1:8001; # 指向uwsgi 所应用的内部地址,所有请求将转发给uwsgi 处理 uwsgi_param UWSGI_PYHOME /home/junesu/movie/venv; # 指向虚拟环境目录 uwsgi_param UWSGI_CHDIR /home/junesu/movie/; # 指向网站根目录 uwsgi_param UWSGI_SCRIPT manage:app; # 指定启动程序 } location ~ \.flv$ { flv; limit_conn addr 1; limit_rate 512k; rewrite ^/static/uploads/(.+?).flv$ /app/static/uploads/$1.flv permanent; } location ~ \.mp4$ { root /home/junesu/movie/; mp4; limit_conn addr 1; limit_rate 512k; rewrite ^/static/uploads/(.+?).mp4$ /app/static/uploads/$1.mp4 permanent; } #静态资源处理 location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|js|css)$ { root /home/junesu/movie/app/; expires 30d; }
二、安装uWSGI web服务器
uWSGI是一个web服务器,Nginx进行反向代理的其实跟这些服务器可以说没有任何关系,你提供动态内容的服务器可以是apache/nginx/tomcat,当然也可以是uWSGI,他们之间的代理关系其实都是通过tcp/ip协议进行通信的。当然uWSGI相对于其它服务器来说有其特殊的地方,不同之处在于它可以提供独特的uwsgi协议进行通信。也就是说,nginx和uWSGI之间的通信协议可以有多种选择,但常用http和uwsgi这两种。
我们用uWSGI启动我们的项目
1.创建config.ini
[uwsgi] # uwsgi 启动时所使用的地址与端口 socket = 127.0.0.1:8001 # 指向网站目录 chdir = /home/www/ # python 启动程序文件 wsgi-file = manage.py # python 程序内用以启动的 application 变量名 callable = app # 处理器数 processes = 4 # 线程数 threads = 2 #状态检测地址 stats = 127.0.0.1:9191
2.运行我们的项目
uwsgi config.ini
三、安装 Supervisor
Supervisor可以同时启动多个应用,最重要的是,当某个应用Crash的时候,他可以自动重启该应用,保证可用性。
#安装 yum install supervisor #设置开机启动 systemctl enable supervisord.service #启动 systemctl start supervisord.service #关闭 systemctl stop supervisord.service #重启 systemctl restart supervisord.service
配置进程
例如有个nginx 进程设置
vim /etc/supervisord.d/nginx.ini
1 [program:nginx] 2 ;directory = /www/lanmps/bin ; 程序的启动目录 3 command = /www/lanmps/bin/nginx start ; 启动命令,可以看出与手动在命令行启动的命令是一样的 4 autostart = true ; 在 supervisord 启动的时候也自动启动 5 startsecs = 5 ; 启动 5 秒后没有异常退出,就当作已经正常启动了 6 autorestart = true ; 程序异常退出后自动重启 7 startretries = 3 ; 启动失败自动重试次数,默认是 3 8 user = www ; 用哪个用户启动 9 redirect_stderr = true ; 把 stderr 重定向到 stdout,默认 false 10 stdout_logfile_maxbytes = 20MB ; stdout 日志文件大小,默认 50MB 11 stdout_logfile_backups = 20 ; stdout 日志文件备份数 12 ; stdout 日志文件,需要注意当指定目录不存在时无法正常启动,所以需要手动创建目录(supervisord 会自动创建日志文件) 13 stdout_logfile = /www/logs/usercenter_stdout.log 14 stopasgroup=false ;默认为false,进程被杀死时,是否向这个进程组发送stop信号,包括子进程 15 killasgroup=false ;默认为false,向进程组发送kill信号,包括子进程
这是我的flask的管理进程配置
[program:movie] # 启动命令入口 command=/home/junesu/movie/venv/bin/uwsgi /home/junesu/movie/config.ini # 命令程序所在目录 directory=/home/junesu/movie #运行命令的用户名 user=root autostart=true autorestart=true #日志地址 stdout_logfile=/home/junesu/movie/logs/uwsgi_supervisor.log
supervisord 客户端管理命令
1 supervisorctl status # 状态 2 supervisorctl stop nginx #关闭 nginx 3 supervisorctl start nginx #启动 nginx 4 supervisorctl restart nginx #重启 nginx 5 supervisorctl reread 6 supervisorctl update #更新新的配置