本指南记录了在 CentOS 6.5 环境中部署 Nginx 的具体步骤,包括服务配置与开机自启动设置,供参考备忘。
安装 Nginx 及其依赖组件
第一步:安装 Nginx 及其相关依赖组件。
首先通过 SSH 连接至目标服务器,沿用常规的远程操作方式。
ssh -t 用户名@服务器IP或者域名 -p 22
ssh -t root@acheng1314.cn -p 22
执行连接命令后需输入密码,终端不会显示输入字符,务必确保密码准确无误。
成功登录服务器后,切换至常用的软件安装目录,例如 /usr/src,随后在终端中执行后续操作指令。
cd /usr/src
mkdir Nginx
yum -y install zlib zlib-devel openssl openssl--devel pcre pcre-devel
wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.40.tar.gz
tar -zxvf pcre-8.40.tar.gz
cd pcre-8.40
./configure
make
make install
cd ..
wget http://zlib.net/zlib-1.2.11.tar.gz
tar -zxvf zlib-1.2.11.tar.gz
cd zlib-1.2.11
./configure
make
make install
cd ..
wget http://www.openssl.org/source/openssl-fips-2.0.14.tar.gz
tar -zxvf openssl-fips-2.0.14.tar.gz
yum -y install openssl openssl-devel
wget http://nginx.org/download/nginx-1.4.2.tar.gz
tar -zxvf nginx-1.4.2.tar.gz
cd nginx-1.4.2
./configure --prefix=/opt/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre
make
make install
至此,Nginx 的基础安装工作已完成,但还需进一步进行服务器配置,包括添加 SSL 访问支持以及设置系统服务和开机自启动项。
第二步:配置服务器环境。
网络上关于服务器设置的资料繁多,但准确详尽的说明较少。以下提供经过验证的正确配置流程。
cd /opt/nginx/conf
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
/opt/nginx/sbin/nginx
/opt/nginx/sbin/nginx -t
/sbin/iptables -I INPUT -p tcp --dport 443 -j ACCEPT
/etc/rc.d/init.d/iptables save
/etc/init.d/iptables status
完成上述步骤后,可测试服务器状态。正常情况下,此时服务器应已完全支持 HTTP 和 HTTPS 协议。
第三步:设置系统服务与开机自启动。
此部分操作相对简单,关键在于确保 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"
接下来继续在终端中执行相关指令以完成服务注册。
chmod u+x nginx4comex
cp nginx4comex /etc/init.d
service nginx4comex status
vim /etc/rc.local
/etc/init.d/nginx4comex start
reboot
最终实现了 Nginx 作为 Tomcat 代理的功能,并配置了服务器程序的自启动机制。
注意:nginx4comex 脚本可能无法直接被 chkconfig 识别,具体原因不明,但原作者文章中使用了 chkconfig 方法加入启动列表,对 Linux 感兴趣的用户可自行尝试验证。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持本站。