Ubuntu16.04下搭建mysql + uwsgi + nginx环境启动flask 项目
1.安装mysql
Sudo apt-get install mysql
配置mysql的数据存储路径,默认在 /var/lib/mysql
sudo cp -R /var/lib/mysql/* /home/applex/mysqldb sudo chown -R mysql:mysql home/applex/mysqldb sudo gedit /etc/mysql/mysql.conf.d/mysqld.cnf
将 [mysqld] 组下的 datadir改为: datadir = /home/applex/mysqldb
sudo vim /etc/apparmor.d/usr.sbin.mysqld 把 /var/lib/mysql r /var/lib/mysql/** rwk 修改成 /home/applex/mysqldb r /home/applex/mysqldb/** rwk
重启mysql
sudo service apparmor restart sudo service mysql restart 或 sudo /etc/init.d/apparmor restart sudo /etc/init.d/mysql restart
配置msyql编码为utf8
sudo gedit /etc/mysql/mysql.conf.d/mysqld.cnf
在[mysqld]下面添加:
character-set-server=utf8
sudo gedit /etc/mysql/conf.d/mysql.cnf
添加
default-character-set=utf8 sudo gedit /etc/mysql/debian.cnf
添加
default-character-set=utf8
2.配置anaconda3
从官网下载当前最新sh安装脚本,执行
bash Anaconda3-2018.12-Linux-x86_64.sh
按照提示安装
conda的安装路径中不要包含中文, 根据项目创建虚拟环境并选择python3.6,其他具体操作方法见conda教程
conda create -n myenv python=3.6
3.conda环境中部署项目
若源码部署方式,conda虚拟环境切换到源码目录,执行
pip install -e .
注意源码目录必须包含setup.py 和 MANIFEST.ini
若以模块文件部署方式, conda虚拟环境切换到wheel文件目录,执行
pip install xxx.whl
模块安装后需要指定一个项目启动目录并编写入口文件,如下例:
# [main.py] form myproject import app if ‘__main__’ == __name__: app.run()
可以执行python main.py 启动flask内置服务器来测试打包的项目模块是否可以正常运行
4.安装uwsgi
切换到conda虚拟环境下,用pip安装uwsgi:
conda activate myenv
pip install uwsgi
安装uwsgi报UnicodeEncoding错误,请确认conda路径是否包含unicode字符(例如中文)
安装uwsgi报如下错,安装配置gcc4.7
/home/applex/conda/envs/myproject/compiler_compat/ld: error: lto-wrapper failed:
sudo apt-get install gcc-4.7 sudo rm /usr/bin/gcc sudo ln -s /usr/bin/gcc-4.7 /usr/bin/gcc
执行uwsgi –help确认uwsgi是否正确安装
uwsgi –help 查看帮助信息,若报如下错,安装配置libpcre3-dev
uwsgi: error while loading shared libraries: libpcre.so.1: cannot open shared object file: No such file or directory
sudo apt-get install libpcre3 libpcre3-dev find / -name libpcre.so.3 找到 /lib/x86_64-linux-gnu/libpcre.so.3 sudo ln -s /lib/x86_64-linux-gnu/libpcre.so.3 /usr/lib/libpcre.so.1
编写uwsgi配置脚本
# [my_uwsgi.ini] # 指定python环境 pythonpath = /home/applex/conda/envs/myproject/bin # 启动http地址还是socket地址(用于与代理通信) http = 127.0.0.1:7878 # socket = 127.0.0.1:7878 # 指定进程数(同process项) workers = 4 # 指定线程数 threads = 2 # 设置日志路径 daemonize = /home/applex/myproject/server.log # 设置app启动目录 chdir = /home/applex/myproject # 设置app启动文件(也可直接指定文件全路径,无依赖时省略chdir) # 注意引用的py文件不能在调用时启动app实例 wsgi-file = ./main.py # 指定application对象实例 callable= app
启动uwsgi服务器
uwsgi mypath/ my_uwsgi.ini
查看uwsgi是否运行
ps -aux | grep uwsgi
查看端口号占用
netstat -anp | grep 7878
结束uwsgi进程
pgrep uwsgi | xargs kill -s 9
在配置的daemonize参数指定的文件中查看错误日志
5. 安装redis
sudo apt-get install redis-server
6.安装nginx
sudo apt-get install nginx
删除/etc/nginx/sites-enabled目录下的default链接,复制/etc/nginx/sites-available/default到/etc/nginx/sites-enabled,并按需求命名为myproject.conf,拷贝uwsgi_params到项目路径以便后期客制
cp /etc/nginx/uwsgi_params /home/applex/myproject/
修改myproject.conf,增加:
server { listen 80; listen [::]:80; server_name 127.0.0.1; charset utf-8; # 转发url请求到uwsgi location / { include /home/applex/myproject/uwsgi_params; uwsgi_pass 127.0.0.1:8000; uwsgi_param UWSGI_CHDIR /home/applex/myproject; uwsgi_param UWSGI_SCRIPT main:app; } # 配置nginx处理静态文件 location /static { alias /home/applex/conda/envs/myproject/lib/python3.6/site-packages/myproject/static; } }
操作nginx服务
sudo service nginx start/stop/reload