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
Ubuntu16.04下搭建mysql + uwsgi + nginx環境啟動flask 項目