購買服務器
# 購買阿里雲服務器
# 短期或是測試使用,創建 按量收費 服務器,可以隨時刪除,刪除后不再計費,但要保證賬戶余額100元以上
連接服務器
1)賬號
>: ssh root@39.98.154.56
2)密碼
>: ********
服務器命令
管理員權限
1)以下所有的服務器命令均可以在管理員權限下執行
>: sudo 命令
配置終端
1)編輯配置文件
>: vim ~/.bash_profile
2)將原來內容全部刪除掉
>: ggdG
3)進入編輯狀態:填入下方兩行
>: i
export PATH=$PATH:$HOME/bin
PS1='Path:\w\n>:'
4)退出編輯狀態
>: esc
5)保存修改並退出
>: :wq
6)生效配置
>: source ~/.bash_profile
重要
更新系統軟件包
>: yum update -y
安裝軟件管理包和可能使用的依賴
>: yum -y groupinstall "Development tools"
>: yum install openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel psmisc libffi-devel
安裝Mysql
1)前往用戶根目錄
>: cd ~
2)下載mysql57
>: wget http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
也可以本地上傳,這條命令要在本地終端上執行
>: scp -r C:\Users\dell\Desktop\pkg\mysql57-community-release-el7-10.noarch.rpm root@39.98.154.56:~
3)安裝mysql57
>: yum -y install mysql57-community-release-el7-10.noarch.rpm
>: yum -y install mysql-community-server
4)啟動mysql57並查看啟動狀態
>: systemctl start mysqld.service
>: systemctl status mysqld.service
5)查看默認密碼並登錄
>: grep "password" /var/log/mysqld.log
>: mysql -uroot -p
6)修改密碼
>: ALTER USER 'root'@'localhost' IDENTIFIED BY 'new password';
>: ALTER USER 'root'@'localhost' IDENTIFIED BY 'Owen1234?';
安裝Redis
1)前往用戶根目錄
>: cd ~
2)下載redis-5.0.5
>: wget http://download.redis.io/releases/redis-5.0.5.tar.gz
>: scp -r C:\Users\dell\Desktop\pkg\redis-5.0.5.tar.gz root@39.98.154.56:~
3)解壓安裝包
>: tar -xf redis-5.0.5.tar.gz
4)進入目標文件
>: cd redis-5.0.5
5)編譯環境
>: make
6)復制環境到指定路徑完成安裝
>: cp -r ~/redis-5.0.5 /usr/local/redis
7)配置redis可以后台啟動:修改下方內容
>: vim /usr/local/redis/redis.conf
daemonize yes
8)完成配置修改
>: esc
>: :wq
9)建立軟連接
>: ln -s /usr/local/redis/src/redis-server /usr/bin/redis-server
>: ln -s /usr/local/redis/src/redis-cli /usr/bin/redis-cli
10)后台運行redis
>: redis-server &
ctrl + c
11)測試redis環境
>: redis-cli
ctrl + c
12)關閉redis服務
>: pkill -f redis -9
安裝Python3.6
1)前往用戶根目錄
>: cd ~
2)下載 或 上傳 Python3.6.7
>: wget https://www.python.org/ftp/python/3.6.7/Python-3.6.7.tar.xz
>: scp -r 本地Python-3.6.7.tar.xz ssh root@39.98.154.56:服務器路徑
>: scp -r C:\Users\dell\Desktop\pkg\Python-3.6.7.tar.xz ssh root@339.98.154.56~
3)解壓安裝包
>: tar -xf Python-3.6.7.tar.xz
4)進入目標文件
>: cd Python-3.6.7
5)配置安裝路徑:/usr/local/python3
>: ./configure --prefix=/usr/local/python3
6)編譯並安裝
>: make && sudo make install
7)建立軟連接:終端命令 python3,pip3
>: ln -s /usr/local/python3/bin/python3.6 /usr/bin/python3
>: ln -s /usr/local/python3/bin/pip3.6 /usr/bin/pip3
8)刪除安裝包與文件:
>: rm -rf Python-3.6.7
>: rm -rf Python-3.6.7.tar.xz
配置pip源:阿里雲不用配置,默認配置阿里源
1)創建pip配置路徑
>: mkdir ~/.pip
2)進入目錄編輯配置文件:填入下方內容
cd ~/.pip && vim pip.conf
[global]
index-url = http://pypi.douban.com/simple
[install]
use-mirrors =true
mirrors =http://pypi.douban.com/simple/
trusted-host =pypi.douban.com
安裝uwsgi
1)在真實環境下安裝
pip3 install uwsgi
2)建立軟連接
ln -s /usr/local/python3/bin/uwsgi /usr/bin/uwsgi
安裝虛擬環境
1)安裝依賴
>: pip3 install virtualenv
>: pip3 install virtualenvwrapper
2)建立虛擬環境軟連接
>: ln -s /usr/local/python3/bin/virtualenv /usr/bin/virtualenv
3)配置虛擬環境:填入下方內容
>: vim ~/.bash_profile
VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
source /usr/local/python3/bin/virtualenvwrapper.sh
4)退出編輯狀態
>: esc
5)保存修改並退出
>: :wq
6)更新配置文件內容
>: source ~/.bash_profile
7)虛擬環境默認根目錄:~/.virtualenvs
安裝Nginx
1)前往用戶根目錄
>: cd ~
2)下載nginx1.13.7
>: wget http://nginx.org/download/nginx-1.13.7.tar.gz
3)解壓安裝包
>: tar -xf nginx-1.13.7.tar.gz
4)進入目標文件
>: cd nginx-1.13.7
5)配置安裝路徑:/usr/local/nginx
>: ./configure --prefix=/usr/local/nginx
6)編譯並安裝
>: make && sudo make install
7)建立軟連接:終端命令 nginx
>: ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx
8)刪除安裝包與文件:
>: rm -rf nginx-1.13.7
>: rm -rf nginx-1.13.7.tar.xz
9)測試Nginx環境,服務器運行nginx,本地訪問服務器ip
>: nginx
>: 服務器綁定的域名 或 ip:80
Nginx命令
1)啟動
>: nginx
2)關閉nginx
>: nginx -s stop
3)重啟nginx
>: nginx -s reload
4)查看端口,強行關閉
>: ps -aux|grep nginx
>: kill <pid:進程編號>
路飛項目部署:Nginx + uwsgi + django + vue
配置前台項目
上線前配置
settings.js
base_url: 'http://39.98.154.56:8000', // 設置公網ip
上線
"""
1)本地項目打包,前往luffycity項目目錄下
>: cnpm run build
2)上傳
>: scp -r dist root@39.98.154.56:~
3)移動並重命名
mv ~/dist /home/html
4)去向Nginx配置目錄,備份配置,完全更新配置:填入下方內容
>: cd /usr/local/nginx/conf
>: cp nginx.conf nginx.conf.bak
>: vim nginx.conf
>: ggdG
>: i
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
server {
listen 80;
server_name 127.0.0.1; # 改為自己的域名,沒域名修改為127.0.0.1:80
charset utf-8;
location / {
root /home/html; # html訪問路徑
index index.html; # html文件名稱
try_files $uri $uri/ /index.html; # 解決單頁面應用刷新404問題
}
}
}
"""
路飛后台部署
上線前配置
prod.py:上線的配置文件,內容拷貝dev.py,前身就是settings.py
# 關閉測試環境
DEBUG = False
ALLOWED_HOSTS = [
'39.98.154.56' # 公網ip地址
]
CORS_ORIGIN_ALLOW_ALL = True # 允許所有跨域
# 靜態文件配置:上線后還有額外配置,見下方 后台樣式問題
STATIC_URL = '/static/'
# 后台http根路徑
# BASE_URL = 'http://127.0.0.1:8000'
BASE_URL = 'http://39.98.154.56:8000'
# 前台http根路徑
# LUFFY_URL = 'http://127.0.0.1:8080'
LUFFY_URL = 'http://39.98.154.56:80'
# 訂單支付成功的后台異步回調接口
NOTIFY_URL = BASE_URL + '/order/success'
# 訂單支付成功的前台同步回調接口
RETURN_URL = LUFFY_URL + '/pay/success'
wsgi.py 、manage.py建立一個副本manage_prod.py
# 需要做上線修改的內容
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'luffyapi.settings.prod')
項目環境
"""
本地導出項目環境:pip3 freeze > packages.txt
注:packages.txt中的
xadmin==2.0.1
要被替換為
https://codeload.github.com/sshwsfc/xadmin/zip/django2
上線導入項目環境:pip3 install -r packages.txt
"""
上線
"""
1)創建線上luffy項目虛擬環境
>: mkvirtualenv luffy
>: workon luffy
2)安裝所需環境,在packages.txt所在目錄下安裝執行packages.txt文件
>: pip install uwsgi
>: pip install -r packages.txt
3)項目根目錄配置uwsgi:填入下方內容
>: mkdir /home/project
# 注:將后台項目移至到/home/project,可以上傳,也可以git,項目設置公開(私有項目需要配置ssl)
>: cd /home/project && git clone https://gitee.com/doctor_owen/luffyapi.git
>: vim /home/project/luffyapi/luffyapi.xml
<uwsgi>
<socket>127.0.0.1:8808</socket> <!-- 內部端口,自定義 -->
<chdir>/home/project/luffyapi/</chdir> <!-- 項目路徑 -->
<module>luffyapi.wsgi</module> <!-- luffyapi為wsgi.py所在目錄名-->
<processes>4</processes> <!-- 進程數 -->
<daemonize>uwsgi.log</daemonize> <!-- 日志文件 -->
</uwsgi>
4)去向Nginx配置目錄,備份配置,完全更新配置:填入下方內容
>: vim /usr/local/nginx/conf/nginx.conf
注:在原來基礎上添加一個server
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
server {
listen 80;
server_name 127.0.0.1; # 改為自己的域名,沒域名修改為127.0.0.1:80
charset utf-8;
location / {
root /home/html; # html訪問路徑
index index.html; # html文件名稱
try_files $uri $uri/ /index.html; # 解決單頁面應用刷新404問題
}
}
# 新增的server
server {
listen 8000;
server_name 127.0.0.1; # 改為自己的域名,沒域名修改為127.0.0.1:80
charset utf-8;
location / {
include uwsgi_params;
uwsgi_pass 127.0.0.1:8808; # 端口要和uwsgi里配置的一樣
uwsgi_param UWSGI_SCRIPT luffyapi.wsgi; #wsgi.py所在的目錄名+.wsgi
uwsgi_param UWSGI_CHDIR /home/project/luffyapi/; # 項目路徑
}
}
}
5)完成服務器數據庫設置:見下方 數據庫設置
6)啟動uwsgi
>: uwsgi -x /home/project/luffyapi/luffyapi.xml
7)啟動nginx
>: nginx -s stop
>: nginx
>: nginx -s reload
7)瀏覽器測試:http://39.98.154.56:8000/xadmin
8)關閉uwsgi所有進程
>: pkill -f uwsgi -9
"""
后台樣式問題
設置文件中配置STATIC_ROOT
# >: vim /home/project/luffyapi/luffyapi/settings/prod.py
# 在STATIC_URL下方再添加兩句
STATIC_URL = '/static/'
STATIC_ROOT = '/home/project/luffyapi/luffyapi/static' # 服務器的絕對路徑
STATICFILES_DIRS = (os.path.join(BASE_DIR, "static"),)
# >: esc
# >: :wq
遷移靜態樣式:項目目錄下
可能錯誤
>: mkdir /home/project/luffyapi/luffyapi/static
>: python /home/project/luffyapi/manage_prod.py collectstatic
Nginx配置靜態路徑
>: vim /usr/local/nginx/conf/nginx.conf
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
server {
listen 8000;
server_name 127.0.0.1; # 改為自己的域名,沒域名修改為127.0.0.1:80
charset utf-8;
location / {
include uwsgi_params;
uwsgi_pass 127.0.0.1:8808; # 端口要和uwsgi里配置的一樣
uwsgi_param UWSGI_SCRIPT luffyapi.wsgi; #wsgi.py所在的目錄名+.wsgi
uwsgi_param UWSGI_CHDIR /home/project/luffyapi/; # 項目路徑
}
# 新增的配置靜態文件
location /static {
alias /home/project/luffyapi/luffyapi/static;
}
}
server {
listen 80;
server_name 127.0.0.1; # 改為自己的域名,沒域名修改為127.0.0.1:80
charset utf-8;
location / {
root /home/html; # html訪問路徑
index index.html; # html文件名稱
try_files $uri $uri/ /index.html; # 解決單頁面應用刷新404問題
}
}
}
>: esc
>: :wq
重啟服務
>: pkill -f uwsgi -9
>: uwsgi -x /home/project/luffyapi/luffyapi.xml
>: nginx -s reload
數據庫設置 + django2.0源碼(2.0.7不用修改源碼)
1.管理員連接數據庫
>: mysql -uroot -pOwen1234?
2.創建數據庫
>: create database luffy default charset=utf8;
# 3.設置權限賬號密碼
# 擁有公網或局域網,其他主機連mysql
>: grant all privileges on luffy.* to 'luffy'@'%' identified by 'Luffy123?';
# 要是本機連mysql連不上,再增加localhost域,本機就可以登錄了
>: grant all privileges on luffy.* to 'luffy'@'localhost' identified by 'Luffy123?';
# 設置完有權限限制的賬號后一定要刷新權限
>: flush privileges;
4.退出mysql
quit;
5.修改 prod.py | manage.py
>: vim /home/project/luffyapi/luffyapi/settings/prod.py
"PASSWORD": "Luffy123?"
>: vim /home/project/luffyapi/manage.py
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'luffyapi.settings.prod')
6.源碼修改,在Django 2.0.7下忽略
>: vim /root/.virtualenvs/luffy/lib/python3.6/site-packages/django/db/backends/mysql/base.py
# 36,37行注釋掉
>: vim /root/.virtualenvs/luffy/lib/python3.6/site-packages/django/db/backends/mysql/operations.py
# 146行添加
query = query.encode()
7.數據庫遷移
>: cd /home/project/luffyapi/
>: python manage_prod.py migrate
8.創建超級用戶
>: python manage_prod.py createsuperuser
# 賬號密碼:admin|admin
添加測試數據
>: mysql -uluffy -pLuffy123?
>: use luffy
INSERT INTO luffy_teacher(id, orders, is_show, is_delete, created_time, updated_time, name, role, title, signature, image, brief) VALUES (1, 1, 1, 0, '2019-07-14 13:44:19.661327', '2019-07-14 13:46:54.246271', 'Alex', 1, '老男孩Python教學總監', '金角大王', 'teacher/alex_icon.png', '老男孩教育CTO & CO-FOUNDER 國內知名PYTHON語言推廣者 51CTO學院2016\2017年度最受學員喜愛10大講師之一 多款開源軟件作者 曾任職公安部、飛信、中金公司、NOKIA中國研究院、華爾街英語、ADVENT、汽車之家等公司');
INSERT INTO luffy_teacher(id, orders, is_show, is_delete, created_time, updated_time, name, role, title, signature, image, brief) VALUES (2, 2, 1, 0, '2019-07-14 13:45:25.092902', '2019-07-14 13:45:25.092936', 'Mjj', 0, '前美團前端項目組架構師', NULL, 'teacher/mjj_icon.png', '是馬JJ老師, 一個集美貌與才華於一身的男人,搞過幾年IOS,又轉了前端開發幾年,曾就職於美團網任高級前端開發,后來因為不同意王興(美團老板)的戰略布局而出家做老師去了,有豐富的教學經驗,開起車來也毫不含糊。一直專注在前端的前沿技術領域。同時,愛好抽煙、喝酒、燙頭(錫紙燙)。 我的最愛是前端,因為前端妹子多。');
INSERT INTO luffy_teacher(id, orders, is_show, is_delete, created_time, updated_time, name, role, title, signature, image, brief) VALUES (3, 3, 1, 0, '2019-07-14 13:46:21.997846', '2019-07-14 13:46:21.997880', 'Lyy', 0, '老男孩Linux學科帶頭人', NULL, 'teacher/lyy_icon.png', 'Linux運維技術專家,老男孩Linux金牌講師,講課風趣幽默、深入淺出、聲音洪亮到爆炸');
INSERT INTO luffy_course_category(id, orders, is_show, is_delete, created_time, updated_time, name) VALUES (1, 1, 1, 0, '2019-07-14 13:40:58.690413', '2019-07-14 13:40:58.690477', 'Python');
INSERT INTO luffy_course_category(id, orders, is_show, is_delete, created_time, updated_time, name) VALUES (2, 2, 1, 0, '2019-07-14 13:41:08.249735', '2019-07-14 13:41:08.249817', 'Linux');
INSERT INTO luffy_course(id, orders, is_show, is_delete, created_time, updated_time, name, course_img, course_type, brief, level, pub_date, period, attachment_path, status, students, sections, pub_sections, price, course_category_id, teacher_id) VALUES (1, 1, 1, 0, '2019-07-14 13:54:33.095201', '2019-07-14 13:54:33.095238', 'Python開發21天入門', 'courses/alex_python.png', 0, 'Python從入門到入土&&&Python從入門到入土&&&Python從入門到入土&&&Python從入門到入土&&&Python從入門到入土&&&Python從入門到入土&&&Python從入門到入土&&&Python從入門到入土&&&Python從入門到入土&&&Python從入門到入土&&&Python從入門到入土&&&Python從入門到入土', 0, '2019-07-14', 21, '', 0, 231, 120, 120, 0.00, 1, 1);
INSERT INTO luffy_course(id, orders, is_show, is_delete, created_time, updated_time, name, course_img, course_type, brief, level, pub_date, period, attachment_path, status, students, sections, pub_sections, price, course_category_id, teacher_id) VALUES (2, 2, 1, 0, '2019-07-14 13:56:05.051103', '2019-07-14 13:56:05.051142', 'Python項目實戰', 'courses/mjj_python.png', 0, '', 1, '2019-07-14', 30, '', 0, 340, 120, 120, 99.00, 1, 2);
INSERT INTO luffy_course(id, orders, is_show, is_delete, created_time, updated_time, name, course_img, course_type, brief, level, pub_date, period, attachment_path, status, students, sections, pub_sections, price, course_category_id, teacher_id) VALUES (3, 3, 1, 0, '2019-07-14 13:57:21.190053', '2019-07-14 13:57:21.190095', 'Linux系統基礎5周入門精講', 'courses/lyy_linux.png', 0, '', 0, '2019-07-14', 25, '', 0, 219, 100, 100, 39.00, 2, 3);
INSERT INTO luffy_course_chapter(id, orders, is_show, is_delete, created_time, updated_time, chapter, name, summary, pub_date, course_id) VALUES (1, 1, 1, 0, '2019-07-14 13:58:34.867005', '2019-07-14 14:00:58.276541', 1, '計算機原理', '', '2019-07-14', 1);
INSERT INTO luffy_course_chapter(id, orders, is_show, is_delete, created_time, updated_time, chapter, name, summary, pub_date, course_id) VALUES (2, 2, 1, 0, '2019-07-14 13:58:48.051543', '2019-07-14 14:01:22.024206', 2, '環境搭建', '', '2019-07-14', 1);
INSERT INTO luffy_course_chapter(id, orders, is_show, is_delete, created_time, updated_time, chapter, name, summary, pub_date, course_id) VALUES (3, 3, 1, 0, '2019-07-14 13:59:09.878183', '2019-07-14 14:01:40.048608', 1, '項目創建', '', '2019-07-14', 2);
INSERT INTO luffy_course_chapter(id, orders, is_show, is_delete, created_time, updated_time, chapter, name, summary, pub_date, course_id) VALUES (4, 4, 1, 0, '2019-07-14 13:59:37.448626', '2019-07-14 14:01:58.709652', 1, 'Linux環境創建', '', '2019-07-14', 3);
INSERT INTO luffy_course_Section(id, is_show, is_delete, created_time, updated_time, name, orders, section_type, section_link, duration, pub_date, free_trail, chapter_id) VALUES (1, 1, 0, '2019-07-14 14:02:33.779098', '2019-07-14 14:02:33.779135', '計算機原理上', 1, 2, NULL, NULL, '2019-07-14 14:02:33.779193', 1, 1);
INSERT INTO luffy_course_Section(id, is_show, is_delete, created_time, updated_time, name, orders, section_type, section_link, duration, pub_date, free_trail, chapter_id) VALUES (2, 1, 0, '2019-07-14 14:02:56.657134', '2019-07-14 14:02:56.657173', '計算機原理下', 2, 2, NULL, NULL, '2019-07-14 14:02:56.657227', 1, 1);
INSERT INTO luffy_course_Section(id, is_show, is_delete, created_time, updated_time, name, orders, section_type, section_link, duration, pub_date, free_trail, chapter_id) VALUES (3, 1, 0, '2019-07-14 14:03:20.493324', '2019-07-14 14:03:52.329394', '環境搭建上', 1, 2, NULL, NULL, '2019-07-14 14:03:20.493420', 0, 2);
INSERT INTO luffy_course_Section(id, is_show, is_delete, created_time, updated_time, name, orders, section_type, section_link, duration, pub_date, free_trail, chapter_id) VALUES (4, 1, 0, '2019-07-14 14:03:36.472742', '2019-07-14 14:03:36.472779', '環境搭建下', 2, 2, NULL, NULL, '2019-07-14 14:03:36.472831', 0, 2);
INSERT INTO luffy_course_Section(id, is_show, is_delete, created_time, updated_time, name, orders, section_type, section_link, duration, pub_date, free_trail, chapter_id) VALUES (5, 1, 0, '2019-07-14 14:04:19.338153', '2019-07-14 14:04:19.338192', 'web項目的創建', 1, 2, NULL, NULL, '2019-07-14 14:04:19.338252', 1, 3);
INSERT INTO luffy_course_Section(id, is_show, is_delete, created_time, updated_time, name, orders, section_type, section_link, duration, pub_date, free_trail, chapter_id) VALUES (6, 1, 0, '2019-07-14 14:04:52.895855', '2019-07-14 14:04:52.895890', 'Linux的環境搭建', 1, 2, NULL, NULL, '2019-07-14 14:04:52.895942', 1, 4);
重點 重點 重點
# 1、真實環境和虛擬環境都要安裝uwsgi,將真實環境下的uwsgi建立軟連接
# 2、redis服務一定要后台啟動:redis-server &
# 3、uwsgi啟動django項目一定要進入虛擬環境下,因為環境都是安裝在虛擬環境中
# 4、服務器的日志都會被記錄在於uwsgi配置文件 luffyapi.xml 同類目下的 uwsgi.log 中