共计 6146 个字符,预计需要花费 16 分钟才能阅读完成。
注:以下所有操作均在 CentOS 6.5 x86_64 位系统下完成。
# 准备工作 #
在安装 Nginx 之前,请确保已经使用 yum 安装了 pcre 等基础组件,具体见《CentOS 安装 LNMP 环境的基础组件》。
#############################################
CentOS 安装 LNMP 环境的基础组件
在安装 LNMP 环境之前,请确保已经使用 yum 安装了以下各类基础组件(如果系统已自带,还可以考虑 yum update 下基础组件):
- gcc
- cmake
- openssl+openssl-devel
- pcre+pcre-devel
- bzip2+bzip2-devel
- libcurl+curl+curl-devel
- libjpeg+libjpeg-devel
- libpng+libpng-devel
- freetype+freetype-devel
- php-mcrypt+libmcrypt+libmcrypt-devel
- libxslt+libxslt-devel
- gmp+gmp-devel
- libxml2+libxml2-devel
- mhash
- ncurses+ncurses-devel
- xml2
#############################################
然后创建 www 的用户组和用户,并且不允许登录权限:
# id www | |
id: www:无此用户 | |
# groupadd www | |
# useradd -g www -s /sbin/nologin www | |
# id www | |
uid=501(www) gid=501(www) 组 =501(www) |
#Nginx 的安装 #
开始下载 Nginx 并进行编译安装:
# cd /usr/local/src | |
# wget http://nginx.org/download/nginx-1.6.2.tar.gz | |
# tar zxf nginx-1.6.2.tar.gz | |
# cd nginx-1.6.2 | |
# ./configure --prefix=/usr/local/nginx-1.6.2 --group=www --user=www --with-http_ssl_module --with-pcre --with-http_stub_status_module --with-http_gzip_static_module | |
Configuration summary | |
+ using system PCRE library | |
+ using system OpenSSL library | |
+ md5: using OpenSSL library | |
+ sha1: using OpenSSL library | |
+ using system zlib library | |
nginx path prefix: "/usr/local/nginx-1.6.2" | |
nginx binary file: "/usr/local/nginx-1.6.2/sbin/nginx" | |
nginx configuration prefix: "/usr/local/nginx-1.6.2/conf" | |
nginx configuration file: "/usr/local/nginx-1.6.2/conf/nginx.conf" | |
nginx pid file: "/usr/local/nginx-1.6.2/logs/nginx.pid" | |
nginx error log file: "/usr/local/nginx-1.6.2/logs/error.log" | |
nginx http access log file: "/usr/local/nginx-1.6.2/logs/access.log" | |
nginx http client request body temporary files: "client_body_temp" | |
nginx http proxy temporary files: "proxy_temp" | |
nginx http fastcgi temporary files: "fastcgi_temp" | |
nginx http uwsgi temporary files: "uwsgi_temp" | |
nginx http scgi temporary files: "scgi_temp" | |
# make && make install | |
# ln -s /usr/local/nginx-1.6.2/ /usr/local/nginx | |
# chown -R www:www /usr/local/nginx | |
# chown -R www:www /usr/local/nginx-1.6.2 |
把 Nginx 的 sbin 目录加入 PATH:
# vim /etc/profile | |
export PATH=$PATH:/usr/local/mysql/bin:$JAVA_HOME/bin:/usr/local/nginx/sbin | |
# source /etc/profile |
查看 Nginx 的版本信息,并且检验上一步骤是否成功:
nginx version: nginx/1.6.2 | |
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-4) (GCC) | |
TLS SNI support enabled | |
configure arguments: --prefix=/usr/local/nginx-1.6.2 --group=www --user=www --with-http_ssl_module --with-pcre --with-http_stub_status_module |
至此,Nginx 已经安装完毕。
#Nginx 的启动 / 重启 / 关闭 #
给 Nginx 的 webapp 配置相关路径(这里是为了后面运维管理方便,可以把不同的 Web 项目放到该目录下):
# mkdir -p /data/www
简单修改下配置文件:
user www; | |
worker_processes 1; | |
events {worker_connections 1024; | |
} | |
http { | |
include mime.types; | |
default_type application/octet-stream; | |
sendfile on; | |
keepalive_timeout 65; | |
gzip on; | |
server {listen 80; | |
server_name localhost; | |
location / { | |
root html; | |
index index.html index.htm; | |
} | |
} | |
} |
开始启动 Nginx:
# nginx
这个时候打开浏览器访问地址 http://youripaddress 应该可以看到:
至此,Nginx 已经启动成功。
一般来说,当修改了 nginx.conf 配置文件后,可以直接重启让配置生效,重启之前一般检测下配置文件是否正确:
# nginx -t | |
nginx: the configuration file /usr/local/nginx-1.6.2/conf/nginx.conf syntax is ok | |
nginx: configuration file /usr/local/nginx-1.6.2/conf/nginx.conf test is successful | |
# nginx -s reload |
另外,重启也可以通过发信号的方式:
# kill -HUP ${master_pid}
关闭的命令如下:
# nginx -s quit | |
# nginx -s stop |
注:quit 表示等请求结束后再关闭,stop 表示立刻关闭。
也可以通过发信号的方式来关闭:
kill -QUIT ${nginx_master} | |
kill -TERM ${nginx_master} | |
kill -9 ${nginx_master} |
注:-QUIT 表示从容停止,等所有请求结束后再关闭进程;TERM 则表示立刻关闭进程;- 9 表示强制关闭。
为了以后管理上的方便,我们这里写个启动脚本,以后就可以用 service 命令来启动,如下:
# vim /etc/init.d/nginxd | |
#!/bin/sh | |
# chkconfig: 2345 85 15 | |
# description:Nginx Server | |
NGINX_HOME=/usr/local/nginx-1.6.2 | |
NGINX_SBIN=$NGINX_HOME/sbin/nginx | |
NGINX_CONF=$NGINX_HOME/conf/nginx.conf | |
NGINX_PID=$NGINX_HOME/logs/nginx.pid | |
NGINX_NAME="Nginx" | |
. /etc/rc.d/init.d/functions | |
if [! -f $NGINX_SBIN ] | |
then | |
echo "$NGINX_NAME startup: $NGINX_SBIN not exists! " | |
exit | |
fi | |
start() {$NGINX_SBIN -c $NGINX_CONF | |
ret=$? | |
if [$ret -eq 0 ]; then | |
action $"Starting $NGINX_NAME: " /bin/true | |
else | |
action $"Starting $NGINX_NAME: " /bin/false | |
fi | |
} | |
stop() {kill `cat $NGINX_PID` | |
ret=$? | |
if [$ret -eq 0 ]; then | |
action $"Stopping $NGINX_NAME: " /bin/true | |
else | |
action $"Stopping $NGINX_NAME: " /bin/false | |
fi | |
} | |
restart() { | |
stop | |
start | |
} | |
check() {$NGINX_SBIN -c $NGINX_CONF -t | |
} | |
reload() {kill -HUP `cat $NGINX_PID` && echo "reload success!" | |
} | |
relog() {kill -USR1 `cat $NGINX_PID` && echo "relog success!" | |
} | |
case "$1" in | |
start) | |
start | |
;; | |
stop) | |
stop | |
;; | |
restart) | |
restart | |
;; | |
check|chk) | |
check | |
;; | |
status) | |
status -p $NGINX_PID | |
;; | |
reload) | |
reload | |
;; | |
relog) | |
relog | |
;; | |
*) | |
echo $"Usage: $0 {start|stop|restart|reload|status|check|relog}" | |
exit 1 | |
esac | |
# chmod +x /etc/init.d/nginxd | |
# chkconfig nginxd on |
这样子就可以通过 service 来启动:
# service nginxd start
#Nginx 的安全配置 #
1、首先设置不允许目录浏览,默认配置即为不允许。
autoindex off
2、开启访问日志,nginx 中默认已开启,这里我们后续为了运维管理上的方便最好把日志单独放到 /data 目录下。
access_log /data/www/logs/localhost.access.log
3、确保目录的安全,由于 Nginx 使用的是 www 用户启动,黑客入侵服务器成功后将获得 www 用户的权限,所以需要确保网站 Web 目录和文件的属主与启动用户不同,防止网站被黑客恶意篡改和删除。网站 Web 目录和文件的属主可以设置为 root,其中 Web 目录权限统一设置为 755,Web 文件权限统一设置为 644。只有上传目录等可读写权限的目录可以被设置为 777,为了防止黑客上传木马到 777 权限目录中,还必须保证该 777 权限的目录没有执行脚本的权限。这里有两种情况处理:
1)对于使用 PHP 的业务,配置如下:
location ~* ^/data/www/logs/.*\.(php|php5)$ {deny all;}
注:当然最安全的还是给 PHP 的可执行目录采用白名单的方式,这个我们在 PHP 的安装一节中再详细介绍。
2)对于非使用 PHP 的业务(如 python、cgi 等),则需要禁止外部访问 777 目录,配置如下:
location ~ ^/data/www/logs/ {deny all;}
4、对于管理目录,需要限制访问的 IP 地址,比如这里限制访问 nginx 状态的:
server {location /nginx-admin { | |
stub_status on; | |
access_log logs/nginx-admin.log; | |
allow 11.12.23.0/24; | |
deny all; | |
} | |
location /admin {...} | |
} |
注:上面配置的 11.12.23.0/24 指的就是当前运维客户端的 IP 地址段。
在允许 IP 的机器上输入地址应该可以看到:
而不允许的用户访问应该是不可以的,会显示 403 错误,比如:
5、把 Nginx 默认的首页等页面删除,使用业务自己的首页来顶替。
6、不允许 IP 直接访问服务器,这样的好处是怕当 IP 地址泄漏出去之后,被人用别的域名来指向了这个 IP 地址,可以设置让其返回 500 等错误码。比如:
server {listen 80 default; | |
return 500; | |
} | |
server {listen 80; | |
server_name www.linuxidc.com tencent.com; | |
root /data/www/linuxidc; | |
access_log /data/logs/nginx/linuxidc.access.log; | |
error_log /data/logs/nginx/linuxidc.error.log; | |
} |
注:上面的配置表示当使用 IP 地址直接访问时将出错,而使用域名访问时(比如请求 linuxidc.com 则正常)。
更多 Nginx 相关教程见以下内容:
CentOS 6.2 实战部署 Nginx+MySQL+PHP http://www.linuxidc.com/Linux/2013-09/90020.htm
搭建基于 Linux6.3+Nginx1.2+PHP5+MySQL5.5 的 Web 服务器全过程 http://www.linuxidc.com/Linux/2013-09/89692.htm
CentOS 6.3 下 Nginx 性能调优 http://www.linuxidc.com/Linux/2013-09/89656.htm
CentOS 6.3 下配置 Nginx 加载 ngx_pagespeed 模块 http://www.linuxidc.com/Linux/2013-09/89657.htm
Ubuntu 16.04 LTS 上安装 Nginx、MariaDB 和 HHVM 运行 WordPress http://www.linuxidc.com/Linux/2016-10/136435.htm
Nginx 安装配置使用详细笔记 http://www.linuxidc.com/Linux/2014-07/104499.htm
Linux(RHEL7.0)下安装 Nginx-1.10.2 http://www.linuxidc.com/Linux/2016-10/136484.htm
Nginx 日志过滤 使用 ngx_log_if 不记录特定日志 http://www.linuxidc.com/Linux/2014-07/104686.htm
Nginx 的详细介绍:请点这里
Nginx 的下载地址:请点这里
本文永久更新链接地址:http://www.linuxidc.com/Linux/2016-12/137984.htm
