以下所有操作均在ubuntu操作系統中進行
sudo apt-get update
sudo apt-get upgrade
首先停止tomcat7的運行不然后占用80端口導致nginx無法運行
sudo service tomcat7 stop
全局安裝的工具
1.安裝mongoDB
Install MongoDB Community Edition on Ubuntu
安裝之后建議停掉mongoDB的進程, 重新啟動一次, 防止下次啟動時報下面這個錯誤
mongodb exception in initAndListen: 29 Data directory /data/db not found
報上面那個錯誤的原因是, 沒有用指定數據庫路徑的命令啟動, 如果用下面的命令啟動, 則不會報上面那個錯誤
mongod --dbpath /var/lib/mongodb --auth
或者 用指定的配置文件啟動數據庫, 也不會報上面那個錯誤
mongod --config /etc/mongod.conf
如果報上面那個錯誤則, 使用
#用root權限創建db文件
sudo mkdir -p /data/db
啟動數據庫
mongod
mongoDB的使用
創建超級管理員
mongo
use admin
db.createUser({user:'root',pwd:'root',roles:[{ "role" : "root", "db" : "admin"}]});
exit
關閉數據庫
mongod --shutdown --dbpath /database/mongodb/data/
用驗證模式啟動數據庫
mongod --auth
創建es_testing_db
數據庫
可以在本地客戶端創建
use es_testing_db
使用es_testing_db
數據庫, 沒有會自動創建
db.createUser({user:"superLi",pwd:"xxxxxx",roles:[{role:"dbOwner",db:"es_testing_db"}]})
創建用戶
退出mongo
交互
exit
mongoDB后臺運行, daemon
簡單的是命令后面加“&”
mongod --config /etc/mongod.conf --auth &
獲取Document的_id
pk_dict = order._object_key
object_id = pk_dict['pk']
orderInfo["order_id"] = str(object_id)
更新數據
task = 'task 1'
todo = Todo.objects(task=task).first() # 先查找
if not todo:
return "the task doesn't exist!"
todo.update(is_completed=True) # 再更新
2.安裝git
sudo apt-get install git
3.安裝nginx服務器
阿里云服務器有時候已經安裝好了, 自帶了nginx服務器
查找nginx配置文件
find / -name nginx.conf
刪除阿里自帶的nginx php 和Tomcat7
sudo service nginx stop
或者
pgrep nginx
kill 1127
然后
cd /alidata
sudo rm -rf server/
sudo apt-get install nginx
運行nginx服務器
sudo /etc/init.d/nginx start
實時查看nginx日志更新
tail -f EnShengServerError.log
4.安裝supervisor
sudo apt-get install supervisor
5.安裝pip
sudo apt-get install python-pip
5.安裝python開發環境, 不然pycrypto
無法安裝
sudo apt-get install python-dev
7.安裝virtualenv
sudo pip install virtualenv virtualenvwrapper
配置virtualenv
不管python是什么版本,都要執行下面兩句:
echo "export WORKON_HOME=~/Env" >> ~/.bashrc
echo "source /usr/local/bin/virtualenvwrapper.sh" >> ~/.bashrc
可以重啟系統激活,也可以運行:
source ~/.bashrc
然后就可以開始建立虛擬環境了
8.下載代碼
git clone https://xxxxxxxxxxxx
進入項目目錄
創建虛擬環境
virtualenv venv
激活虛擬環境
source venv/bin/activate
在虛擬環境里執行
9..執行
pip install -r requirements.txt
的時候提示'pycrypto'安裝失敗
原來是因為, 需要先安裝python-dev
執行
sudo apt-get install python-dev
后在執行pip install pycrypto
,pycrypto安裝成功
10.配置nginx服務器
cd /etc/nginx/sites-available
vim enshengserver
server {
listen 80;
location /static {
alias /home/enshengserver/static;
}
location / {
proxy_pass http://127.0.0.1:9000;
}
}
https版nginx配置
server {
listen 443 ssl;
server_name misite.ltd;
ssl_certificate /etc/nginx/ssl/nginx.crt;
ssl_certificate_key /etc/nginx/ssl/nginx.key;
location /static {
alias /home/enshengserver/static;
}
location / {
proxy_pass http://127.0.0.1:9000;
}
}
任意http跳轉https的配置
server {
listen *:80;
return 301 https://www.your_domain.com$request_uri;
}
server {
listen 443 ssl;
server_name www.your_domain.com;
ssl_certificate /etc/nginx/ssl/nginx.crt;
ssl_certificate_key /etc/nginx/ssl/nginx.key;
location /static {
alias /home/static;
}
location / {
root /home/webSite/;
}
location /en_sheng_api {
proxy_pass http://127.0.0.1:9000;
}
}
創建配置文件的軟連接
sudo ln -s /etc/nginx/sites-available/enshengserver /etc/nginx/sites-enabled/
重新加載nginx配置文件
sudo service nginx reload
2.supervisor的使用
編寫配置文件
cd /etc/supervisor
cd conf.d/
sudo vim app.conf
[program:EnShengServer]#應用程序名稱
autostart = True #在 supervisord 啟動的時候也自動啟動
autorestart = True #程序異常退出后自動重啟
environment = PATH = "/home/enshengserver/venv/bin" #可以通過 environment 來添加需要的環境變量,一種常見的用法是使用指定的 virtualenv 環境
command = python EnShengServer.py #啟動命令,與手動在命令行啟動的命令是一樣的
directory = /home/enshengserver #程序的啟動目錄
stderr_logfile = /var/log/supervisor/EnShengServerError.log #錯誤日志輸出路徑
stdout_logfile = /var/log/supervisor/EnShengServer.log #日志輸出路徑
注意:
directory
不能放在command
上面 否則無法啟動
用虛擬環境啟動也可以寫成
command = /home/enshengserver/venv/bin/gunicorn -b 127.0.0.1:9000 EnShengServer:
app
ubuntu查看進程指令
sudo netstat -antup
查看指定程序的進程
pgrep nginx
殺掉進程
kill 1313
啟動supervisor
sudo service supervisor start
或supervisorctl reload
重新加載配置
進入supervisor控制臺
sudo supervisorctl
status
查看進程狀態
stop EnShengServer
停止程序運行
start EnShengServer
運行程序
3.mongoDB的安裝
參考Ubuntu16.04安裝mongodb
ubuntu 12.04 Server 上安裝 MongoDB及運行
ubuntu12.04上的mongodb卸載
4.Nginx https配置的關鍵是監聽443端口
運維方案的實現
fabric安裝
Mac下安裝fabric
brew install fabric
ubuntu安裝fabric
pip install fabric
5.自簽名證書crt到cer格式轉化
openssl x509 -in nginx.crt -out misite.cer -outform der
6.flask-RESTFul 接收數組參數
orderPostParser.add_argument('validate_car_images[]', type=unicode, required=True, help='驗車照片不能為空', action='append') #重點在action='append'