xshell连接Linux、ngix部署
Linux端安裝sshd服務(wù)(openssh-server)
查看防火墻:ufw(Linux默認(rèn)安裝了)
再就是客戶端了。。
?
平時(shí)在測(cè)試環(huán)境下的項(xiàng)目不能承載高并發(fā),需要部署到web server上。
web server:
? ? apache(早期)
? ? ngix(更能承載高并發(fā)、輕量級(jí),底層是I/O多路復(fù)用epoll)
如何在生產(chǎn)上部署Django?
Django的部署可以有很多方式,采用nginx+uwsgi的方式是其中比較常見的一種方式。
?uwsgi介紹
uWSGI是一個(gè)Web服務(wù)器,它實(shí)現(xiàn)了WSGI協(xié)議、uwsgi、http等協(xié)議。Nginx中HttpUwsgiModule的作用是與uWSGI服務(wù)器進(jìn)行交換。
要注意 WSGI / uwsgi / uWSGI 這三個(gè)概念的區(qū)分。
- WSGI是一種Web服務(wù)器網(wǎng)關(guān)接口。它是一個(gè)Web服務(wù)器(如nginx,uWSGI等服務(wù)器)與web應(yīng)用(如用Flask框架寫的程序)通信的一種規(guī)范。
- uwsgi是一種線路協(xié)議而不是通信協(xié)議,在此常用于在uWSGI服務(wù)器與其他網(wǎng)絡(luò)服務(wù)器的數(shù)據(jù)通信。
- 而uWSGI是實(shí)現(xiàn)了uwsgi和WSGI兩種協(xié)議的Web服務(wù)器。
- uwsgi協(xié)議是一個(gè)uWSGI服務(wù)器自有的協(xié)議,它用于定義傳輸信息的類型(type of information),每一個(gè)uwsgi packet前4byte為傳輸信息類型描述,它與WSGI相比是兩樣?xùn)|西。
?uwsgi性能非常高
uWSGI的主要特點(diǎn)如下
- 超快的性能
- 低內(nèi)存占用(實(shí)測(cè)為apache2的mod_wsgi的一半左右)
- 多app管理(終于不用冥思苦想下個(gè)app用哪個(gè)端口比較好了-.-)
- 詳盡的日志功能(可以用來分析app性能和瓶頸)
- 高度可定制(內(nèi)存大小限制,服務(wù)一定次數(shù)后重啟等)
總而言之uwgi是個(gè)部署用的好東東,正如uWSGI作者所吹噓的:
If you are searching for a simple wsgi-only server, uWSGI is not for you, but if you are building a real (production-ready) app that need to be rock-solid, fast and easy to distribute/optimize for various load-average, you will pathetically and morbidly fall in love (we hope) with uWSGI.
Uwsgi 安裝使用
1 # Install the latest stable release: 2 pip install uwsgi 3 # ... or if you want to install the latest LTS (long term support) release, 4 pip install https://projects.unbit.it/downloads/uwsgi-lts.tar.gz安裝uwsgi
基本測(cè)試
Create a file called?test.py:
# test.py
def application(env, start_response):start_response('200 OK', [('Content-Type','text/html')])return [b"Hello World"] # python3#return ["Hello World"] # python2
運(yùn)行
uwsgi --http :8000 --wsgi-file test.py 用uwsgi啟動(dòng)Django
uwsgi --http :8000 --module midware.wsgi
可以把參數(shù)寫到配置文件uwsgi.ini
[uwsgi]
http = :9000
# the local unix socket file than commnuincate to Nginx
socket = 127.0.0.1:8001
# the base directory (full path)
chdir = /home/ubuntu/midware
# Django's wsgi file
wsgi-file = midware/wsgi.py
# maximum number of worker processes
processes = 4
# thread numbers startched in each worker process
threads = 2
# monitor uwsgi status
stats = 127.0.0.1:9191
# clear environment on exit
vacuum = true 用uwsgi啟動(dòng)Django
ubuntu@ubuntu:~/midware$ uwsgi uwsgi.ini
?用uwsgitop監(jiān)控
安裝uwsgitop
sudo pip3 install uwsgitop 進(jìn)行監(jiān)控
uwsgitop :9191 #9191是配置文件中的監(jiān)控進(jìn)行127.0.0.1:9191 安裝Nginx
sudo apt-get install nginx
sudo /etc/init.d/nginx start # start nginx
sudo /etc/init.d/nginx restart # restart nginx
為你的項(xiàng)目生成Nginx配置文件
You will need the?uwsgi_params?file, which is available in the?nginx?directory of the uWSGI distribution, or from?https://github.com/nginx/nginx/blob/master/conf/uwsgi_params
Copy it into your project directory. In a moment we will tell?nginx?to refer to it.
Now create a file called mysite_nginx.conf, and put this in it:
# uwsgi_params #把這個(gè)文件從/etc/nginx/拷貝到項(xiàng)目midware下。
[uwsgi]
http = :9000
# the local unix socket file than commnuincate to Nginx
socket = 127.0.0.1:8001
# the base directory (full path)
chdir = /home/ubuntu/midware
# Django's wsgi file
wsgi-file = midware/wsgi.py #每個(gè)Django項(xiàng)目都會(huì)有一個(gè)名為wsgi.py的文件。
# maximum number of worker processes
processes = 4
# thread numbers startched in each worker process
threads = 2
# monitor uwsgi status
stats = 127.0.0.1:9191 #uwsgitop監(jiān)控的就是它
# clear environment on exit
vacuum = true
文件mysite_nginx.conf # 放到/etc/nginx/sites-enabled下,或者放到項(xiàng)目midware下,然后鏈接到/etc/nginx/sites-enabled(建立短鏈接ln -s mysite_nginx.conf /etc/nginx/sites-enabled/)# mysite_nginx.conf# the upstream component nginx needs to connect to
upstream django {# server unix:///path/to/your/mysite/mysite.sock; # for a file socketserver 127.0.0.1:8001; # for a web port socket (we'll use this first) #uwsgi.ini中的the local unix socket(socket = 127.0.0.1:8001)
}# configuration of the server
server {# the port your site will be served onlisten 8000; #用戶訪問的端口# the domain name it will serve forserver_name .example.com; # substitute your machine's IP address or FQDN #服務(wù)器名稱charset utf-8;# max upload sizeclient_max_body_size 75M; # adjust to taste #用戶請(qǐng)求最大為75M# Django medialocation /media {alias /path/to/your/mysite/media; # your Django project's media files - amend as required}location /static {alias /home/ubuntu/midware/static; # your Django project's static files - amend as required #靜態(tài)文件路徑}# Finally, send all non-media requests to the Django server.location / {uwsgi_pass django;include /home/ubuntu/midware/uwsgi_params; # the uwsgi_params file you installed #把這個(gè)文件放到項(xiàng)目目錄,或者/etc/nginx/sites-enabled/uwsgi_params}
} 啟動(dòng)uwsgi、重啟Nginx
...
Deploying static files
Before running?nginx, you have to collect all Django static files in the static folder. First of all you have to edit mysite/settings.py adding:
# 文件settings.py
DEBUG = False #生成環(huán)境中不能用DEBUG模式
STATIC_ROOT = os.path.join(BASE_DIR, "all_tatic_files")
?#把所有靜態(tài)文件放到一個(gè)文件夾下,注意不要與"static"重名,否則會(huì)被覆蓋。 ?
?最后執(zhí)行
python manage.py collectstatic #合并靜態(tài)文件
不要忘了,修改mysite_nginx.conf中的靜態(tài)文件配置 啟動(dòng)uwsgi、重啟Nginx
?
轉(zhuǎn)載于:https://www.cnblogs.com/yangxiaoling/p/6831530.html
總結(jié)
以上是生活随笔為你收集整理的xshell连接Linux、ngix部署的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 第五人格在什么场景下要用什么求生者
- 下一篇: 创维电视多少钱啊?