一、安裝Nginx及其依賴php
首先是老套路,使用ssh連接服務器,還記得之前的代碼嗎?html
ssh -t 用戶名@服務器IP或者域名 -p 22
<!--用戶名通常是root,方便操做,個人登陸代碼以下-->
ssh -t root@acheng1314.cn -p 22複製代碼
在終端中輸入上面命令按下回車,要求咱們輸入密碼,這個密碼是不可見的,因此必定要輸入正確。linux
連接到服務器後,咱們切換到經常使用的安裝路徑,固然我服務器上面的安裝路徑是/usr/src,接着開始在終端操做:nginx
<!--切換到安裝目錄下-->
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
<!--設置、編譯、安裝-->
./configure
make
make 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.gz
tar -zxvf nginx-1.4.2.tar.gz
cd nginx-1.4.2
<!--設置Nginx安裝目錄/opt/nginx,且添加ssl支持-->
./configure --prefix=/opt/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre
make
make install複製代碼
到這裏來說,咱們的nginx安裝完成了,可是咱們還須要作更多的事情,那就是配置服務器,添加ssl訪問,設置服務和開機啓動vim
配置服務器tomcat
互聯網上關於服務器設置的不少,可是準確闡述的卻不是那麼多,而我恰好是在看了他們的東西后就呵呵了。正確的配置方法以下:bash
<!--切換到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;
}
}
}複製代碼
其實在寫這個的時候必須注意的是,無論什麼應用程序端口不能衝突!好比說個人nginx是綁定的80端口,若是tomcat再設定80端口,那麼個人設置就算是綁定到localhost去也是會轉發失敗的!畢竟網絡端口只能一個應用程序佔用。網絡
<!--檢查咱們的設置是否正確,正確或者錯誤都有對應的提示-->
/opt/nginx/sbin/nginx -t
<!--配置正確,則啓用nginx-->
/opt/nginx/sbin/nginx
<!--從新載入配置文件-->
/opt/nginx/sbin/nginx -t
<!--固然到了這裏的時候確定還不能通行,畢竟咱們防火牆還把443端口攔截的,因此接着走起來。-->
<!--添加443端口到防火牆-->
/sbin/iptables -I INPUT -p tcp --dport 443 -j ACCEPT
<!--保存防火牆配置-->
/etc/rc.d/init.d/iptables save
<!--是配置文件生效-->
/etc/init.d/iptables status複製代碼
走到這一步,咱們能夠測試一下服務器了,按照正常的來說,我如今的服務器已是http和https都已經徹底支持了。session
三、設置服務和自啓
其實說來,這裏基本也沒啥注意的,只要nginx路徑設置正確便可。
#!/bin/sh
# Name:nginx4comex
# nginx - this script starts and stops the nginx daemon
#
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /opt/nginx/conf/nginx.conf
# pidfile: /comexHome/nginx/nginx.pid
#
# Created By http://comexchan.cnblogs.com/
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
NGINX_DAEMON_PATH="/opt/nginx/sbin/nginx"
NGINX_CONF_FILE="/opt/nginx/conf/nginx.conf"
NGINX_LOCK_FILE="/var/lock/subsys/nginx4comex"
prog=$(basename $NGINX_DAEMON_PATH)
start() {
[ -x $NGINX_DAEMON_PATH ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
echo -n $"Starting $prog: "
daemon $NGINX_DAEMON_PATH -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $NGINX_LOCK_FILE
return $retval
}
stop() {
echo -n $"Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $NGINX_LOCK_FILE
return $retval
}
restart() {
configtest || return $?
stop
start
}
reload() {
configtest || return $?
echo -n $"Reloading $prog: "
killproc $NGINX_DAEMON_PATH -HUP
RETVAL=$?
echo
}
force_reload() {
restart
}
configtest() {
$NGINX_DAEMON_PATH -t -c $NGINX_CONF_FILE
}
rh_status() {
status $prog
}
rh_status_q() {
rh_status >/dev/null 2>&1
}
case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit 2
esac複製代碼
上面的代碼就是用來建立服務的代碼,將他們保存在nginx4comex文件中(這個文件我仍在了/opt/nginx目錄下,同樣使用vim編寫)。注意下面的代碼和你的配置對應便可。
NGINX_DAEMON_PATH="/opt/nginx/sbin/nginx"
NGINX_CONF_FILE="/opt/nginx/conf/nginx.conf"複製代碼
接着咱們繼續終端指令操做。
<!--受權nginx4comex可執行-->
chmod u+x nginx4comex
<!--拷貝nginx4comex到/etc/init.d目錄-->
cp nginx4comex /etc/init.d
<!--檢查運行狀態-->
service nginx4comex status
<!--添加到啓動,先vim打開啓動文件,而後添加啓動代碼-->
vim /etc/rc.local
<!--添加的啓動代碼以下-->
/etc/init.d/nginx4comex start
<!--至此咱們的啓動也添加完成,如今重啓檢查效果-->
reboot複製代碼
最後來講咱們已經設置了nginx代理tomcat,還設置了對應的服務器程序自啓動。
注意!nginx4comex是不能被chkconfig的,具體緣由我也不清楚,可是原做者的文章中確實用了chkconfig的方法加入了啓動,對linux有興趣的能夠去試一試。
若是你承認我所作的事情,而且認爲我作的事對你有必定的幫助,但願你也能打賞我一杯咖啡,謝謝。