1.准備工作
1.1已購買阿里雲雲服務器ECS
1.2域名已購買並解析成功
1.3安裝有遠程鏈接工具Xshell和文件傳輸工具Xftf,並鏈接上ECS實例
2.安裝nginx(在Xshell操作)
2.1檢查安裝環境
Nginx是C寫的,需要用GCC編譯;Nginx的Rewrite和HTTP模塊會用到PCRE(Perl Compatible Regular Expression);Nginx中的Gzip用到zlib[1]。
因此在安裝nginx之前需要檢查當前環境是否已經安裝有GCC、PCRE、Zlib還有一個就是OpenSSL。
使用rpm -qa命令查看GCC、Zlib、PCRE和OpenSSL是否已安裝:
# rpm -qa gcc
gcc-4.4.7-17.el6.x86_64
# rpm -qa pcre
pcre-7.8-7.el6.x86_64
# rpm -qa zlib
zlib-1.2.3-29.el6.x86_64
# rpm -qa openssl
openssl-1.0.1e-48.el6_8.3.x86_64
可知,ECS centos6.8系統下已安裝有所需要的以上軟件,下一步直接安裝nginx。
3.下載安裝nginx
3.1下載
從http://nginx.org/download/下載ngixn安裝包到/usr/local/src路徑下(可指定),此次選擇的是nginx-1.10.3.tar.gz:
# cd /usr/local/src
# wget http://nginx.org/download/nginx-1.10.3.tar.gz
3.2解壓
# tar -zxvf nginx-1.10.3.tar.gz
3.3安裝
3.3.1源碼編譯准備
使用./configure進行安裝環境檢查和安裝配置(由於不確定默認安裝路徑,故指定安裝路徑為/usr/local/nginx),此命令會生成 Makefile:
# ./configure --prefix=/usr/local/nginx
3.3.2編譯
# make
3.3.3安裝
# make install
4.查看安裝
# whereis nginx
nginx: /etc/nginx /usr/lib64/nginx /usr/local/nginx /usr/share/nginx
除了指定安裝了nginx的路徑/usr/local/nginx,其他路徑的應該是nginx配置文件、可執行文件以及其他資源文件默認存放的位置吧。
5.啟動nginx
nginx可執行文件存放路徑: /usr/local/nginx/sbin/nginx
可進入/usr/local/nginx/sbin路徑,輸入 ./nginx -h命令,查看命令幫助:
# ./nginx -h
nginx version: nginx/1.10.3
Usage: nginx [-?hvVtTq] [-s signal] [-c filename] [-p prefix] [-g directives]
Options:
-?,-h : this help
-v : show version and exit
-V : show version and configure options then exit
-t : test configuration and exit
-T : test configuration, dump it and exit
-q : suppress non-error messages during configuration testing
-s signal : send signal to a master process: stop, quit, reopen, reload
-p prefix : set prefix path (default: /usr/local/nginx/)
-c filename : set configuration file (default: conf/nginx.conf)
-g directives : set global directives out of configuration file
也可以通過ps -A命令查看nginx進程狀態,確認nginx已啟動:
#ps -A | grep nginx
6.此時,輸入服務器域名或公網IP,可以nginx的歡迎頁面,表明nginx web服務器已經成功安裝,下一步可進行相關配置。
至於為什么會出現該頁面,可以查看nginx.conf,所在路徑為:/usr/local/nginx/conf/ngin.conf,進入/usr/local/nginx/conf路徑,輸入以下命令:
# vi nginx.conf
產看到nginx.conf配置文件內容:
location / { root html; index index.html index.htm; } #error_page 404 /404.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; #}
其中:
listener 監聽端口
server_name 監聽域名
location{}是用來為匹配的 URI 進行配置,URI 即語法中的“/uri/”。
location / { }匹配任何查詢,因為所有請求都以 / 開頭。
root指定對應uri的資源查找路徑,html為相對路徑,在我的服務器上完整路徑為/usr/local/nginx/html/,
該路徑下有一個index.html的文件,便是輸入ECS實例IP或綁定的域名返回的響應內容。[2]
7.上線靜態網站到服務器來完成網站簡單發布
比如你已經建立了一個網站,資源文件都存放在test文件夾中,該文件下有一個index.html,路徑為test/index.html。
7.1上傳網站文件
通過xftp把test文件上傳到/usr/local/nginx/html路徑下
7.2修改nginx.conf文件
# vi ./nginx.conf
更改nginx.conf配置文件中location / {}中內容為:
location / {
root html/test;
index index.html index.htm;
}
然后,依次鍵入esc : wq Enter保存修改並退出
7.3重啟nginx
進入/usr/local/nginx/sbin路徑:
# ./nginx -s reload
然后,輸入你的域名,就可以看到test/inde.html頁面了,至此,完成nginx的安裝和簡單配置,不過此時服務器還只能處理靜態資源,要提供動態的web服務,則需要其他模塊來組合使用。
(ps.網上相關的內容有很多,處理更復雜需求的配置也有很多,不過我只是想通過解決簡單的目的來梳理前端到后端的認知體系!我還是小白!)
[1]http://www.cnblogs.com/jtlgb/p/5809808.html
[2]http://www.cnblogs.com/skynet/p/4146083.html