環(huán)境準備
發(fā)布CRM你將使用以下軟件
nginx
uWSGI
CentOS7
CRM項目文件
virtualenv/virtualenvwrapper
supervisor
WSGI、uWSGI
python web服務器開發(fā)使用WSGI協(xié)議(Web Server Gateway Interface)
python web項目默認會生成一個wsgi.py文件,確定好應用模塊。
生產環(huán)境中使用的是uWSGI,實現(xiàn)了WSGI所有接口,C語言編寫,效率很高的web服務器。
uWSGI是一個全功能的HTTP服務器,實現(xiàn)了WSGI協(xié)議、uwsgi協(xié)議、http協(xié)議等。它要做的就是把HTTP協(xié)議轉化成語言支持的網絡協(xié)議。比如把HTTP協(xié)議轉化成WSGI協(xié)議,讓Python可以直接使用。
Nginx
使用nginx是為了它的反向代理功能,項目會通過Django+uWSGI+Nginx進行服務器線上部署。
CentOS
1.打包項目CRM文件夾,壓縮文件
2.通過xftp、scp、lrzsz等上傳文件至Centos服務器
Linux使用技巧
1.通過xshell或者iTerm等軟件,多終端作你的linxu,這樣對uwsgi、nginx、項目代碼調試的時候,避免來回切換目錄,提供工作效率。
2.注意修改了linux軟件的配置文件,都要重啟服務才能生效。
Virtualenv/Virtualenvwrapper
構建一個干凈,隔離的python解釋器環(huán)境,防止軟件依賴,沖突等問題,建議使用。
Supervisor
Supervisor(http://supervisord.org/)是用Python開發(fā)的一個client/server服務,是Linux/Unix系統(tǒng)下的一個進程管理工具,不支持Windows系統(tǒng)。它可以很方便的監(jiān)聽、啟動、停止、重啟一個或多個進程。用Supervisor管理的進程,當一個進程意外被殺死,supervisort監(jiān)聽到進程死后,會自動將它重新拉起,很方便的做到進程自動恢復的功能,不再需要自己寫shell腳本來控制。
nginx部署python程序
1.在進行項目部署的時候,如果報錯
no application not found
就是因為你的uwsgi沒找到django的wsgi.py應用文件
2.為什么要用nginx uwsgi
因為用戶只想訪問域名,不帶有任何端口
通過nginx反向代理,用戶直接訪問 chiji.com,但是nginx直接轉發(fā)給了django,我們其實看到的頁面是django
uwsgi是支持并發(fā)的,python web服務器,讓你的django并發(fā)性更高,但是uwsgi不支持靜態(tài)文件的處理,靜態(tài)文件會丟失
用nginx處理靜態(tài)文件,uwsgi處理動態(tài)請求
3.項目部署實驗步驟
####
nginx
####
uwsgi+django
1.workon切換之前的虛擬環(huán)境或者創(chuàng)建新的虛擬環(huán)境(mkvirtualenv nginx_crm)且解決環(huán)境依賴
[root@localhost ~]# workon
[root@localhost ~]# workon myblog
(myblog) [root@localhost ~]# pip3 list
2.在虛擬環(huán)境下安裝uwsgi
(myblog) [root@localhost ~]# pip3 install uwsgi
(myblog) [root@localhost ~]# which uwsgi
/root/Envs/myblog/bin/uwsgi
(myblog) [root@localhost ~]# uwsgi --version
2.0.18
3.用uwsgi啟動一個python文件:
uwsgi --http :8000 --wsgi-file test.py
http :8000: 使用http協(xié)議,端口8000
wsgi-file test.py: 加載指定的文件,test.py
#test.py
def application(env, start_response):
start_response('200 OK', [('Content-Type','text/html')])
return [b"Hello World"] # python3
#訪問:http://192.168.0.101:8000/
4.用uwsgi啟動一個項目(在虛擬環(huán)境下作):
#cd到目錄中,myblog/wsgi.py確保找到這個文件
(myblog) [root@localhost ~]# uwsgi --http :8000 --module myblog.wsgi --py-autoreload=1
module myblog.wsgi: 加載指定的wsgi模塊
--py-autoreload=1 熱加載命令(修改django代碼,uWSGI會自動加載django程序)
使用uwsgi配置文件去啟動項目:
1.手動創(chuàng)建uwsgi.ini 配置文件
(myblog) [root@localhost myblog]# ls
backend db.sqlite3 manage.py media myblog repository static templates utils web
(myblog) [root@localhost myblog]# touch uwsgi.ini
(myblog) [root@localhost myblog]# vim uwsgi.ini
#########################
# mysite_uwsgi.ini file
[uwsgi]
# Django-related settings
# the base directory (full path)
#指定django的項目目錄,第一層
chdir = /opt/myblog
# Django's wsgi file
#找到django的wsgi文件
#這里需要寫項目的第二層目錄myblog
module = myblog.wsgi
# the virtualenv (full path)
#填寫虛擬環(huán)境的絕對路徑
home = /root/Envs/myblog
# process-related settings
# master
master = true
# maximum number of worker processes
開啟進程數(shù)量
processes = 5
# the socket (use the full path to be safe
#指定socket協(xié)議,運行django,只能與nginx結合時使用
#指定socket協(xié)議,運行django,只能與nginx結合時使用
socket = 0.0.0.0:8000
#如果你沒用nginx,只想啟動一個http界面,用這個
#http = 0.0.0.0:8000
# ... with appropriate permissions - may be needed
# chmod-socket = 664
# clear environment on exit
vacuum = true
#########################
2.通過配置文件啟動uwsgi
(myblog) [root@localhost myblog]# ls
backend db.sqlite3 manage.py media myblog repository static templates utils web
(myblog) [root@localhost myblog]# uwsgi --ini uwsgi.ini
5.收集myblog的靜態(tài)文件
編輯myblog的settings.py配置文件
#定義django的靜態(tài)資源根目錄,便于用命令收集資源,存放的地兒
STATIC_ROOT="/opt/myblog_static"
[root@localhost myblog]# vim settings.py
加上STATIC_ROOT="/opt/myblog_static"
用命令收集靜態(tài)文件
[root@localhost myblog]# python3 manage.py collectstatic
6.配置nginx,反向代理django服務器,且解析靜態(tài)文件
proxy_pass僅僅是請求轉發(fā)的參數(shù),與uwsgi結合,還有更高級的協(xié)議參數(shù)
修改nginx配置文件如下:
http {
server {
listen 80;
#server_name myblog.com;
server_name localhost;
location / {
#使用uwsgi_pass轉發(fā)基于uwsgi協(xié)議的一個請求
include /opt/nginx112/conf/uwsgi_params;
uwsgi_pass 192.168.0.101:8000;
}
#配置一個url的入口,告訴django靜態(tài)文件去哪里找
#當請求靜態(tài)文件是就進行別名,nginx去/opt/myblog_static/下找,注意不要用tab鍵切換,敲空格
location /static{alias /opt/myblog_static/;}
}
}
7.此時nginx結合uwsgi已經完成
192.168.0.101:8000/訪問不到,避免了端口工具
192.168.0.101通過nginx的反向代理訪問
8.配置supervisor工具,管理django后臺【記住這里退出虛擬環(huán)境,使用物理環(huán)境去運行】
1.下載supervisor
[root@localhost ~]# easy_install supervisor
2.配置supervisor的配置文件,編寫django任務
(myblog) [root@localhost myblog]# deactivate
[root@localhost myblog]# echo_supervisord_conf > /etc/supervisor.conf
[root@localhost myblog]# vim /etc/supervisor.conf
最底行寫入:
[program:myblog]
command=/root/Envs/myblog/bin/uwsgi --ini /opt/myblog/uwsgi.ini ; #寫絕對路徑
autostart=true
stopasgroup=true
killasgroup=true
3.啟動supervisor服務端
[root@localhost myblog]# supervisord -c /etc/supervisor.conf
通過客戶端命令查看任務
[root@localhost myblog]# supervisorctl -c /etc/supervisor.conf
myblog RUNNING pid 21129, uptime 0:00:14
supervisor>
4.supervisor管理命令
[root@localhost myblog]# supervisorctl -c /etc/supervisor.conf
myblog RUNNING pid 21129, uptime 0:00:14
supervisor>
supervisor> stop myblog #停止所有任務
myblog: stopped
supervisor> start myblog #啟動所有任務
myblog: started
supervisor>
supervisor> status
myblog RUNNING pid 21284, uptime 0:01:17
supervisor>
其他:
找uwsgi的絕對路徑
(myblog) [root@localhost myblog]# which uwsgi
/root/Envs/myblog/bin/uwsgi
(myblog) [root@localhost myblog]# pwd
/opt/myblog
怎么查看虛擬環(huán)境絕對路徑:
(myblog) [root@localhost opt]# cdvirtualenv
(myblog) [root@localhost myblog]# pwd
/root/Envs/myblog
找uwsgi_params的路徑
[root@localhost myblog]# find / -name uwsgi_params
/opt/nginx-1.12.0/conf/uwsgi_params
/opt/nginx112/conf/uwsgi_params
[root@localhost myblog]#
nginx+uwsgi+路飛學城部署
1.單機本地測試運行方式,調用django第三方的wsgiref單機模塊,性能很低
python3 manage.py runserver 0.0.0.0:8000
2.使用uwsgi去啟動django項目,支持并發(fā)更多
3.準備前后端代碼
4.先從vue前端整起
1.解決node環(huán)境
2.更改vue發(fā)送請求的接口地址
這個vue發(fā)送的地址,應該是發(fā)送給nginx代理,然后代理再轉發(fā)請求給drf后臺
用以下命令,更改vue發(fā)送的接口地址
[root@localhost restful]# sed -i "s/127.0.0.1/192.168.0.101/g" /opt/luffy/07-luffy_project_01/src/restful/api.js
!!!待會要準備nginx的代理地址,如下:
!!!待會要準備nginx的代理地址,如下:
!!!待會要準備nginx的代理地址,如下:
192.168.0.101:8000
3.打包編譯vue靜態(tài)文件
[root@localhost 07-luffy_project_01]# ls
build config index.html package.json package-lock.json README.md src static
[root@localhost 07-luffy_project_01]# pwd
/opt/luffy/07-luffy_project_01
[root@localhost 07-luffy_project_01]# npm install
[root@localhost 07-luffy_project_01]# ls
build config index.html node_modules package.json package-lock.json README.md src static
[root@localhost 07-luffy_project_01]# npm run build
[root@localhost 07-luffy_project_01]# ls
build config dist index.html node_modules package.json package-lock.json README.md src static
4.生成的dist文件夾,就是路飛學城的靜態(tài)頁面,丟給nginx去返回即可
配置nginx.conf找到vue的靜態(tài)頁面
http {
server {
listen 80;
server_name luffy.com;
#server_name localhost;
location / {
root /opt/luffy/07-luffy_project_01/dist;
index index.html index.htm;
}
}
}
5.開始配置后端代碼,用uwsgi啟動luffy
1.新建虛擬環(huán)境
mkvirtualenv luffy
2.解決所需的依賴模塊,準備一個模塊版本文件:requirements.txt 這個文件可以手動創(chuàng)建寫入如下依賴
(luffy) [root@localhost opt]# cd /opt/luffy/luffy_boy
(luffy) [root@localhost luffy_boy]# touch requirements.txt
(luffy) [root@localhost luffy_boy]# vim requirements.txt
##############################
certifi==2018.11.29
chardet==3.0.4
crypto==1.4.1
Django==2.1.4
django-redis==4.10.0
django-rest-framework==0.1.0
djangorestframework==3.9.0
idna==2.8
Naked==0.1.31
pycrypto==2.6.1
pytz==2018.7
PyYAML==3.13
redis==3.0.1
requests==2.21.0
shellescape==3.4.1
urllib3==1.24.1
uWSGI==2.0.17.1
##############################
(luffy) [root@localhost luffy_boy]# ls
api db.sqlite3 keys luffy_boy manage.py requirements.txt static templates
3.安裝所需的依賴模塊
(luffy) [root@localhost luffy_boy]# pip3 install -i https://pypi.douban.com/simple -r requirements.txt
4.看一看
pip3 list
5.運行
python3 manage.py runserver 0.0.0.0:8000 (注意端口得是8000,因為前端的vue發(fā)的就是8000)
6.準備uwsgi,以及uwsgi.ini
# mysite_uwsgi.ini file
[uwsgi]
# Django-related settings
# the base directory (full path)
chdir = /opt/luffy/luffy_boy
# Django's wsgi file
module = luffy_boy.wsgi
# the virtualenv (full path)
home = /root/Envs/luffy
# process-related settings
# master
master = true
# maximum number of worker processes
processes = 5
# the socket (use the full path to be safe
socket = 0.0.0.0:9000
# ... with appropriate permissions - may be needed
# chmod-socket = 664
# clear environment on exit
vacuum = true
7.通過配置文件啟動uwsgi
(luffy) [root@localhost luffy_boy]# ls
! api db.sqlite3 keys luffy_boy manage.py requirements.txt static templates uwsgi.ini
(luffy) [root@localhost luffy_boy]# uwsgi --ini uwsgi.ini
8.配置nginx,反向代理django服務器,且解析靜態(tài)文件
#虛擬主機1的功能是web頁面返回
server {
listen 80;
server_name 192.168.0.101;
location / {
root /opt/luffy/07-luffy_project_01/dist;
index index.html index.htm;
}
}
#虛擬主機2的功能是反向代理,vue發(fā)送的代理地址是192.168.0.101:8000
server {
listen 8000;
server_name 192.168.0.101;
location / {
include /opt/nginx112/conf/uwsgi_params;
uwsgi_pass 192.168.0.101:9000;
}
}
#解釋:
當用戶發(fā)送請求給192.168.0.101時,默認走的是第一個虛擬主機,進行vue靜態(tài)頁面返回
然后在瀏覽器中點擊課程列表,vue發(fā)送請求給代理ip,(然后nginx就支持反向代理,再定義虛擬主機2,監(jiān)聽的地址是8000,
轉發(fā)的請求地址是后臺的uwsgi端口,可以是9000,也可以是9999,自定義)
9.修改項目的settings文件中的redis配置
CACHES = {
"default": {
"BACKEND": "django_redis.cache.RedisCache",
"LOCATION": "redis://192.168.0.101:6380",
"OPTIONS": {
"CLIENT_CLASS": "django_redis.client.DefaultClient",
"CONNECTION_POOL_KWARGS": {"max_connections": 100},
# "PASSWORD": "密碼",
"DECODE_RESPONSES":True
}
},
}
10修改項目代碼中/opt/luffy/luffy_boy/api/views/shoppingcart.py 的代碼
(更改redis數(shù)據(jù)庫連接的驅動,用django的驅動)
#REDIS_CONN = redis.Redis(decode_responses=True)改成下面的
REDIS_CONN = get_redis_connection()
11.redis的配置
(luffy) [root@localhost redis-4.0.10]# pwd
/opt/redis-4.0.10
(luffy) [root@localhost redis-4.0.10]# vim redis-6380.conf
復制如下代碼:
port 6380
daemonize yes
pidfile /data/6380/redis.pid
loglevel notice
logfile "/data/6380/redis.log"
dir /data/6380
requirepass 111111
appendonly yes
appendfsync everysec
protected-mode no
bind 192.168.0.101
12.啟動redis
(luffy) [root@localhost redis-4.0.10]# redis-server redis-6380.conf
(luffy) [root@localhost redis-4.0.10]# redis-cli -p 6380 -a 111111 -h 192.168.0.101
13.redis nginx uwsgi 確保都啟動了,alex alex3714登錄
其他:
配置兩個本地解析的域名
C:\Windows\System32\drivers\etc\hosts文件,寫入如下配置:
192.168.1.101 chiji.com
192.168.1.101 hanju.com
#如果打包不成功
1.更換網絡
2.在window中打包,生成dist文件夾后,發(fā)送給Linux
3.更換源,加速下載
命令:curl -I baidu.com
[root@localhost ~]# curl -I baidu.com
HTTP/1.1 200 OK
Date: Tue, 24 Sep 2019 19:24:44 GMT
Server: Apache
Last-Modified: Tue, 12 Jan 2010 13:48:00 GMT
ETag: "51-47cf7e6ee8400"
Accept-Ranges: bytes
Content-Length: 81
Cache-Control: max-age=86400
Expires: Wed, 25 Sep 2019 19:24:44 GMT
Connection: Keep-Alive
Content-Type: text/html
web服務器(nginx):接收HTTP請求(例如www.pythonav.cn/xiaocang.jpg)并返回數(shù)據(jù)
web服務器,僅僅就是接受一個web請求,返回一個磁盤上的靜態(tài)資源(jpg,mp4...)
錯誤碼
50x 服務器錯誤,django flask后臺蹦了
40x 客戶端錯誤,權限不足,資源不存在等等...
30x 資源重定向
20x 請求正確返回
技術棧:
貴:
Java+apache(web服務器,處理靜態(tài)資源)+oracle(數(shù)據(jù)庫)+tomcat(處理Java應用)+svn(代碼托管)+js+jQuery +redhat
為了省錢,切換開源技術棧
java + perl + python + nginx + mysql + git + js + centos
web框架(django,flask):開發(fā)web應用程序,處理接收到的數(shù)據(jù)
nginx安裝步驟,源碼編譯安裝(源碼編譯,開源自定制更多功能)
1.解決軟件正常運轉所需依賴包
yum install gcc patch libffi-devel python-devel zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel openssl openssl-devel -y
2.下載源代碼
[root@localhost ~]# cd /opt
[root@localhost opt]# wget -c https://nginx.org/download/nginx-1.12.0.tar.gz
3.解壓縮源碼
[root@localhost opt]# tar -zxvf nginx-1.12.0.tar.gz
4.進入源碼目錄,編譯安裝
[root@localhost opt]# cd nginx-1.12.0/
[root@localhost nginx-1.12.0]# ./configure --prefix=/opt/nginx112
[root@localhost nginx-1.12.0]# make && make install
5.進入Nginx功能目錄
cd /opt/nginx112
6.學習Nginx功能目錄
[root@localhost nginx112]# ls
conf 配置文件nginx.conf
html 存放前端頁面
logs 存放Nginx的運行日志,錯誤日志
sbin 存放Nginx可執(zhí)行程序的目錄
7.卸載原本的yum裝的Nginx
[root@localhost nginx112]# yum remove nginx -y
8.啟動
[root@localhost nginx112]# netstat -tunlp | grep 80
[root@localhost nginx112]# ps -ef|grep nginx
root 15678 13010 0 00:45 pts/3 00:00:00 grep --color=auto nginx
[root@localhost nginx112]# ls
conf html logs sbin
[root@localhost nginx112]# cd sbin
[root@localhost sbin]# ls
nginx
[root@localhost sbin]# ./nginx
[root@localhost sbin]# !ps
ps -ef|grep nginx
root 15682 1 0 00:47 ? 00:00:00 nginx: master process ./nginx
nobody 15683 15682 0 00:47 ? 00:00:00 nginx: worker process
root 15685 13010 0 00:47 pts/3 00:00:00 grep --color=auto nginx
[root@localhost sbin]#
[root@localhost sbin]# history
1 pwd
2 ip a
3 w
4 ip a
...
341 ps -ef|grep nginx
342 ls
343 cd sbin
344 ls
345 ./nginx
346 history
[root@localhost sbin]# !341
ps -ef|grep nginx
root 15682 1 0 00:47 ? 00:00:00 nginx: master process ./nginx
nobody 15683 15682 0 00:47 ? 00:00:00 nginx: worker process
root 15687 13010 0 00:48 pts/3 00:00:00 grep --color=auto nginx
[root@localhost sbin]#
9.添加Nginx的環(huán)境變量,可以快捷使用Nginx
[root@localhost sbin]# pwd
/opt/nginx112/sbin
[root@localhost sbin]# vim /etc/profile
PATH="/opt/python36/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/opt/node-v8.6.0-linux-x64/bin:/opt/nginx112/sbin"
10.讀取配置文件 source /etc/profile
[root@localhost sbin]# nginx
-bash: nginx: command not found
[root@localhost sbin]# source /etc/profile
[root@localhost sbin]# which nginx
/opt/nginx112/sbin/nginx
11.測試
192.168.0.101
12.修改配置文件
13.平滑重啟nginx
[root@localhost nginx112]# ls
client_body_temp conf fastcgi_temp html logs proxy_temp sbin scgi_temp uwsgi_temp
[root@localhost nginx112]# ./sbin/nginx -s reload
Nginx功能目錄
conf 配置文件nginx.conf (Nginx的功能參數(shù)都在這個文件定義)
html 存放前端頁面
logs 存放Nginx的運行日志,錯誤日志
sbin 存放Nginx可執(zhí)行程序的目錄
conf/nginx.conf參數(shù)說明:
####################
#user nobody;
#工作進程數(shù),根據(jù)cpu的核數(shù)優(yōu)化。
worker_processes 4;
#錯誤日志
#error_log logs/error.log;
#error_log logs/error.log notice;
error_log logs/error.log info;
#進程id文件
#pid logs/nginx.pid;
#nginx事件驅動
events {
worker_connections 60000;
}
#nginx web配置核心區(qū)域
http {
include mime.types;
default_type application/octet-stream;
#日志格式
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
#日志文件
access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
#定義nginx虛擬主機的
server {
#nginx監(jiān)聽的端口,默認瀏覽器是80
listen 80;
#填寫服務器的域名,如果你有域名,nginx會解析到當前這個虛擬主機
#當訪問pythonav.cn:80
server_name pythonav.cn;
#charset koi8-r;
#access_log logs/host.access.log main;
#location 就是nginx的路徑資源匹配
#就是當請求
#pythonav.cn/
#pythonav.cn/zoie.jpg
#python.cn/flash/zoie.mp4
#這個location / 這個語法是萬能匹配,你所有的請求,都會進入這個location
location / {
#這個root用于定義網頁根目錄路徑(可以換成其他絕對路徑,相對路徑是當前nginx)
root html;
#定義網頁的首頁文件,名字必須叫做index.html
index index.html index.htm;
}
#通過這個參數(shù),定義錯誤頁面的文件,當狀態(tài)碼是404 400 401時,返回40x.html頁面
error_page 404 401 400 403 /40x.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
####################
1.nginx定義多虛擬主機配置如下:
http{
#虛擬主機1,運行吃雞網站
#虛擬主機的加載,自上而下的加載,如果是訪問的ip地址,永遠訪問第一個
server{
listen 80;
#當訪問的域名是chiji.com就進入這個server標簽
server_name chiji.com;
location / {
root /opt/chiji;
index index.html index.htm;
}
}
#虛擬主機2,運行韓劇網站
server{
listen 80;
#當訪問的域名是hanju.com就進入這個server標簽
server_name hanju.com;
location / {
root /opt/hanju;
index index.html index.htm;
}
}
}
2.配置兩個虛擬主機的網站資源
1.配置吃雞網游的資料
在/opt/chiji 目錄下創(chuàng)建index.html
2.配置韓劇的資料
在/opt/hanju 目錄下創(chuàng)建index.html
3.配置兩個本地解析的域名
C:\Windows\System32\drivers\etc\hosts文件,寫入如下配置:
192.168.1.101 chiji.com
192.168.1.101 hanju.com
4.在Windows下測試訪問是否正常
chiji.com
hanju.com
3.定義nginx錯誤頁面優(yōu)化404頁面定制
修改nginx.conf 找到如下參數(shù)
#通過這個參數(shù),定義錯誤頁面的文件,當狀態(tài)碼是404 400 401時,返回40x.html頁面
error_page 404 401 400 403 /40x.html;
4.nginx用戶訪問日志access.log
####################
nginx反向代理
代理:
微商,代購,房屋中介,黃牛,
用戶向一個代理請求資源,而不需要關注這個資源的真實位置
用戶(瀏覽器) 請求網站資源 --》直接定位到django后臺(所有的請求壓力,都直接給了后臺)
django默認對并行很差,并且處理網頁的靜態(tài)資源效率很差。
10萬個并發(fā)請求--》后臺應用
靜態(tài)請求 --> nginx(天然并發(fā)性很高,并且處理靜態(tài)資源css,js,jpg),靜態(tài)資源nginx直接從磁盤上返回。
hanju.com/zoie.png
nginx無法處理動態(tài)請求,直接將這個請求丟給后端應用django
動態(tài)請求(對數(shù)據(jù)庫進行交互,必須有編程語言的支撐)
hanju.com/login/ --> django 后臺,django這個編程語言框架,就可以對login請求處理
####################
實現(xiàn)nginx反向代理的功能
1.實驗環(huán)境準備,2臺機器
用戶:瀏覽器發(fā)送請求,得到請求
192.168.0.101 nginx反向代理服務器(房屋中介代理)
192.168.0.111 真實資源服務器(有房源的房東)
2.分別在2臺機器上,安裝nginx
3.先配置真實資源服務器 192.168.0.111,打開這個機器的頁面是吃雞的網游頁面
4.配置反向代理服務器192.168.0.101
http{
#虛擬主機的加載,自上而下的加載,如果是訪問的ip地址,永遠訪問第一個
#這個nginx服務器,不再是用作虛擬主機了
#而是直接轉發(fā)別人的請求,是一個代理身份
server{
listen 80;
#當訪問的域名是chiji.com就進入這個server標簽
server_name chiji.com;
location / {
#root /opt/chiji;
#index index.html index.htm;
#當請求是chiji.com的時候,這個nginx不做處理,直接轉發(fā)請求給另一臺機器
proxy_pass http://192.168.0.111;
}
}
####################其他
1.nginx腳本命令
nginx 直接輸入是啟動
nginx -s stop 停止
nginx -s reload 平滑重啟,重新讀取配置文件
.nginx.conf.swp 這個文件是由于你在vim編輯文件的時候,異常退出,或者有其他人也想獲取這個文件句柄,
vim防止文件內容錯亂,丟失,自動生成一個swp緩存文件,用于保護文件的。
[root@localhost conf]# rm -rf .nginx.conf.swp #刪除這個隱藏文件就可以了
###
top 命令可以查看cpu核數(shù)
hostnamectl set-hostname master
hostnamectl set-hostname slave
redis持久化之RDB:
1.rdb持久化,可以手動觸發(fā)持久化,通過redis放入save命令觸發(fā)
2.rdb文件是一個經過壓縮的二進制文件,redis可以通過這個文件還原數(shù)據(jù)
3.rdb持久化還有時間策略
save 900 1 #900秒 1個修改類的作
save 300 10 #300秒 10個作
save 60 10000 #60秒 10000個作
4.使用rdb持久化的方式
--配置文件:
[root@localhost redis-4.0.10]# vim redis-6380.conf
寫入以下內容:
port 6380 #redis端口
daemonize yes #后臺運行redis
pidfile /data/6380/redis.pid #pid號碼
loglevel notice #日志等級
logfile "/data/6380/redis.log" #日志文件存放路徑
requirepass bubu666 #redis的密碼
dir /data/6380 #redis數(shù)據(jù)目錄/#定義持久化文件存儲位置【記得去創(chuàng)建一下目錄】
dbfilename redis.dump #rdb持久化文件(此文件在/data/6380目錄下)
save 900 1 #rdb機制 每900秒 有1個修改記錄
save 300 10 #每300秒 10個修改記錄
save 60 10000 #每60秒內 10000修改記錄
5.關閉redis服務端,準備重啟或者使用kill
redis-cli -p 6380 -a bubu666 shutdown
6.使用新的支持rdb持久化的配置文件啟動redis
redis-server redis-6380.conf
7.手動觸發(fā)rdb持久化
127.0.0.1:6380> set name dsb
OK
127.0.0.1:6380> save
OK
8.讓配置文件支持定期持久化
save 900 1 #rdb機制 每900秒 有1個修改記錄
save 300 10 #每300秒 10個修改記錄
save 60 10000 #每60秒內 10000修改記錄
redis持久化之AOF(企業(yè)一般多用這個):
1.配置文件:
[root@localhost redis-4.0.10]# vim redis-6380.conf
寫入以下內容:
port 6380 #redis端口
daemonize yes #后臺運行redis
pidfile /data/6380/redis.pid #pid號碼
loglevel notice #日志等級
logfile "/data/6380/redis.log" #日志文件存放路徑
requirepass bubu666 #redis的密碼
dir /data/6380 #redis數(shù)據(jù)目錄/#定義持久化文件存儲位置【記得去創(chuàng)建一下目錄】
appendonly yes
appendfsync everysec
2.啟動
redis-server redis-6380.conf
3.寫入數(shù)據(jù)
[root@localhost redis-4.0.10]# redis-cli -p 6380 -a bubu666
Warning: Using a password with '-a' option on the command line interface may not be safe.
127.0.0.1:6380> set name dsb
OK
127.0.0.1:6380> keys *
1) "name"
4.新終端中檢查
[root@localhost redis-4.0.10]# cd /data/6380
[root@localhost 6380]# tail -f appendonly.aof
##############################################
AOF持久化配置,兩條參數(shù)
appendonly yes
appendfsync always 總是修改類的作
everysec 每秒做一次持久化
no 依賴于系統(tǒng)自帶的緩存大小機制
#bind 10.0.0.10 127.0.0.1 #redis綁定地址
[root@localhost ~]# redis-server -v
Redis server v=4.0.10 sha=00000000:0 malloc=jemalloc-4.0.3 bits=64 build=b632d68350d21f18
Linux redis學習
1.安裝redis的方式
yum (刪除這個yum安裝的redis,我們只用源碼編譯安裝的)
rpm
源碼編譯
2.刪除原本的redis
yum remove redis -y
3.源碼安裝redis
1.下載redis源碼
wget http://download.redis.io/releases/redis-4.0.10.tar.gz
2.解壓縮
tar -zxf redis-4.0.10.tar.gz
3.切換redis源碼目錄
cd redis-4.0.10.tar.gz
4.編譯源文件
make
5.編譯好后,src/目錄下有編譯好的redis指令
6.make install 安裝到指定目錄
7.默認在/usr/local/bin
8.配置文件:
[root@localhost opt]# cd redis-4.0.10/
[root@localhost redis-4.0.10]# touch redis-6380.conf
[root@localhost redis-4.0.10]# vim redis-6380.conf
寫入以下內容:
port 6380 #redis端口
daemonize yes #后臺運行redis
pidfile /data/6380/redis.pid #pid號碼
loglevel notice #日志等級
logfile "/data/6380/redis.log" #日志文件存放路徑
requirepass bubu666 #redis的密碼
dir /data/6380 #redis數(shù)據(jù)目錄【記得去創(chuàng)建一下目錄不然啟動redis就報錯了】
9.新建文件
[root@localhost redis-4.0.10]# mkdir -p /data/6380
10.指定redis的配置文件,啟動redis
redis-server redis-6380.conf
[root@localhost ~]# ps -ef|grep redis
[root@localhost ~]# netstat -tunlp
11.連接
[root@localhost ~]# redis-cli -p 6380
127.0.0.1:6380> ping
PONG
127.0.0.1:6380>
12.加了密碼以后(auth bubu666)
[root@localhost redis-4.0.10]# redis-cli -p 6380
127.0.0.1:6380> ping
(error) NOAUTH Authentication required.
127.0.0.1:6380> auth bubu666
OK
127.0.0.1:6380> ping
PONG
127.0.0.1:6380>
或者
[root@localhost redis-4.0.10]# redis-cli -p 6380 -a bubu666
127.0.0.1:6380> ping
PONG
127.0.0.1:6380>
編譯安裝python3
1.切換opt目錄(這是一個約定)
cd /opt
2.下載python3的源代碼
wget https://www.python.org/ftp/python/3.6.2/Python-3.6.2.tgz
3.安裝python前的庫環(huán)境,非常重要
yum install gcc patch libffi-devel python-devel zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel -y
4.解壓縮python3的源代碼壓縮文件
cd /opt/ 進入存在這個文件的目錄
tar -xf Python-3.6.2.tgz 解壓
5.cd Python-3.6.2/ 進入源代碼目錄下,準備開始編譯安裝
6.編譯安裝三部曲
第一曲:
cd Python-3.6.2/
#configure 這個腳本文件,只是為了釋放makefile,用于指定python3安裝到哪里
./configure --prefix=/opt/python36 --with-ssl
--prefix 這個參數(shù)用于指定python安裝的路徑
第二曲:
執(zhí)行make開始編譯python3.6的源代碼
make
第三曲:
make install 生成/opt/python36
(第二曲和第三曲可以寫成 make && make install)
7.配置環(huán)境變量
echo $PATH #$PATH是有優(yōu)先級順序的
將/opt/python36/bin/放入環(huán)境變量,注意要添加到path的最前面
#變量的賦值只是臨時有效
PATH="/opt/python36/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin"
#將這個參數(shù),寫入到Linux的全局變量配置文件中
vim /etc/profile #打開這個全局變量文件,寫入PATH="/opt/python36/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin"到文件最后
8.退出(logout),重新加載全局變量.
9.which python3 確認是否正確安裝
10.不要將系統(tǒng)默認的python 改成python3 這是一個大坑。
(因為Linux有很多工具,默認是用的python2,例如yum)
11.在Linux下安裝django程序
pip3 install -i https://pypi.douban.com/simple django==1.11.16
pip3 install django==1.11.16
pip3 list
12.使用django命令創(chuàng)建項目
[root@localhost ~]# cd /opt
[root@localhost opt]# django-admin startproject mysite
13.修改mysite的settings.py中的ALLOWED_HOSTS
vim /opt/mysite/mysite/settings.py #/查找
ALLOWED_HOSTS = ["*"]
14.啟動項目
[root@localhost opt]# cd mysite/
[root@localhost mysite]# python3 manage.py runserver 0.0.0.0:9999
補充:
python3的管理工具是pip3 install flask
python2的是easy_install flask
[root@localhost ~]# python -V
Python 2.7.5
[root@localhost ~]# python --version
Python 2.7.5
[root@localhost ~]# python3 --version
Python 3.6.2
[root@localhost ~]# pip3 list
DEPRECATION: The default format will switch to columns in the future. You can use --format=(legacy|columns) (or define a format=(legacy|columns) in your pip.conf under the [list] section) to disable this warning.
Django (1.11.16)
pip (9.0.1)
pytz (2019.2)
setuptools (28.8.0)
You are using pip version 9.0.1, however version 19.2.3 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
[root@localhost ~]#
###########################################
dns /etc/resolv.conf
[root@localhost ~]# ifconfig
eno16777736: flags=4163 mtu 1500
inet 192.168.0.101 netmask 255.255.255.0 broadcast 192.168.0.255
inet6 fe80::20c:29ff:fe53:9402 prefixlen 64 scopeid 0x20
ether 00:0c:29:53:94:02 txqueuelen 1000 (Ethernet)
RX packets 68 bytes 9208 (8.9 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 84 bytes 11947 (11.6 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73 mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10
loop txqueuelen 0 (Local Loopback)
RX packets 11 bytes 1220 (1.1 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 11 bytes 1220 (1.1 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@localhost ~]# vim /etc/sysconfig/net
netconsole network network-scripts/
[root@localhost ~]# vim /etc/sysconfig/network-scripts/if
ifcfg-eno16777736 ifdown-eth ifdown-post ifdown-Team ifup-aliases ifup-ipv6 ifup-post ifup-Team
ifcfg-lo ifdown-ippp ifdown-ppp ifdown-TeamPort ifup-bnep ifup-isdn ifup-ppp ifup-TeamPort
ifdown ifdown-ipv6 ifdown-routes ifdown-tunnel ifup-eth ifup-plip ifup-routes ifup-tunnel
ifdown-bnep ifdown-isdn ifdown-sit ifup ifup-ippp ifup-plusb ifup-sit ifup-wireless
[root@localhost ~]# vim /etc/sysconfig/network-scripts/if
ifcfg-eno16777736 ifdown-eth ifdown-post ifdown-Team ifup-aliases ifup-ipv6 ifup-post ifup-Team
ifcfg-lo ifdown-ippp ifdown-ppp ifdown-TeamPort ifup-bnep ifup-isdn ifup-ppp ifup-TeamPort
ifdown ifdown-ipv6 ifdown-routes ifdown-tunnel ifup-eth ifup-plip ifup-routes ifup-tunnel
ifdown-bnep ifdown-isdn ifdown-sit ifup ifup-ippp ifup-plusb ifup-sit ifup-wireless
[root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-eno16777736 #打開網卡配置文件
HWADDR="00:0C:29:53:94:02"
TYPE="Ethernet"
BOOTPROTO="dhcp" #動態(tài)獲取ip協(xié)議
DEFROUTE="yes"
PEERDNS="yes"
PEERROUTES="yes"
IPV4_FAILURE_FATAL="no"
IPV6INIT="yes"
IPV6_AUTOCONF="yes"
IPV6_DEFROUTE="yes"
IPV6_PEERDNS="yes"
IPV6_PEERROUTES="yes"
IPV6_FAILURE_FATAL="no"
NAME="eno16777736"
UUID="67dc0736-0a20-48c1-8576-31ae47670ff5"
ONBOOT="yes" #必須將他改為ONBOOT="yes" 開機時讀取這個網卡腳本
不能上網
1.網卡配置文件中必須將他改為ONBOOT="yes" 開機時讀取這個網卡腳本
2.systemctl restart network
centos7 mariadb的學習
1.在Linux上安裝軟件的方式
yum安裝 在線搜索rpm格式的軟件包,進行自動的依賴關系處理,下載,安裝
(阿里云的yum倉庫,里面的軟件都是阿里云運維工程師定義的)
yum install mysql -y
手動rpm包安裝,需要手動解決N個軟件依賴
rpm -ivh mysqlxx.rpm
源碼編譯安裝(這種方式是企業(yè)應用最多的)
(可以自定制軟件的版本,以及可以最優(yōu)先的使用最新版本的軟件)
2.yum源的配置(mysql的yum源)
cd /etc/yum.repos.d/
1.在線下載阿里云的yum倉庫,以及epel倉庫
2.安裝mysql的方式
yum install mysql-server mysql -y
3.安裝mysql的方式也有2種,阿里云官方提供的mariadb軟件包,版本可能太低。(下載網速很快,方便學習使用)
4.企業(yè)里多半不會用阿里云的,因為版本低,安全性太低,會配置mariadb官方的yum倉儲
1.如下作:(手動創(chuàng)建mariadb倉庫文件,寫入配置信息,安裝最新版本mariadb)
[root@localhost ~]# cd /etc/yum.repos.d/
[root@localhost yum.repos.d]# touch Mariadb.repo
[root@localhost yum.repos.d]# vim Mariadb.repo
寫入如下配置:
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.1/centos7-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1
[root@localhost yum.repos.d]# mysql -V
mysql Ver 15.1 Distrib 5.5.64-MariaDB, for Linux (x86_64) using readline 5.1
[root@localhost yum.repos.d]# yum install mariadb-server mariadb -y
2.啟動
systemctl start mariadb
3.初始化mysql
mysql_secure_installation
【
Set root password? [Y/n] y
New password: 輸入root管理員設置的數(shù)據(jù)庫密碼
Re-enter new password: 再次輸入密碼
Remove anonymous users? [Y/n] y 刪除匿名賬戶
Disallow root login remotely? [Y/n] n 允許root管理員從遠程登錄
Remove test database and access to it? [Y/n] y 刪除test數(shù)據(jù)庫并取消對它的訪問權限
Reload privilege tables now? [Y/n] y #刷新授權表,讓初始化后的設定立即生效
】
4.配置數(shù)據(jù)庫的中文支持
\s 查看數(shù)據(jù)庫編碼
編輯mysql配置文件/etc/my.cnf,寫入以下內容:
[mysqld]
character-set-server=utf8
collation-server=utf8_general_ci
log-error=/var/log/mysqld.log
[client]
default-character-set=utf8
[mysql]
default-character-set=utf8
5.在服務器上,修改了配置文件,都需要重啟數(shù)據(jù)庫服務
systemctl restart mariadb
6.查看具體庫表的編碼
show create database myblog;
show create table user;
5.window去鏈接(報錯需要授權配置)
mysql -uroot -p -h 192.168.0.101
6.授權配置:配置mysql支持遠程連接的sql語句
授予root用戶對所有的庫表所有的權限,在所有的機器上作,皆可登陸。
MariaDB [(none)] > grant all privileges on *.* to root@'%' identified by '密碼';
#刷新權限
MariaDB [(none)] > flush privileges;
mysql主從同步技術
1.環(huán)境準備,準備2臺機器,一臺master一臺slave
192.168.0.101 (主庫)
192.168.0.111 (從庫)
【主庫】
2.配置主庫的環(huán)境
1.#停mariadb
systemctl stop mariadb
2.#修改主庫的配置文件,開啟binlog功能
vim /etc/my.cnf
#修改內容
#解釋:server-id服務的唯一標識(主從之間都必須不同);log-bin啟動二進制日志名稱為mysql-bin
[mysqld]
server-id=1
log-bin=mysql-bin
3.#啟動mariadb
systemctl start mariadb
4.新建用于主從同步的用戶tom,允許登錄的從庫是'192.168.0.111'
create user 'tom'@'192.168.0.111' identified by '密碼';
#create user 'tom'@'%' identified by '密碼';
5.授權tom一個slave的身份標識,在192.168.0.111機器上復制
grant replication slave on *.* to 'tom'@'192.168.0.111';
#grant replication slave on *.* to 'tom'@'%';
6.鎖表
flush table with read lock;
7.數(shù)據(jù)庫信息導出
mysqldump -uroot -p --all-databases > /opt/db.dump
8.從主庫把sql文件發(fā)給從庫
scp /opt/db.dump [email protected]:/opt/
9.【從庫配置】
10.解鎖
unlock tables;
11.驗證主從同步是否正常。
【從庫】
配置從庫的環(huán)境
1.編碼:編輯mysql配置文件/etc/my.cnf,寫入以下內容:
[mysqld]
character-set-server=utf8
collation-server=utf8_general_ci
log-error=/var/log/mysqld.log
[client]
default-character-set=utf8
[mysql]
default-character-set=utf8
2.修改Slave的/etc/my.cnf,寫入
[mysqld]
server-id=3
read-only=true
(說明:設置server-id值并關閉binlog功能參數(shù)
數(shù)據(jù)庫的server-id在主從復制體系內是唯一的,Slave的server-id要與主庫和其他從庫不同,并且注釋掉Slave的binlog參數(shù)。)
3.啟動
systemctl start mariadb
4.導入數(shù)據(jù)(之前從主庫傳過來的)
mysql -uroot -p < /opt/db.dump
5.在從庫,通過一條命令,開啟主從同步
mysql > change master to master_host='192.168.0.101',
6.配置復制的參數(shù),Slave從庫連接Master主庫的配置
【master_log_file和master_log_pos 通過show master status 查看】
mysql >
change master to master_host='192.168.0.101',
master_user='tom',
master_password='tom密碼',
master_log_file='mysql-bin.000001',
master_log_pos=575;
7.開啟slave同步
start slave;
8.檢查主從同步狀態(tài)
show slave status\G;
保障都yes(注意關閉防火墻)
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
9.驗證主從同步是否正常。
注意:如果你用的就是阿里云的yum源,安裝mariadb的軟件包名字就是如下:
yum install mysql-server mysql -y
如果你用的就是官方mariadb的yum源,
yum install mariadb-server mariadb -y
熟悉mariadb curd作,mariadb主從同步,讀寫分離技術...
學習運維架構技術乃王道
1.Nginx高可用負載均衡/緩存、限流/提升集群處理并發(fā)能力
2.mysql主從復制,讀寫分離
3.redis緩存加速與主從復制,哨兵,集群
4.異步消息/服務解耦之rabbitMQ消息隊列
5.代碼層優(yōu)化/改進算法/多線程與內存優(yōu)化
6.使用golang,Erlang等csp并發(fā)編程語言
7.docker容器時代
服務器介紹:
1.路飛的7臺阿里云,騰訊云,亞馬遜云,華為云
數(shù)據(jù)都放在其他人的電腦上,安全性由別人掌控
2.公司有錢,有26臺Dell實體服務器,vmware esxi虛擬化的300+Linux
公司有錢,自建機房(蓋了一個廠房,里面專業(yè)托管服務器)
有專業(yè)的公司,建造機房(世紀互聯(lián))
機房都是有嚴格的標準,保護機器不斷電,不損壞
1.無靜電
2.無塵
3.無濕度
4.低溫
Linux發(fā)行部:
Linux是一個名詞,是系統(tǒng)的代表
1.紅帽子公司 redhat Linux 收費
-2045之前,外企
-紅帽Linux 資格認證證書
rhcsa 紅帽系統(tǒng)管理員
rhce 紅帽工程師
rhca 紅帽構架師
2.centos 免費版,社區(qū)版 centos7
3.Ubuntu 烏班圖系統(tǒng)
4.suse 德國 Linux 收費
保證開發(fā)環(huán)境的一致性
1.手動解決
2.自動化解決
3.通過docker鏡像
4.通過vmware系統(tǒng)模板鏡像
Linux網絡連接方式
橋接:
在一個局域網內,添加了一個新的機器
vmware就像是虛擬化的一個機房的服務器
遠程連接
1.使用ssh命令
2.獲取服務器的ip地址
3.window的xshell中輸入:ssh [email protected] 遠程連接Linux機器
Linux重要的目錄
bin目錄 有關bin的文件夾都是存放可執(zhí)行文件的
etc目錄存放配置文件的 /etc/my.conf /etc/nginx.conf /etc/redis.conf
配置文件就是定制一堆參數(shù),自己控制的參數(shù)
opt目錄 存放第三方軟件的安裝路徑的 /opt/redis /opt/nginx /opt/python36
root目錄 存放超級用戶的家目錄
var目錄 存放系統(tǒng)日志相關
Linux目錄結構的作
cd - 上一次的工作目錄
cd ~ 當前登錄用戶的家目錄
cd . 當前目錄
cd .. 上一級目錄
絕對路徑:一切從根目錄開始,就是一個絕對路徑
相對路徑:以當前位置為相對,找到路徑
PATH變量是用于,當用戶直接輸入命令的時候,去查找的一個路徑尋找點
當我們輸入ls的時候,Linux回去PATH中尋找,哪里有l(wèi)s
Linux命令學習:
ssh [email protected]
1.查看ip地址
ip a
ifconfig
2.linux目錄分割是正斜杠/
3.w 那些用戶在登錄
4.whoami 我是誰
5.pwd 我在哪
6.更改目錄
cd /
7.用于輸出文件夾的內容
ls
ll
ls -a 查看隱藏文件
ls -la
ls -lh 帶文件大小
8.linux文件的顏色
淺藍是軟鏈接目錄
藍色是文件夾
白色是普通文件
綠色是可執(zhí)行文件
9.創(chuàng)建目錄
mkdir
mkdir -p ./a/b/c/d
mkdir -p ./bubu/{linux,python}
10.創(chuàng)建文件
touch
11.vi/vim寫入內容
a.此時進入了命令模式,輸入鍵盤的i,進入編輯模式
b.編輯模式下可以寫入代碼
c.退出編輯模式按下Esc,此時就進入了底線命令模式
d.輸入:wq!
:進入底線模式
w 寫入
q 退出
!強制的
底線命令模式 :set nu 顯示行號
12.cat查看寫入cat >>ceshi.txt ceshi.txt 123
> EOF
[root@localhost /tmp/bubu 00:20:02]#cat ceshi.txt
ceshi
[root@localhost /tmp/bubu 00:20:09]#
cat /etc/passwd > ./passwd.txt 將/etc/passwd的內容寫入到當前passwd.txt文件中
13.創(chuàng)建新用戶
useradd bubu
passwd bubu
14.切換用戶
su bubu
15.刪除
rmdir 刪除空文件夾
rm -rf * == rm -rf ./* 強制刪除當前目錄的所有內容
rm -rf /* 刪除根(作死)
-f遞歸刪除文件夾
-r強制性刪除
-i提示性作,是否刪除
16.ctrl+c 中斷當前作
17.which ls
18.echo $PATH
19.hostname 查看主機名
20.hostnamectl set-hostname localhost
21.logout 退出重新登錄查看
22.Linux命令提示符
[root@localhost ~]#
23.修改命令提示符
PS1變量控制
[root@localhost ~]# echo $PS1
[\u@\h \W]\$
修改命令提示符:
PS1='[\u@\h \w \t]\$'
24.軟件時間date,硬件時間hwclock
ntpdate -u ntp.aliyun.com
25.clear
26.history
27.yum install tree -y 安裝樹狀圖命令
28.tree mysite/
29.echo 寫入文件內容
echo 'hello' > ./test.txt
> 重定向覆蓋寫入符
>> 追加
30.拷貝 cp 目標文件 拷貝后的文件
cp my.py my.py.bak
31.重命名命令,以及移動命令
mv 舊文件名 新文件名
mv 目標文件 移動后的路徑
mv ../tmp/ceshi/txt ./
32.find 查找文件
find / -name test.txt 從根目錄開始找,一個叫做test.txt的文件
find /opt -name '*.py' 從/opt目錄下查找所有的.py文件
linux 管道符“|” 語法就是將第一條命令的結果傳給第二條
33.過濾出python有關的進程
ps -ef | grep python
34.過濾出22端口的信息
netstat -tunlp| grep 22
35.過濾命令grep
grep nobody ./passwd.txt 從當前的passwd.txt文件中過濾出nobody的信息
grep all settings.py 過濾出django中settings.py 中allow_hosts的信息
grep all settings.py -n 過濾出allow_hosts的信息且輸出行號
grep -v all settings.py 過濾出除allow_hosts之外的信息
grep -v all settings.py -n 過濾出除allow_hosts之外的信息且輸出行號
cat settings.py | grep allow
36.head
head -5 test.txt
37.tail
tail -5 test.txt
tail -f test.txt 實時監(jiān)控
37.alias別名
alias rm="rm -i"
alias rm="echo 求求你了,你可別坑我了!!!"
38.unalias rm 取消別名
39.遠程傳輸文件,在兩臺Linux上
scp 想要作的文件地址 存放的目標文件地址
scp test.txt [email protected]:/tmp/
scp -r a [email protected]:/tmp/ -r遞歸傳輸文件夾
40.查看文件夾大小
方式1:ls -lh
方式2:du -h
du -sh /var/log/
-s 合計文件夾大小
-h 顯示友好的單位換算
41命令(任務管理器)用于動態(tài)地監(jiān)視進程活動和系統(tǒng)負載等信息 q退出
42.wget 下載
wget -r -p http://www.luffycity.com
43.Linux和window互相傳遞文件
1.lrzsz
sz test.txt(下載 從Linux傳給window)
rz test.txt(上傳)
2.xftp
44.reboot 重啟
45.poweroff 關閉系統(tǒng)
46.free -m 查看內存
47.man grep 查看grep的幫助手冊
48.防火墻
iptables -L #查看規(guī)則
iptables -F #清空防火墻規(guī)則
永久關閉防火墻,可能防火墻會阻擋你的Linux環(huán)境配置實驗,為了方便,關閉它。
systemctl stop firewalld #關閉防火墻服務
systemctl disable firewalld #設置開機禁用防火墻
49.tar -xf Python-3.6.2.tgz 解壓
50.logout 退出賬號
#######################################################################
用戶權限相關
[root@localhost ~]# id #返回用戶的身份信息,當uid為0的時候,這個用戶就是超級用戶
uid=0(root) gid=0(root) groups=0(root) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
[root@localhost ~]# id root
uid=0(root) gid=0(root) groups=0(root)
用戶的存放文件:
/etc/passwd
/etc/shadow 存放用戶密碼的文件
用戶的管理命令:
添加普通用戶(只有root用戶有這個權利)
[root@localhost ~]# useradd tom #創(chuàng)建用戶,這個用戶的信息會存放到/etc/passwd
[root@localhost ~]# grep tom /etc/passwd #查看用戶信息,普通用戶的id默認是從1000開始的
tom:x:1001:1001::/home/tom:/bin/bash
[root@localhost ~]#
修改普通用戶的密碼
passwd 用戶名
切換用戶(必須加上中橫線- 代表完全用戶切換)
[root@localhost ~]# su - tom
[tom@localhost root]$
添加用戶組
groupadd it_dep
刪除用戶
userdel -rf tom
-f 強制刪除用戶
-r 同事刪除用戶以及家目錄
sudo命令用來以其他身份來執(zhí)行命令,預設的身份為root
1.編輯sudoers配置文件,添加tom用戶信息
vim /etc/sudoers (/查找root n查找下一個)
2.定位權限設置到
root ALL=(ALL) ALL
添加
root ALL=(ALL) ALL
tom ALL=(ALL) ALL
3.使用sudo命令,去執(zhí)行Linux命令,解決權限不足
[tom@localhost ~]$ ls /root
ls: cannot open directory /root: Permission denied
[tom@localhost ~]$ sudo ls /root
[sudo] password for tom:
anaconda-ks.cfg
4.sudo還提供了一個語法檢測的命令 visudo
文件權限:
讀取(vim) 查看(cat) 執(zhí)行(xx.py xx.sh)
通過解釋器執(zhí)行 ./xx.sh 或者 source xx.sh 或者 . xx.sh
文件夾的權限:
新增 修改 刪除 進入目錄
Linux軟鏈接創(chuàng)建注意用絕對路徑
ln -s 目標文件絕對路徑 軟鏈接絕對路徑
tar命令,參數(shù)
-x 解壓
-v 顯示壓縮解壓過程
-f 指定壓縮文件
-z 指定調用gzip命令
-c 壓縮當前目錄的所有內容:
-r 添加文件到已有的壓縮文件中
tar -rf alltmp.tgz 新加的文件名
壓縮當前目錄的所有內容:
tar -cf alltmp.tgz ./*
tar -cf alltmp.tgz *
tar -cf alltmp.tar *
tar -zcf alltmp.tar.gz *
解壓到當前目錄
tar -xf alltmp.tgz
查看命令幫助:
tar --help
man tar
linum 中文手冊
進程查看
ps -ef 查看所有進程的狀態(tài)
ps -ef | grep python
網絡端口管理命令
netstat -tunlp | grep 8000
kill命令 殺死進程
kill pid (pid通過ps -ef 查看)
一次性殺死多個匹配的進程,一個是pkill,一個是killall,
如果沒有killall就安裝一下 yum install psmisc -y
killall python
查看磁盤空間
df -h
Linux的dns服務相關
1.dns服務指定配置文件,這個文件,告訴Linux去那解析dns域名,
有主備兩個dns服務器ip地址
[root@localhost ~]# cat /etc/resolv.conf
# Generated by NetworkManager
domain www.tendawifi.com
search www.tendawifi.com
nameserver 192.168.0.1
[root@localhost ~]#
2.Linux解析dns的命令
nslookup www.baidu.com #解析到這個域名對應的ip地址
3.Linux強制dns,或者本地dns域名解析
vim /etc/hosts
ip 自定義的域名
4.域名解析的流程,(一個域名從瀏覽器訪問,解析過程是如何的)
1.瀏覽器輸入域名,瀏覽器首先會去當前機器的本地dns緩存中查找記錄
2.去hosts文件中查找是否寫死了對應的記錄
3.去指定的dns服務器中尋找dns記錄,如果找到了域名解析記錄后
4.將這個dns記錄緩存到本機的緩存記錄中
5.下一次尋找域名,就去緩存中找
Linux的計劃任務,也叫做定時任務,名字是crond
1.查看Linux本機的定時任務
crontab -l #查看計劃任務
2.編寫自己的計劃任務
crontab -e #編輯計劃任務
分時日月周 命令絕對路徑
* * * * * /user/bin/echo "666" >> /tmp/ceshi.txt
Linux系統(tǒng)服務管理命令
系統(tǒng)服務(Linux安裝的軟件名)
只有通過yum安裝的軟件才可以使用這個命令
systemctl 這是centos7系列的命令
service 這是centos6系統(tǒng)的服務管理命令
查看網絡狀態(tài)
systemctl status network
查看發(fā)行版
cat /etc/os-release
pip3是管理python模塊的工具,自動解決模塊依賴
pip3 list
pip3 install flask 默認是去python官方下載,網速很慢
更改pip下載的源,加速下載,使用離咱們近的豆瓣源,163源或者清華源
pip3 install -i https://pypi.douban.com/simple flask
Linux安裝軟件包
rpm手動安裝
yum工具自動化安裝 效果等同于 pip3
1.理解yum源(阿里云的yum源 epel額外倉庫)
yum源倉庫的位置:cd /etc/yum.repos.d/,并且只能讀出第一層的repo文件
yum倉庫的文件都是以.repo結尾的
2.下載阿里云的.repo倉庫文件,放到/etc/yum.repos.d/
第一步:備份原本的倉庫文件
cd /etc/yum.repos.d/
mkdir allbak
mv * allbak
1.下載第一個阿里云倉庫文件
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
-O 參數(shù),指定一個下載地址,且改名
2.配置第二個倉庫文件 epel 額外倉庫(redis,nginx,mongo,ipython)
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
3.清空原本yum緩存
yum clean all
4.生成新的阿里云的yum緩存,加速下載預熱數(shù)據(jù)(此步驟非必須)
yum makecache
代碼:
1.cd /etc/yum.repos.d/
2.mkdir allbak
3.mv * allbak
4.wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
5.wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
6.yum clean all
7.yum makecache
yum安裝nginx:
1.yum install nginx -y
2.systemctl start/restart/stop nginx
3.netstat -tunlp | grep 80 查看端口,過濾80端口的信息
4.iptables -F (清空防火墻規(guī)則)
5.客戶端:192.168.0.101:80 訪問
/usr/share/nginx/html/index.html
yum remove nginx
virtualenv 是python解釋器的分身
它是基于物理解釋器,進行一個解釋器分身,這個分身,可以用于運行各種python開發(fā)環(huán)境,
并且創(chuàng)建簡單,刪除銷毀也簡單,解決環(huán)境依賴災難。
1.安裝虛擬環(huán)境
pip3 install -i https://pypi.douban.com/simple virtualenv
2.通過virtualenv創(chuàng)建虛擬環(huán)境
mkdir /opt/allenv #建立統(tǒng)一管理目錄
cd /opt/allenv #進入統(tǒng)一管理目錄
virtualenv --no-site-packages --python=python3 venv1 #建立虛擬環(huán)境venv1
--no-site-packages 創(chuàng)建一個干凈隔離的python環(huán)境
--python=python3 基于python3創(chuàng)建虛擬環(huán)境
venv1 虛擬環(huán)境文件夾的名字,自己定義
3.激活虛擬環(huán)境
cd /opt/allenv
通過source命令,讀取激活腳本,激活虛擬環(huán)境
source /opt/allenv/venv1/bin/activate
4.激活虛擬環(huán)境后,檢查以下幾個步驟,是否正確激活
which pip3
which python3
5.檢查虛擬環(huán)境是否干凈隔離
pip3 list
6.退出虛擬環(huán)境的命令
deactivate
[root@localhost ~]# pip3 install -i https://pypi.douban.com/simple virtualenv
[root@localhost ~]# cd /opt
[root@localhost opt]# mkdir allenv
[root@localhost opt]# cd allenv
[root@localhost allenv]# virtualenv --no-site-packages --python=python3 venv1
Running virtualenv with interpreter /opt/python36/bin/python3
Already using interpreter /opt/python36/bin/python3
Using base prefix '/opt/python36'
New python executable in /opt/allenv/venv1/bin/python3 # 這個就是虛擬環(huán)境venv1的python3解釋器
Also creating executable in /opt/allenv/venv1/bin/python
Installing setuptools, pip, wheel...
done.
[root@localhost allenv]#
[root@localhost allenv]# ls
venv1
[root@localhost allenv]# cd venv1
[root@localhost venv1]# ls
bin include lib
[root@localhost venv1]# cd bin
[root@localhost bin]# ls
activate activate.fish activate_this.py easy_install pip pip3.6 python3 python-config
activate.csh activate.ps1 activate.xsh easy_install-3.6 pip3 python python3.6 wheel
[root@localhost bin]# source activate
(venv1) [root@localhost bin]#
(venv1) [root@localhost bin]# which pip3
/opt/allenv/venv1/bin/pip3
(venv1) [root@localhost bin]#
(venv1) [root@localhost bin]# pip3 list
Package Version
---------- -------
pip 19.2.3
setuptools 41.2.0
wheel 0.33.6
(venv1) [root@localhost bin]# which python3
/opt/allenv/venv1/bin/python3
(venv1) [root@localhost bin]#
(venv1) [root@localhost bin]# echo $PATH
/opt/allenv/venv1/bin:/opt/python36/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/root/bin
(venv1) [root@localhost bin]# pip3 install -i https://pypi.douban.com/simple django==2.0
確保開發(fā)環(huán)境的一致性:
解決方案:
1.通過命令保證環(huán)境的一致性,導出當前python環(huán)境的包
pip3 freeze > requirements.txt
這將會創(chuàng)建一個 requirements.txt 文件,其中包含了當前環(huán)境中所有包及 各自的版本的簡單列表。
可以使用 “pip list”在不產生requirements文件的情況下, 查看已安裝包的列表。
2.上傳至服務器后,在服務器下創(chuàng)建virtualenv,在venv中導入項目所需的模塊依賴
pip3 install -r requirements.txt
虛擬環(huán)境之virtualenvwrapper:
1.因為virtualenv工具使用并不方便
2.安裝virtualenvwrapper
pip3 install virtualenvwrapper
3.設置Linux的環(huán)境變量,每次啟動就加載virtualenvwrapper
1.打開文件
vim ~/.bashrc (這個文件是用戶個人配置文件)
2.寫入以下幾行代碼 (export和source一樣都是讀取Linux shell 變量的命令)
export WORKON_HOME=~/Envs #設置virtualenv的統(tǒng)一管理目錄
export VIRTUALENVWRAPPER_VIRTUALENV_ARGS='--no-site-packages' #添加virtualenvwrapper的參數(shù),生成干凈隔絕的環(huán)境
export VIRTUALENVWRAPPER_PYTHON=/opt/python36/bin/python3 #指定python解釋器
source /opt/python36/bin/virtualenvwrapper.sh #這一步才是真正使用工具的步驟,執(zhí)行virtualenvwrapper安裝腳本
3.退出當前會話,重新登錄Linux
logout
4.重新登錄,查看是否可以使用virtalenvwrapper
5.確保可以使用后,學習這個工具的命令
1.創(chuàng)建新的虛擬環(huán)境
mkvirtualenv django201
mkvirtualenv django115
2.切換不同的虛擬環(huán)境
workon django201
workon django115
3.退出虛擬環(huán)境
deactivate
4.刪除虛擬環(huán)境
rmvirtualenv django116
5.進入虛擬環(huán)境的家目錄(直接進入到 site-packages 目錄中)
cdsitepackages
6.列舉所有的環(huán)境。
lsvirtualenv
7.顯示 site-packages 目錄中的內容。
lssitepackages
運行crm
1.準備代碼
2.上傳代碼到Linux中
方式1:lrzsz(只能傳單個的文件,必須是壓縮包)
yum install lrzsz -y # 安裝傳輸工具
sz test.txt(下載 從Linux傳給window)
rz test.txt(上傳)
方式2:xftp
3.解壓
unzip crm.zip
4.用之前建好的虛擬環(huán)境去運行 :
workon django201
5.啟動
python3 /opt/crm/manage.py runserver 0.0.0.0:9000
上面第四步也可以新建虛擬環(huán)境去運行 :
1.mkvirtualenv myblog
2.解決環(huán)境依賴,少什么裝什么
pip3 install -i https://pypi.douban.com/simple django==2.0
pip3 install -i https://pypi.douban.com/simple pymysql
pip3 install -i https://pypi.douban.com/simple django-multiselectfield [multiselectfield模塊]
3.安裝Linux的mysql數(shù)據(jù)庫
在centos7中,mysql叫mariadb 安裝mariadb的服務端和客戶端
yum install mariadb-server mysql -y
4.啟動mariadb數(shù)據(jù)庫
[root@localhost ~]# systemctl start mariadb
5.檢查數(shù)據(jù)庫是否正常(下面幾種方式都可以)
ps -ef|grep mariadb
netstat -tunlp |grep 3306
systemctl status mariadb
6.看settings的配置,DATABASES,ALLOWED_HOSTS
7.建數(shù)據(jù)庫,導入數(shù)據(jù)
[root@localhost ~]# systemctl start mariadb
[root@localhost ~]# mysql -uroot -p
MariaDB [(none)]> create database myblog;
MariaDB [(none)]> use myblog;
MariaDB [myblog]> source /opt/myblog.sql;
MariaDB [myblog]> show databases;
8.運行
python3 /opt/myblog/manage.py runserver 0.0.0.0:9000
運行路飛學城
1.下載代碼
路飛學城django代碼
wget https://files.cnblogs.com/files/pyyu/luffy_boy.zip
vue代碼
wget https://files.cnblogs.com/files/pyyu/07-luffy_project_01.zip
2.解壓
unzip 07-luffy_project_01.zip
unzip luffy_boy.zip
3.先從后端代碼開始,進入后端代碼文件夾
4.新建虛擬環(huán)境
mkvirtualenv luffy
5.解決所需的依賴模塊,準備一個模塊版本文件:requirements.txt 這個文件可以手動創(chuàng)建寫入如下依賴
(luffy) [root@localhost opt]# cd /opt/luffy/luffy_boy
(luffy) [root@localhost luffy_boy]# touch requirements.txt
(luffy) [root@localhost luffy_boy]# vim requirements.txt
##############################
certifi==2018.11.29
chardet==3.0.4
crypto==1.4.1
Django==2.1.4
django-redis==4.10.0
django-rest-framework==0.1.0
djangorestframework==3.9.0
idna==2.8
Naked==0.1.31
pycrypto==2.6.1
pytz==2018.7
PyYAML==3.13
redis==3.0.1
requests==2.21.0
shellescape==3.4.1
urllib3==1.24.1
uWSGI==2.0.17.1
##############################
(luffy) [root@localhost luffy_boy]# ls
api db.sqlite3 keys luffy_boy manage.py requirements.txt static templates
6.安裝所需的依賴模塊
(luffy) [root@localhost luffy_boy]# pip3 install -i https://pypi.douban.com/simple -r requirements.txt
7.看一看
pip3 list
8.運行
python3 manage.py runserver 0.0.0.0:8000 (注意端口得是8000,因為前端的vue發(fā)的就是8000)
9.新開一個終端開始前端(要在服務器上,編譯打包vue項目,必須得有node環(huán)境)
1.準備node環(huán)境
下載node二進制包,此包已經包含node,不需要再編譯
[root@localhost ~]# cd /opt
[root@localhost opt]# wget https://nodejs.org/download/release/v8.6.0/node-v8.6.0-linux-x64.tar.gz
2.解壓縮
[root@localhost opt]# tar -zxvf node-v8.6.0-linux-x64.tar.gz
3.進入node文件夾
[root@localhost opt]# cd node-v8.6.0-linux-x64/
[root@localhost node-v8.6.0-linux-x64]# ls
bin CHANGELOG.md etc include lib LICENSE README.md share
[root@localhost node-v8.6.0-linux-x64]# cd bin
[root@localhost bin]# ls
node npm npx
[root@localhost bin]# pwd
/opt/node-v8.6.0-linux-x64/bin
4.配置環(huán)境變量
[root@localhost bin]# vim /etc/profile
PATH="/opt/python36/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/opt/node-v8.6.0-linux-x64/bin"
5.讀取全局配置文件,加載node的環(huán)境變量
[root@localhost bin]# source /etc/profile #就不需要退出重新加載了
[root@localhost bin]# echo $PATH
/opt/python36/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/opt/node-v8.6.0-linux-x64/bin
[root@localhost bin]# which node
/opt/node-v8.6.0-linux-x64/bin/node
6.檢測node環(huán)境
[root@localhost bin]# node -v
v8.6.0
[root@localhost bin]# npm -v
5.3.0
[root@localhost bin]#
【注意】進入vue代碼包中,開始進行打包,注意修改vue的api請求接口地址!!
【注意】進入vue代碼包中,開始進行打包,注意修改vue的api請求接口地址!!
【注意】進入vue代碼包中,開始進行打包,注意修改vue的api請求接口地址!!
7.修改src/restful/api.js中Axios的請求地址 ##里面的127.0.0.1都換成你后端服務器的id地址 192.168.0.101
[root@localhost ~]# sed -i "s/127.0.0.1/192.168.0.101/g" /opt/luffy/07-luffy_project_01/src/restful/api.js ##全局替換命令
8.進入vue目錄(安裝vue模塊,默認去裝package.json的模塊內容,如果出現(xiàn)模塊安裝失敗,手動再裝)
[root@localhost bin]# cd /opt/luffy/07-luffy_project_01/
[root@localhost 07-luffy_project_01]# ls
build config index.html package.json package-lock.json README.md src static
[root@localhost 07-luffy_project_01]# npm install
[root@localhost 07-luffy_project_01]# ls #增加了node_modules
build config index.html node_modules package.json package-lock.json README.md src static
9.打包vue項目,生成一個dist靜態(tài)文件夾
[root@localhost 07-luffy_project_01]# npm run build
10.檢查dist文件夾
[root@localhost 07-luffy_project_01]# ls
build config dist index.html node_modules package.json package-lock.json README.md src static #增加了dist
[root@localhost 07-luffy_project_01]# ls dist
index.html static
11.只需要將vue的靜態(tài)文件,發(fā)布到web我放棄,訪問web服務器即可。
12.安裝配置nginx web服務器,訪問到vue的靜態(tài)文件
[root@localhost ~]# yum install nginx -y
[root@localhost ~]# systemctl start nginx
[root@localhost ~]# netstat -tunlp|grep nginx
[root@localhost ~]# find / -name index.html # 192.168.0.101 看到的就是這個頁面/usr/share/nginx/html/index.html
[root@localhost ~]# vim /etc/nginx/nginx.conf
###################
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /opt/luffy/07-luffy_project_01/dist; ##更改這的路徑
#root這個參數(shù)代表定義網頁根目錄,只要訪問Nginx,Nginx就去這個根目錄下尋找一個index.html的文件
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
###################
12.重啟nginx服務才能生效
[root@localhost ~]# systemctl restart nginx
13.添加課程數(shù)據(jù)是存放到redis中的,需要安裝redis
yum install redis -y
14.啟動redis
[root@localhost ~]# systemctl start redis
[root@localhost ~]# netstat -tunlp |grep 6379
[root@localhost ~]# redis-cli
127.0.0.1:6379> ping
PONG
127.0.0.1:6379> FLUSHDB #手動清除數(shù)據(jù)
OK
127.0.0.1:6379> keys *
15.測試
http://192.168.0.101
#編輯:
vim /etc/bashrc
#最下面添加:
export PS1="[\[\e[34;1m\]\u@\[\e[0m\]\[\e[32;1m\]\H\[\e[0m\]\[\e[31;1m\] \w\[\e[0m\]]\\$ "
#生效:
source /etc/bashrc
crm項目部署
激活虛擬python環(huán)境
#創(chuàng)建基于python3的虛擬解釋器環(huán)境venv
virtualenv --no-site-packages --python=python3 venv
#激活python3虛擬環(huán)境
[root@yugo /data 11:11:30]#source venv/bin/activate
(venv) [root@yugo /data 11:11:35]#
安裝uwsgi
(venv) [root@yugo /data 11:13:23]#pip3 install uwsgi
配置啟動uwsgi.ini,啟動uwsgi時候,用這個配置文件啟動
(venv) [root@yugo /data 11:14:25]#cat uwsgi.ini
[uwsgi]
#使用nginx連接時使用
socket=0.0.0.0:8000
#不用nginx直接當做web服務器使用
#http=0.0.0.0:9000
#項目目錄絕對路徑
chdir=/data/Ace_crm#wsgi文件路徑,在項目底下
wsgi-file=Ace_crm/wsgi.py
#指定解釋器目錄
home=/data/venv
processes=4
threads=2
master=True
pidfile=uwsgi.pid
daemonize=uwsgi.log
配置nginx
配置nginx.conf,通過nginx反向代理將請求丟給django處理
(venv) [root@yugo /data 11:20:32]#cat /opt/nginx1-12/conf/nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log logs/access.log main;
sendfile on;
keepalive_timeout 65;
#定義負載均衡池,名字叫做django,池子中寫入uwsgi發(fā)布django的socket地址
upstream django {
server 0.0.0.0:8000;
}
server {
listen 80;
server_name pythonav.cn;
#訪問nginx的根路徑時,轉發(fā)請求給uwsgi的8000端口,這里要和uwsgi.ini寫的一致
location / {
include /opt/nginx1-12/conf/uwsgi_params; #請求轉發(fā)給upstream地址池里的uwsgi程序
uwsgi_pass django;
}
location /static/ {
alias /opt/nginx1-12/html/static/;
}
}
}
熱加載nginx服務,讀取nginx.conf內容
(venv) [root@yugo /data 11:24:24]#/opt/nginx1-12/sbin/nginx -t
nginx: the configuration file /opt/nginx1-12/conf/nginx.conf syntax is ok
nginx: configuration file /opt/nginx1-12/conf/nginx.conf test is successful
(venv) [root@yugo /data 11:26:07]#/opt/nginx1-12/sbin/nginx -s reload
啟動uwsgi,啟動django
(venv) [root@yugo /data 11:26:54]#uwsgi --ini uwsgi.ini
[uWSGI] getting INI configuration from uwsgi.ini
(venv) [root@yugo /data 11:27:10]#ps -ef|grep uwsgi
root 15540 1 0 11:27 ? 00:00:00 uwsgi uwsgi.ini
root 15543 15540 0 11:27 ? 00:00:00 uwsgi uwsgi.ini
root 15544 15540 0 11:27 ? 00:00:00 uwsgi uwsgi.ini
root 15545 15540 0 11:27 ? 00:00:00 uwsgi uwsgi.ini
root 15546 15540 0 11:27 ? 00:00:00 uwsgi uwsgi.ini
root 15590 11958 0 11:27 pts/0 00:00:00 grep --color=auto uwsgi#如果需要停止uwsgi可以使用ps -ef|grep uwsgi,找到pid殺掉#更好的一個殺掉uwsgi的方式killall -9 uwsgi
訪問nginx的80端口,查看是否請求轉發(fā)給django
http://pythonav.cn/login/
或者10.0.0.10/login
配置nginx的靜態(tài)資源

為什么要配置靜態(tài)資源?
配置靜態(tài)資源目錄是因為讓靜態(tài)資源通過nginx可以直接返回,不需要通過uwsgi,也就是讓uwsgi只處理后端邏輯,不處理靜態(tài)資源,優(yōu)化性能
配置靜態(tài)資源,django和nginx
#創(chuàng)建靜態(tài)資源存放目錄
[root@yugo /opt/nginx1-12/html 11:39:51]#mkdir -vp /opt/nginx1-12/html/static
mkdir: created directory ‘/opt/nginx1-12/html/static’#給目錄添加權限[root@yugo /opt/nginx1-12/html 11:40:57]#chmod? 755 /opt/nginx1-12/html/static/
配置django的settings.py
DEBUG = False
ALLOWED_HOSTS = ['*']
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.11/howto/static-files/
STATIC_URL = '/static/'
STATIC_ROOT= '/opt/nginx1-12/html/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static')
]

分割線--

收集django靜態(tài)文件
python3 manage.py collectstatic
這一句話就會把以前放在app下static中的靜態(tài)文件全部拷貝到 settings.py 中設置的 STATIC_ROOT 文件夾中然后請求靜態(tài)資源就會去nginx配置的 location /static {alias /opt/nginx1-12/html/static/ } 尋找
以上步驟完成后,訪問服務器主機地址和端口,如果nginx.conf中配置的為80端口,則地址欄不需要輸入端口,因為瀏覽器請求端口也是默認為80端口,非80端口的需要自己在ip后面添加
luffy項目部署(vue+uwsgi+nginx)
路飛學城django代碼:https://files.cnblogs.com/files/pyyu/luffy_boy.zip
vue代碼:https://files.cnblogs.com/files/pyyu/07-luffy_project_01.zip
一、將代碼搞到服務器上
在linux上直接下載
wget https://files.cnblogs.com/files/pyyu/luffy_boy.zip
wget https://files.cnblogs.com/files/pyyu/07-luffy_project_01.zip
在window上下載,通過lrzsz,或者xftp傳輸?shù)絣inux服務器上
二、先從前端vue搞起
要在服務器上,編譯打包vue項目,必須得有node環(huán)境
下載node二進制包,此包已經包含node,不需要再編譯
wget https://nodejs.org/download/release/v8.6.0/node-v8.6.0-linux-x64.tar.gz
解壓縮
tar -zxvf node-v8.6.0-linux-x64.tar.gz
進入node文件夾
[root@web02 opt]# cd node-v8.6.0-linux-x64/
[root@web02 node-v8.6.0-linux-x64]# ls
bin CHANGELOG.md etc include lib LICENSE README.md share
[root@web02 node-v8.6.0-linux-x64]# ls bin
node npm npx
[root@web02 node-v8.6.0-linux-x64]# ./bin/node -v
v8.6.0
[root@web02 node-v8.6.0-linux-x64]# ./bin/npm -v
5.3.0
將node命令,添加至linux環(huán)境變量,修改/etc/profile,寫入
PATH=$PATH:/opt/node-v8.6.0-linux-x64/bin
讀取文件,生效PATH
source /etc/profile
測試path
[root@web02 node-v8.6.0-linux-x64]# node -v
v8.6.0
[root@web02 node-v8.6.0-linux-x64]# npm -v
5.3.0
node環(huán)境有了,安裝node模塊,以及打包node項目
進入vue源碼目錄
cd 07-luffy_project_01/
安裝vue模塊,默認去裝package.json的模塊內容,如果出現(xiàn)模塊安裝失敗,手動再裝
npm install
此時注意,你本地寫的vue代碼,接口很可能連接的服務器地址有問題,注意Axios.POST提交的地址,一定得發(fā)送給django應用(如果用了nginx,就發(fā)送給nginx的入口端口)
超哥這里為了試驗方便,將vue項目和django項目放在了一臺服務器,通過nginx反向代理功能(8000端口),轉發(fā)vue請求給django(9000)
準備編譯打包vue項目,替換配置文件所有地址,改為服務器地址
sed -i 's/127.0.0.1/192.168.119.12/g' /opt/07-luffy_project_01/src/restful/api.js確保vue的route模式是history路徑:opt/luffy/07-luffy_project_01/src/router/index.js
export default new Router({linkActiveClass:'is-active',mode: 'history',//改成history模式
此時打包vue項目,生成一個dist靜態(tài)文件夾
npm run build
檢查dist文件夾
[root@web02 07-luffy_project_01]# ls dist/
index.html static
至此vue代碼就結束了,只需要讓nginx配置,找到vue的index.html首頁文件即可
nginx這里不做解釋,編譯安裝好即可
server { #用戶訪問域名或者ip,默認是nginx的80端口
listen 80;
server_name 192.168.119.12; #url匹配 / 也就是請求地址是192.168.119.12時,進入此location,返回vue的dist下index.html路飛學城首頁
location / {
root /opt/07-luffy_project_01/dist;
index index.html;
}
}
三、配置后端代碼,解決虛擬環(huán)境,保證項目干凈隔離
激活虛擬環(huán)境venv1,在虛擬環(huán)境下,安裝路飛項目所需的依賴模塊
[root@web02 opt]# cat requirements.txtcertifi==2018.11.29chardet==3.0.4crypto==1.4.1Django==2.1.4django-redis==4.10.0django-rest-framework==0.1.0djangorestframework==3.9.0idna==2.8Naked==0.1.31pycrypto==2.6.1pytz==2018.7PyYAML==3.13redis==3.0.1requests==2.21.0shellescape==3.4.1urllib3==1.24.1uWSGI==2.0.17.1
這個路飛代碼數(shù)據(jù)庫用的是sqllite,不需要配置數(shù)據(jù)庫了
購物車用都的是redis,因此要啟動服務器的redis-server服務端
redis-server /etc/redis.conf
ps -ef|grep redis
redis-server *:6379
通過uwsgi啟動路飛項目
[uwsgi]
# Django-related settings
# the base directory (full path)
chdir = /opt/luffy_boy
# Django's wsgi file
module = luffy_boy.wsgi
# the virtualenv (full path)
home = /opt/venv1
# process-related settings
# master
master = true
# maximum number of worker processes
processes = 1
# the socket (use the full path to be safe
socket = 0.0.0.0:9000
# clear environment on exit
vacuum = true#后臺運行uwsgidaemonize=yes
(venv1) [root@web02 opt]# uwsgi --ini luffy_boy/uwsgi.ini
四、配置nginx,此步重要
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name 192.168.119.12;
location / {
root /opt/07-luffy_project_01/dist;
index index.html; #這一條參數(shù)確保vue頁面刷新時候,不會出現(xiàn)404頁面 ?try_files $uri $uri/ /index.html;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 8000;
server_name 192.168.119.12;
location / {
uwsgi_pass 0.0.0.0:9000;
include /opt/nginx/conf/uwsgi_params;
}
location /static {
alias /opt/static;
}
}
}
原理圖

|