共计 6167 个字符,预计需要花费 16 分钟才能阅读完成。
一、简介
Nginx(“engine x”) 是一款是由俄罗斯的程序设计师 Igor Sysoev 所开发高性能的 Web 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器。在高连接并发的情况下,Nginx 是 Apache 服务器不错的替代品。
二、准备
1、环境
系统平台:Red Hat Enterprise Linux Server release 7.3 (Maipo)
内核版本:3.10.0-514.el7.x86_64
2、安装编译工具和库文件
yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel
3、安装 pcre
PCRE 作用是让 Ngnix 支持 Rewrite 功能。
查看是否安装 pcre
# pcre-config –version
上述表明已安装。
若未安装,参照以下步骤:
1)下载
地址:https://sourceforge.net/projects/pcre/files/pcre/
2)解压安装包:
# tar zxvf pcre-8.35.tar.gz
3) 编译安装
# cd pcre-8.35
# ./configure
# make && make install
三、安装
1、下载 nginx 安装包
http://nginx.org/download/
2、解压
# tar zxvf nginx-1.10.2.tar.gz
3、编译
# ./configure –prefix=/usr/local/nginx –with-http_stub_status_module –with-http_ssl_module –with-pcre
4、安装
# make
# make install
5、测试
查看 nginx 版本
# /usr/local/nginx/sbin/nginx -v
显示版本信息,证明已安装成功
四、配置
1、创建用户
创建 Nginx 运行使用的用户 ruready:
# /usr/sbin/groupadd ruready
# /usr/sbin/useradd -g ruready ruready
2、配置 nginx.conf
# vi /usr/local/nginx/conf/nginx.conf
user ruready ruready;
worker_processes 2;
error_log /usr/local/nginx/logs/error.log crit; # 日志位置和日志级别
pid /usr/local/nginx/logs/nginx.pid;
#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 65535;
events {
use epoll;
worker_connections 65535;
}
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 /usr/local/nginx/logs/access.log main;
sendfile on;
tcp_nopush on;
keepalive_timeout 60;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain application/x-Javascript text/css application/xml;
gzip_vary on;
# 下面是 server 虚拟主机的配置
server {
listen 80;# 监听端口
server_name localhost;# 域名
charset utf-8;
access_log /usr/local/nginx/logs/host.access.log main;
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;
}
# 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 server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
3、检查配置文件 ngnix.conf 的正确性
# /usr/local/nginx/sbin/nginx -t
五、启动
1、启动命令
# /usr/local/nginx/sbin/nginx
2、访问测试
3、可以通过 links 命令测试
links 127.0.0.1:8080
六、常用命令
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/sbin/nginx/nginx.conf # 加载指定配置文件启动
/usr/local/nginx/sbin/nginx -s reload # 重新载入配置文件
/usr/local/nginx/sbin/nginx -s reopen # 重启 Nginx
/usr/local/nginx/sbin/nginx -s stop # 停止 Nginx
七、其他
1、设置开机启动
echo “/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf” >> /etc/rc.local
2、添加到 service 服务
touch /etc/init.d/nginx
chmod 755 nginx // 修改脚本文件 nginx 的权限
chkconfig –add nginx // 将脚本文件加入 chkconfig 中
chkconfig –level 35 nginx on // 设置 nginx 开机在 3 和 5 级别自动启动
nginx 文件内容如下:
#!/bin/sh
#
# nginx – this script starts and stops the nginx daemon
#
# chkconfig: – 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /etc/nginx/nginx.conf
# config: /etc/sysconfig/nginx
# pidfile: /var/run/nginx.pid
# 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=”/usr/local/nginx/sbin/nginx”
prog=$(basename $nginx)
NGINX_CONF_FILE=”/usr/local/nginx/conf/nginx.conf”
[-f /etc/sysconfig/nginx] && . /etc/sysconfig/nginx
lockfile=/var/lock/subsys/nginx
start() {
[-x $nginx] || exit 5
[-f $NGINX_CONF_FILE] || exit 6
echo -n $”Starting $prog: ”
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[$retval -eq 0] && touch $lockfile
return $retval
}
stop() {
echo -n $”Stopping $prog: ”
killproc $prog -QUIT
retval=$?
echo
[$retval -eq 0] && rm -f $lockfile
return $retval
killall -9 nginx
}
restart() {
configtest || return $?
stop
sleep 1
start
}
reload() {
configtest || return $?
echo -n $”Reloading $prog: ”
killproc $nginx -HUP
RETVAL=$?
echo
}
force_reload() {
restart
}
configtest() {
$nginx -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
Nginx、Apache 工作原理及 Nginx 为何比 Apache 高效 http://www.linuxidc.com/Linux/2017-03/141896.htm
CentOS 7 下 Nginx 服务器的安装配置 http://www.linuxidc.com/Linux/2017-04/142986.htm
CentOS 上安装 Nginx 服务器实现虚拟主机和域名重定向 http://www.linuxidc.com/Linux/2017-04/142642.htm
CentOS 6.8 安装 LNMP 环境(Linux+Nginx+MySQL+PHP)http://www.linuxidc.com/Linux/2017-04/142880.htm
Nginx 服务的 SSL 认证和 htpasswd 认证 http://www.linuxidc.com/Linux/2017-04/142478.htm
Linux 中安装配置 Nginx 及参数详解 http://www.linuxidc.com/Linux/2017-05/143853.htm
Nginx 日志过滤 使用 ngx_log_if 不记录特定日志 http://www.linuxidc.com/Linux/2014-07/104686.htm
CentOS 7.2 下 Nginx+PHP+MySQL+Memcache 缓存服务器安装配置 http://www.linuxidc.com/Linux/2017-03/142168.htm
Nginx 的详细介绍 :请点这里
Nginx 的下载地址 :请点这里
本文永久更新链接地址 :http://www.linuxidc.com/Linux/2017-05/143938.htm