第一:安装工具及依赖
[root@localhost 20190903]# yum install -y wget make cmake gcc gcc-c++
[root@localhost 20190903]# yum install -y pcre pcre-devel lib zlib-devel
[root@localhost 20190903]#openssl openssl-devel
第二:下载及解压nginx
下载nginx-1.12.2.tar.gz安装包
[root@localhost 20190903] #wget http://nginx.org/download/nginx-1.12.2.tar.gz
创建nginx的解压之后文件(安装nginx文件)
[root@localhost local]# mkdir nginx
解压nginx
[root@localhost 20190903]# tar -xzvf nginx-1.12.2.tar.gz -C /usr/local/nginx/
第三:编译安装
进入该目录/usr/local/nginx/nginx-1.12.2
[root@localhost nginx-1.12.2]#
./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_realip_module --http-client-body-temp-path=/usr/local/var/tmp/nginx/client
Configuration summary
+ using system PCRE library
+ using system OpenSSL library
+ using system zlib library
nginx path prefix: "/usr/local/nginx"
nginx binary file: "/usr/local/nginx/sbin/nginx"
nginx modules path: "/usr/local/nginx/modules"
nginx configuration prefix: "/usr/local/nginx/conf"
nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
nginx pid file: "/usr/local/nginx/logs/nginx.pid"
nginx error log file: "/usr/local/nginx/logs/error.log"
nginx http access log file: "/usr/local/nginx/logs/access.log"
nginx http client request body temporary files: "/usr/local/var/tmp/nginx/client"
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"
[root@localhost nginx-1.12.2]# make && make install
第四:检查安装
启动Nginx(进入/usr/local/nginx/sbin目录),启动完之后检查nginx是否已经正常启动: [root@localhost sbin]# ./nginx
[root@localhost sbin]# ps -ef |grep nginx
root 52169 1 0 14:09 ? 00:00:00 nginx: master process ./nginx
nobody 52170 52169 0 14:09 ? 00:00:00 nginx: worker process
root 52291 46547 0 14:10 pts/0 00:00:00 grep --color=auto nginx
第五:测试nginx访问
通过浏览器访问nginx欢迎页,在地址栏输入:http://192.168.126.139/
,如下图所示。
特别注意要关闭防火墙。
第六:配置nginx
nginx最重要的配置文件nginx.conf
[root@localhost nginx-1.12.2]# vi /usr/local/nginx/conf/nginx.conf
配置文件里可以添加多个server,server监听的端口不同,可以根据需要让nginx代理多个端口,每个端口指定去做某些事情。
第其:创建nginx自启动
nginx自启动命令脚本如下所示:
#! /bin/bash
# chkconfig: - 85 15
PATH=/usr/local/nginx
DESC="nginx daemon"
NAME=nginx
DAEMON=$PATH/sbin/$NAME
CONFIGFILE=$PATH/conf/$NAME.conf
PIDFILE=$PATH/logs/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME
set -e
[ -x "$DAEMON" ] || exit 0
do_start() {
$DAEMON -c $CONFIGFILE || echo -n "nginx already running"
}
do_stop() {
$DAEMON -s stop || echo -n "nginx not running"
}
do_reload() {
$DAEMON -s reload || echo -n "nginx can't reload"
}
case "$1" in
start)
echo -n "Starting $DESC: $NAME"
do_start
echo "."
;;
stop)
echo -n "Stopping $DESC: $NAME"
do_stop
echo "."
;;
reload|graceful)
echo -n "Reloading $DESC configuration..."
do_reload
echo "."
;;
restart)
echo -n "Restarting $DESC: $NAME"
do_stop
do_start
echo "."
;;
*)
echo "Usage: $SCRIPTNAME {start|stop|reload|restart}" >&2
exit 3
;;
esac
exit 0
在/etc/init.d/目录下创建nginx
[root@localhost nginx]# cd /etc/init.d/
[root@localhost init.d]# vi nginx
修改nginx的权限
[root@localhost init.d]# chmod a+x nginx
注册成服务
[root@localhost init.d]# chkconfig --add nginx
设置开机启动
[root@localhost init.d]# chkconfig nginx on
测试自启动: