python+nginx+uwsgi的服務器搭建
搭建一個python的服務器,用來接收API的開發,以及進行數據爬蟲相關分析
阿里云的云服務器(ECS)可以選擇多種操作系統,打算用它運行 Drupal 或者 WordPress ,你最好選擇 Linux 系統,這篇文章的演示是基于阿里云的 CentOS 操作系統的服務器。我們在上面搭建一個 nginx + ots + oss + uwsgi,我們不過多解釋什么是什么,而是著重講流程與方法,想了解具體的細節,去搜索下吧:)這個手冊是在阿里云上測試的,不過應該也適用于其它使用 CentOS 系統的服務器。
購買服務器
在阿里云網站進行購買服務器,選擇對應的操作系統如下圖所示
遠程控制你的服務器
遠程控制 Linux 類型的系統的服務器,比如 CentOS 系統的服務器,一般不像 Windows 服務器那樣,使用圖形界面的遠程控制。我們需要使用命令行工具,遠程連接到服務器,然后使用命令去控制服務器。Windows 用戶可以使用 Putty ,Mac 用戶可以使用系統自帶的終端工具或者一些第三方的工具eg: SSH Shell。然后用 ssh 命令,連接到你的服務器。像這樣:
ssh:root@你的服務器地址
倉庫
為了可以使用 CentOS 系統的包管理工具去安裝更多的東西,我們需要單獨安裝一些額外的軟件倉庫。
EPEL
sudo yum install epel-release -y
IUS
sudo yum install https://centos7.iuscommunity.org/ius-release.rpm -y
IUS 倉庫里面有我們需要的一些新的軟件包,比如 PHP 7。注意我們安裝的是為 CentOS 7 這個系統準備的 IUS 倉庫,如果你的系統是 CentOS 6,你需要到 IUS 網站上去查找適合的倉庫的下載地址。
安裝NGINX
NGINX 這個軟件包已經包裝在 EPEL 倉庫里了,之前我們已經安裝了這個倉庫,所以可以直接使用包管理工具去安裝 NGINX。
sudo yum install nginx -y
啟動
sudo systemctl start nginx
開啟自啟動
sudo systemctl enable nginx
服務啟動以后,你就可以在瀏覽器上使用服務器的 IP 地址,或者指向這個地址的域名訪問服務器指定的目錄了。你會看到類似下面的圖片,就說明可以了
配置 nginx 虛擬主機
- 配置nginx的配置文件
- 重新啟動nginx {sudo systemctl reload nginx}
配置文件如下圖所示
nginx的默認配置
nginx的配置
安裝uwsgi
通過pip進行安裝,如果安裝權限不夠,用sudo進行安裝
sudo pip install uwsgi
uwsgi啟動
sudo uwsgi --ini /etc/uwsgi.ini
uwsgi 的相關配置
這樣就能訪問相關網頁了
配置出現錯誤的信息
*** Starting uWSGI 2.0.15 (64bit) on [Wed Dec 13 14:47:37 2017] ***
compiled with version: 4.8.5 20150623 (Red Hat 4.8.5-16) on 13 December 2017 14:46:26
os: Linux-3.10.0-693.2.2.el7.x86_64 #1 SMP Tue Sep 12 22:26:13 UTC 2017
nodename: moshimol
machine: x86_64
clock source: unix
pcre jit disabled
detected number of CPU cores: 1
current working directory: /home/moshimol
writing pidfile to /var/run/uwsgi.pid
detected binary path: /usr/bin/uwsgi
setgid() to 995
setuid() to 997
your processes number limit is 7283
your memory page size is 4096 bytes
***
detected max file descriptor number: 65535
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
Listen queue size is greater than the system max net.core.somaxconn (128).
說明listen 監聽的過多,設置uwsgi的配置文件到相關100
2017/12/13 15:00:00 [error] 6569#0: *307 connect() failed (111: Connection refused) while connecting to upstream, client: 115.236.171.18, server: 106.14.207.197, request: "GET /fitness/env HTTP/1.1", upstream: "uwsgi://127.0.0.1:6000", host: "106.14.207.197"
2017/12/13 15:00:02 [error] 6569#0: *307 connect() failed (111: Connection refused) while connecting to upstream, client: 115.236.171.18, server: 106.14.207.197, request: "GET /fitness/env HTTP/1.1", upstream: "uwsgi://127.0.0.1:6000", host: "106.14.207.197"
2017/12/13 15:00:02 [error] 6569#0: *307 open() "/usr/share/nginx/html/favicon.ico" failed (2: No such file or directory), client: 115.236.171.18, server: 106.14.207.197, request: "GET /favicon.ico HTTP/1.1", host: "106.14.207.197", referrer: "http://106.14.207.197/fitness/env"
一個API就寫成功了
http://106.14.207.197/fitness/env