本文介紹了centos6.5服務器安裝Nginx設置服務和開機自啟的方法,分享給大家,也給自己留個筆記
1、安裝Nginx及其依賴
首先是老套路,使用ssh鏈接服務器,還記得以前的代碼嗎?
ssh -t 用戶名@服務器IP或者域名 -p 22<!--用戶名一般是root,方便操作,我的登錄代碼如下-->ssh -t root@acheng1314.cn -p 22
在終端中輸入上面命令按下回車,要求我們輸入密碼,這個密碼是不可見的,所以一定要輸入正確。
鏈接到服務器后,我們切換到常用的安裝路徑,當然我服務器上面的安裝路徑是/usr/src,接著開始在終端操作:
<!--切換到安裝目錄下-->cd /usr/src<!--創建Nginx文件夾用來存放Nginx相關的資源和依賴-->mkdir Nginx<!--下載資源和依賴-->yum -y install zlib zlib-devel openssl openssl--devel pcre pcre-devel <!--上面的命令一般來說會是不需要安裝什么,不過這都不重要,我們接著會重新安裝指定的版本--><!--下載pcre-->wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.40.tar.gz<!--解壓-->tar -zxvf pcre-8.40.tar.gz<!--切換到pcre目錄-->cd pcre-8.40<!--設置-->./configure<!--編譯-->make<!--安裝-->make install<!--切換到Nginx主目錄-->cd ..<!--下載及安裝zlib-->wget http://zlib.net/zlib-1.2.11.tar.gz<!--解壓-->tar -zxvf zlib-1.2.11.tar.gz<!--切換到zlib目錄-->cd zlib-1.2.11<!--設置、編譯、安裝-->./configuremakemake install<!--切換到Nginx主目錄-->cd ..<!--下載及準備ssl-->wget http://www.openssl.org/source/openssl-fips-2.0.14.tar.gz<!--解壓-->tar -zxvf openssl-fips-2.0.14.tar.gz<!--yum安裝ssl-->yum -y install openssl openssl-devel<!--下載及安裝nginx-->wget http://nginx.org/download/nginx-1.4.2.tar.gztar -zxvf nginx-1.4.2.tar.gzcd nginx-1.4.2<!--設置Nginx安裝目錄/opt/nginx,且添加ssl支持-->./configure --prefix=/opt/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcremakemake install
到這里來講,我們的nginx安裝完成了,但是我們還需要做更多的事情,那就是配置服務器,添加ssl訪問,設置服務和開機啟動
2、配置服務器
互聯網上關于服務器設置的很多,但是準確闡述的卻不是那么多,而我剛好是在看了他們的東西后就呵呵了。正確的配置方法如下:
<!--切換到nginx設置目錄-->cd /opt/nginx/conf<!--vim編輯nginx配置文件-->vi nginx.conf
我的nginx.conf如下:
#user nobody;worker_processes 1;#error_log logs/error.log;#error_log logs/error.log notice;#error_log logs/error.log info;#pid logs/nginx.pid;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; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on;# 注意這里是設置本機的相關的東西,建議不要更改 server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { root html; index index.html index.htm; # proxy_pass http://localhost; # proxy_set_header Host $host; # proxy_set_header X-Real-IP $remote_addr; # proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } #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; #} # 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訪問的,這里必須設置才能正確時https # HTTPS server # server { listen 443; server_name localhost acheng1314.cn www.acheng1314.cn; ssl on; # 這里是你申請的簽名,扔到conf下面的cert目錄中 ssl_certificate cert/214217283570796.pem; ssl_certificate_key cert/214217283570796.key; ssl_session_timeout 5m; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4; ssl_prefer_server_ciphers on; location / { # root html; # index index.html index.htm; proxy_pass http://localhost; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; }}# 這里是設置域名跳轉的,轉發這些域名到本機的8080端口,server { listen 80; server_name *.acheng1314.cn acheng1314.cn; location / { proxy_pass http://localhost:8080/; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } } }
新聞熱點
疑難解答