1、Linux命令:
查看服務器的版本: cat /etc/redhat-release
clear清屏
2、更新或者安裝依賴庫:都是使用yum進行安裝
yum install gcc-c++
yum install wget openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel
yum install libxml*
3、安裝Python3:
a) 下載:wget https://www.python.org/ftp/python/3.6.3/Python-3.6.3.tgz 下載到當前目錄
b) 解壓:tar -zxvf Python-3.6.3.tgz
c) 預編譯: ./configure --prefix=/usr/local/python3
d) 編譯並安裝: make && make install
e) 創建一個python3和pip3的軟鏈接,方便使用python3和pip3進行操作
ln -s /usr/local/python3/bin/python3.6 /usr/bin/python3
ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3
f) 可以去修改pip3的源:在家目錄下的.pip文件夾如果就創建一個,在創建pip.conf修改里面的內容,這是永久修改,
也可以做一次性修改:
pip3 install packagename -i http://pypi.douban.com/simple
4、安裝Django(框架)和uWSGI(web服務器)
pip3 install Django==2.0
pip3 install uwsgi
給uwsgi創建一個軟鏈接,方便操作
ln -s /usr/local/python3/bin/uwsgi /usr/bin/uwsgi3
5、編寫啟動uWSGI服務器的配置文件(當然也可以直接使用uWSGI的命令)
a)可以使用 xml配置文件進行啟動,啟動命令是uwsgi3 -x crm.xml
<uwsgi>
<socket>127.0.0.1:8000</socket><!-- 內部端口,自定義 -->
<chdir>/shsxt/projects/crm</chdir><!-- 項目路徑 -->
<module>crm.wsgi</module><!--wsgi模塊位置 -->
<master>True</master><!--wsgi主進程-->
<pidfile>crm-master.pid</pidfile><!--進程號-->
<processes>4</processes> <!-- 進程數 -->
<daemonize>uwsgi.log</daemonize><!-- 日志文件 daemonize守護進程-->
</uwsgi>
b)可以使用crm.ini配置文件進行啟動 啟動命令是uwsgi3 crm.ini即可
[uwsgi]
socket = 127.0.0.1:3031
chdir = /home/foobar/myproject/
wsgi-file = myproject/wsgi.py
processes = 4
threads = 2
stats = 127.0.0.1:9191 # 這是一個狀態監控
c)直接使用命令:uwsgi --socket 127.0.0.1:3031 --chdir /home/foobar/myproject/ --wsgi-file myproject/wsgi.py --master --processes 4 --threads 2 --stats 127.0.0.1:9191
6、把項目copy到服務器上,啟動uwsgi服務器
a)在項目根目錄下添加crm.xml文件,注意的是項目存放路徑
b) cd到項目,執行uwsgi3 -x crm.xml 啟動uWSGI服務器
c) 關閉服務器 uwsgi3 --stop/reload crm-master.pid
7、安裝mysql: yum list|grep mysql-community-server查看當前mysql的安裝包有沒有,如果有執行yum install -y mysql-community-server即可
如果沒有:先下載一個rpm的包,然后安裝到yum源中,在執行yum install
wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm #這里安裝的mysql6 默認密碼是空
rpm -ivh mysql-community-release-el7-5.noarch.rpm
yum install mysql-server mysql-devel mysql其中mysql-server是mysql服務器,mysql是客戶端,devel就是一些開發依賴包
啟動mysql:
service mysqld start 如果是centos7的話執行systemctl start mysqld.service
mysql56操作:
a) 使用mysql客戶端登錄數據庫:mysql -uroot -p 進行登錄 密碼是空,或者直接mysql登錄
b) 切換到mysql數據庫 use mysql
c)修改host為%,方便外網進行訪問:update user set host = '%' where host='127.0.0.1' and user = 'root';
d) 修改密碼:update user set password = PASSWORD('123456') where host = '%';
e) 修改完后記得刷新權限或者重啟mysql服務器
刷新權限:flush privileges;
重啟服務器:退出mysql客戶端,然后執行service mysqld restart或者c7里面 systemctl restart mysqld.service
8、安裝nginx
1、下載wget http://nginx.org/download/nginx-1.10.3.tar.gz
2、解壓 tar -zxvf nginx-1.10.3.tar.gz
3、預編譯 cd nginx-1.10.3 執行./configure --prefix=/usr/local/nginx
4、安裝 make && make install
5、修改配置 vim /usr/local/nginx/conf ,將user改成root或者登陸賬號
6、啟動 cd /usr/local/nginx/sbin下執行./nginx 就啟動nginx
7、在瀏覽器訪問ip:80,如果訪問失敗,那么把防火牆關閉 centos6下:service iptables stop/status centos7下:systemctl stop firewalld.service
9、生成環境軟件:
1、Python3的環境
2、web服務器 uWSGI
3、MySQL數據庫
4、Django開發框架
5、Nginx請求轉發
上線項目准備:
1.修改settings的相關配置DBUG=FALSE ALLOWED_HOSTS=['*'] 數據庫相關配置
2.確定下uWSGI的配置
3.注意依賴模塊的安裝比如安裝pip3 install pymysql
10、修改nginx的配置,讓其轉發到127.0.0.1:8000上,配置完后,重啟nginx: ./nginx -s reload
server {
listen 80; #暴露給外部訪問的端口
server_name localhost;
charset utf-8;
location / {
include uwsgi_params;
uwsgi_pass 127.0.0.1:8000; #外部訪問80就轉發到內部8000
}
location /static/ {
alias /root/shsxt/projects/crm/static/; #項目靜態路徑設置
}
}
11、注意:生成項目的依賴庫文件requirements.txt
a) pip3 freeze > requirements.txt, 會把解析器下所有的第三方模塊全局展示,一般會結合virtual env使用
b) 使用pipreqs 模塊進行生成,但有個缺點會有模塊丟失的可能
pip3 install pipreqs
pipreqs -h 查看幫助文檔
pipreqs --use-local ./ --encoding utf-8 就會在當前目錄下生成requirements.txt文件
12、安裝requirements.txt文件
pip3 install -r requirements.txt
總結:
1、線上部署Django項目的網絡架構,使用的是uWSGI作為web服務器,(Django自帶也有一個python3 manager.py runserver),
使用Nginx作為反向代理的服務器,使用MySQL作為數據庫存儲
2、先安裝python3(安裝完后設置一個軟鏈),在安裝uWSGI服務器(配置一個軟鏈),再安裝數據MySQL,在安裝Nginx服務器
3、配置:
a) uWSGI的啟動配置,可以ini, xml還可以直接命令
<uwsgi>
<socket>127.0.0.1:8000</socket><!-- 內部端口,自定義 -->
<chdir>/shsxt/projects/crm</chdir><!-- 項目路徑 -->
<module>crm.wsgi</module><!--wsgi模塊位置 -->
<master>True</master><!--wsgi主進程-->
<pidfile>crm-master.pid</pidfile><!--進程號-->
<processes>4</processes> <!-- 進程數 -->
<daemonize>uwsgi.log</daemonize><!-- 日志文件 daemonize守護進程-->
</uwsgi>
b) nginx服務器的配置,修改用戶,在加入uWSGI的相關配置
server {
listen 80; #暴露給外部訪問的端口
server_name localhost;
charset utf-8;
location / {
include uwsgi_params;
uwsgi_pass 127.0.0.1:8000; #外部訪問80就轉發到內部8000
}
location /static/ {
alias /root/shsxt/projects/crm/static/; #項目靜態路徑設置
}
}
c) MySQL的賬號和密碼的設置:如果是初次安裝mysql后,需要修改賬號的host和配置密碼
使用MySQL的客戶端mysql 登錄進入mysql服務端,然后進入mysql數據庫use mysql; 查看賬號 select host, user, password from user;
找到其中的某個賬號進行改動:update user set host = '%', password = PASSWORD('123456') where host = '127.0.0.1' and user='root';
修改完后,需要刷新權限:flush privileges; 這就可以遠程登錄,創建一個數據庫,然后把對應的數據庫表拷貝過去。
4、上傳代碼的准備工作:修改settings.py中一些配置:DEBUG = False, ALLOWED_HOSTS=['*'], 還要修改其他的軟件環境配置比如數據庫,還要生成requirements.txt 依賴庫的清單
5、上傳完后,直接在項目根路徑下找到對應的requirements.txt, 直接執行pip3 install -r requirements.txt, 安裝對應的依賴包
6、啟動服務器:
a) 啟動uWSGI服務器:uwsgi -x crm.xml或者uwsgi (--ini) crm.ini
b) 啟動Nginx服務器: ./nginx
7、訪問:域名訪問或者使用ip地址訪問