up line
阿新 • • 發佈:2020-07-31
目錄
- 安裝軟體前準備
- AliYun Ecs伺服器安裝Redis
- AliYun Ecs伺服器安裝MySql
- AliYun Ecs伺服器安裝Python
- AliYun Ecs伺服器安裝Nginx
- Nginx轉發前端專案靜態檔案
- AliYun Ecs伺服器安裝uwsgi
- AliYun Ecs伺服器建立虛擬環境
- AliYun Ecs伺服器轉發動態請求、django專案(使用nginx、uwsgi)
- 資料庫插入資料
- 後臺樣式
- 向上專案測試
安裝軟體前準備
#更新系統軟體包 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 #檢測是否安裝成功,輸入git測試有反應即可,看圖1
圖1
AliYun Ecs伺服器安裝Redis
#cd 進入家目錄 #安裝redis-5.0.5 wget http://download.redis.io/releases/redis-5.0.5.tar.gz #解壓安裝包 tar -xf redis-5.0.5.tar.gz #進入解壓檔案 cd redis-5.0.5 #編譯環境 make #複製環境到指定路徑完成安裝 cp -r ~/redis-5.0.5 /usr/local/redis #建立軟連線(相當於windows快捷方式) ln -s /usr/local/redis/src/redis-server /usr/bin/redis-server ln -s /usr/local/redis/src/redis-cli /usr/bin/redis-cli #後臺執行redis cd /usr/local/redis redis-server & #測試redis環境 redis-cli crtl + c可以停止 #關閉redis服務 pkill -f redis -9
AliYun Ecs伺服器安裝MySql
#cd 進入家目錄 #下載mysql 5.7版本 wget http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm #安裝 yum -y install mysql57-community-release-el7-10.noarch.rpm yum -y install mysql-community-server #啟動mysql 5.7並檢視啟動狀態 systemctl start mysqld.service systemctl status mysqld.service #檢視預設密碼(預設密碼不能操作資料庫) grep "password" /var/log/mysqld.log #登入並修改密碼,新密碼需要符合密碼策略,檢視密碼策略SHOW VARIABLES LIKE 'validate_password%'; mysql -uroot -p預設密碼 ALTER USER 'root'@'localhost' IDENTIFIED BY '新密碼'; #我的是ABC123456 #上一步執行不了使用忘記密碼把,看這裡。。。
AliYun Ecs伺服器安裝Python
#cd 進入家目錄
#下載Python3.6.6
wget https://www.python.org/ftp/python/3.6.6/Python-3.6.6.tar.xz
#解壓安裝包
tar -xf Python-3.6.6.tar.xz
#進入解壓包
cd Python-3.6.6
#配置安裝路徑到/usr/local/python3
./configure --prefix=/usr/local/python3
#編譯並安裝
make && sudo make install
##建立軟連線(相當於windows快捷方式),終端下能用python3,pip3
ln -s /usr/local/python3/bin/python3.6 /usr/bin/python3
ln -s /usr/local/python3/bin/pip3.6 /usr/bin/pip3
#可以選擇刪除安裝包與解壓包
rm -rf Python-3.6.7
rm -rf Python-3.6.7.tar.xz
AliYun Ecs伺服器安裝Nginx
#cd 進入家目錄
#下載niginx 1.13.7
wget http://nginx.org/download/nginx-1.13.7.tar.gz
#解壓安裝包
tar -xf nginx-1.13.7.tar.gz
#進入解壓包
cd nginx-1.13.7
#配置安裝路徑到/usr/local/nginx
./configure --prefix=/usr/local/nginx
#編譯並安裝
make && sudo make install
#建立軟連線(相當於windows快捷方式),終端下能用nginx
ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx
#可以選擇刪除安裝包與解壓包
rm -rf nginx-1.13.7
rm -rf nginx-1.13.7.tar.xz
#終端下輸入nginx啟動nginx,瀏覽器訪問伺服器公網ip(或繫結的域名)埠80
nginx
39.97.209.187
#nginx命令
啟動 nginx
關閉 nginx -s stop
重啟 nginx -s reload
檢視埠,強行關閉
ps -aux|grep nginx
kill <pid:程序編號>
#nginx介紹
Nginx是一款輕量級的Web 伺服器/反向代理伺服器及電子郵件(IMAP/POP3)代理伺服器,在BSD-like 協議下發行。其特點是佔有記憶體少,併發能力強,事實上nginx的併發能力在同類型的網頁伺服器中表現較好,中國大陸使用nginx網站使用者有:百度、京東、新浪、網易、騰訊、淘寶等。
作用:
http請求轉發
反向代理伺服器
負載均衡
動靜分離
Nginx轉發前端專案靜態檔案
#上線前配置,src\assets\js\settings.js
base_url: 'http://39.97.209.187:8000', // 改成自己阿里雲伺服器官網ip的8000埠
#前端專案打包,pycharm終端下執行
cnpm run build
#利用finalshell上傳到伺服器家目錄下
#伺服器操作,移動並重命名上傳的dist檔案
mv ~/dist /home/html
#到nginx配置目錄,備份配置,替換下面內容
cd /usr/local/nginx/conf
mv nginx.conf nginx.conf.bak
vim nginx.conf #按i或insert插入下面的內容,完成後按esc,:wq儲存並退出
替換的內容
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/dist; # html訪問路徑
index index.html; # html檔名稱
try_files $uri $uri/ /index.html; # 解決單頁面應用重新整理404問題
}
}
}
#重啟nginx
nginx -s reload
#http://39.97.209.187/就可以訪問前端專案了,看圖1
圖1
AliYun Ecs伺服器安裝uwsgi
#原生環境下安裝
pip3 install uwsgi
# 建立軟連線(相當於windows快捷方式)
ln -s /usr/local/bin/uwsgi /usr/bin/uwsgi
AliYun Ecs伺服器建立虛擬環境
#安裝依賴
pip3 install virtualenv
pip3 install virtualenvwrapper
#建立軟連線(相當於windows快捷方式)
ln -s /usr/local/python3/bin/virtualenv /usr/bin/virtualenv
#配置虛擬環境,填入下面內容,填入內容後按esc,:wq儲存並退出,
vim ~/.bash_profile
填入內容,看圖1
VIRTUALENVWRAPPER_PYTHON=/usr/local/python3/bin/python3
source /usr/local/python3/bin/virtualenvwrapper.sh
#檢視下修改好的neir
cat ~/.bash_profile
#更新配置檔案內容
source ~/.bash_profile
#虛擬環境預設的根目錄
cd ~/.virtualenvs
#建立虛擬環境
1 建立虛擬環境到配置的WORKON_HOME路徑下
mkvirtualenv -p python3 自定義的虛擬環境名稱(比如:luffy) 使用的是python3
mkvirtualenv -p python2 自定義的虛擬環境名稱(比如:luffy) 使用的是python2
2 檢視已有的虛擬環境
workon
3 使用虛擬環境
workon 已有的虛擬環境名稱(比如:luffy)
4 進入、退出該虛擬環境的Python環境
python、exit()
5 為虛擬環境安裝模組
pip install 模組名稱(比如:django==2.2)
6 退出當前虛擬環境
deactivate
7 刪除虛擬環境(刪除當前虛擬環境要先退出)
rmvirtualenv
圖1
AliYun Ecs伺服器轉發動態請求、django專案(使用nginx、uwsgi)
AliYun Ecs 伺服器操作
#mkdir /home/project
#cd /home/project
#git clone https://gitee.com/liuqingzheng/luffyapi.git #換成自己git倉庫的地址,clone後端程式碼
#進入虛擬環境,專案根目錄下安裝專案需要的依賴包,
workon luffy
cd /home/project/luffyapi
pip install -r requirements.txt
#pip install uwsgi #workon環境和原生環境都需要安裝,退出虛擬環境命令deactivate
#uwsgi服務配置,內容日下
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>
#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 80;
server_name 127.0.0.1; # 改為自己的域名,沒域名修改為127.0.0.1:80
charset utf-8;
location / {
root /home/html/dist; # 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/; # 專案路徑
}
}
}
#mysql資料庫root使用者登入,建立庫luffyapi,建立使用者,授權該使用者
mysql -uroot -p你的密碼
#建立庫
create database luffyapi default charset=utf8;
#設定許可權賬號密碼:賬號密碼要與專案中配置的一致
grant all privileges on luffyapi.* to 'luffyapi'@'%' identified by 'Luffy123?';
grant all privileges on luffyapi.* to 'luffyapi'@'localhost' identified by 'Luffy123?';
flush privileges; #更新許可權
#退出mysql
exit/quit
#到專案下執行資料遷移命令,並建立超級使用者
python manage_pro.py makemigrations
python manage_pro.py migrate
python manage_pro.py createsuperuser #我的joab [email protected] lqb666666
#啟動uwsgi、nginx,後端專案就起來了
#啟動uwsgi
uwsgi -x /home/project/luffyapi/luffyapi.xml
#重啟nginx
nginx -s reload
資料庫插入資料
-- 老師表
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);
後臺樣式
修改線上配置
#編輯線上配置檔案
vim /home/project/luffyapi/luffyapi/settings/pro.py
#修改static配置,新增STATIC_ROOT
STATIC_URL = '/static/'
STATIC_ROOT = '/home/project/luffyapi/luffyapi/static'
STATICFILES_DIRS = (os.path.join(BASE_DIR, "static"),)
#退出儲存即可
esc
:wq
遷移靜態樣式
#後端專案目錄下建立static
mkdir /home/project/luffyapi/luffyapi/static
#完成靜態檔案遷移
python /home/project/luffyapi/manage_pro.py collectstatic
Nginx配置靜態檔案路徑
#全部修改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 80;
server_name 127.0.0.1; # 改為自己的域名,沒域名修改為127.0.0.1:80
charset utf-8;
location / {
root /home/html/dist; # html訪問路徑
index index.html; # html檔名稱
try_files $uri $uri/ /index.html; # 解決單頁面應用重新整理404問題
}
}
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;
}
}
}
#退出儲存即可
esc
:wq
啟動服務
#關閉uwsgi服務,重啟uwsgi
pkill -f uwsgi -9
uwsgi -x /home/project/luffyapi/luffyapi.xml
#關閉nginx,重啟nginx
nginx -s stop
nginx
向上專案測試
#本地瀏覽器訪問xadmin後臺
http://39.99.192.127:8000/xadmin
#登入,錄入測試資料
賬號密碼:joab|lqb666666
#或是匯出本地資料庫為sql,再在線上匯入sql
>: mysql -h 39.99.192.127 -P 3306 -u luffy -p
>: Luffy123?
>: use luffy
>: 複製之前的資料備份