1、安装准备
1.1 下载安装包
nginx官网:https://nginx.org/
本次下载1.16.1
1.2 上传安装包
下载后将源码上传到/opt/apps目录下,
1.3 解压
tar -zxvf nginx-1.16.1.tar.gz
解压后生成目录如下:
这些目录主要作用如下
auto:存放 Nginx 自动安装的相关文件
conf:存放 Nginx 服务器配置文件
configure:命令,用于对即将安装的软件的配置,完成 makefile 编译文件的生成
contrib:存放由其他机构贡献的文档材料
html:存放 Nginx 欢迎页面
man:manual,手册,存放 Nginx 帮助文档
src:存放 Nginx 源码
2、安装源码编译以及Nginx依赖的库
yum install gcc gcc-c++ pcre pcre-devel openssl openssl-devel zlib zlib-devel -y
3、生成 makefile
在 Nginx 解压目录下运行 make 命令,用于完成编译。但此时会给出提示:没有指定目标,并且没有发现编译文件 makefile。编译命令 make 需要根据编译文件 makefile 进行编译,所以在编译之前需要先生成编译文件 makefile。使用 configure 命令可以生成该文件。那么,configure 命令需要配置些什么参数呢?使用–help 可以查看到可以使用的参数说明。这些参数可以分为三类:
第一类:基本信息的配置。
第二类:默认没有安装,可以指定安装的模块,使用–with 开头。Nginx 的高扩展性就
体现在这里。
第三类:默认已经安装,可以指定卸载的模块,使用–without 开头。
mkdir –p /var/tmp/nginx/client
./configure \
--prefix=/opt/nginx \
--sbin-path=/usr/sbin/nginx\
--conf-path=/etc/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_flv_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/tmp/nginx/client/ \
--http-proxy-temp-path=/var/tmp/nginx/proxy/ \
--http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \
--http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \
--http-scgi-temp-path=/var/tmp/nginx/scgi \
--with-pcre
也可以使用简单安装,指定安装目录和https访问支持,本实例采用简单安装
./configure --prefix=/opt/nginx --with-http_ssl_module --with-http_gzip_static_module --error-log-path=/var/log/nginx/nginx.log --pid-path=/var/log/nginx/pid
4、编译安装
这是两个命令,make 为编译命令,make install 为安装命令,可以分别执行。这里使用&&将两个命令连接执行,会在前面命令执行成功的前提下才会执行第二个命令。
make && make install
5、.环境变量配置
方式一 、 修改/etc/profile 文件
在/etc/profile 文件最后添加以下内容,将安装目录下的 sbin 目录添加到 PATH 系统环境变量中。然后再重新加载该文件即可。
#修改环境变量
export PATH=$PATH:/usr/sbin/nginx #注意执行文件的位置
useradd nginx #添加用户(默认添加nginx用户组)
方式二 、 添加安装的nginx到服务列表:将如下内容添加到/etc/init.d/nginx脚本中,nginx需要具有可执行权限。
本实例采用方式二
5.1 增加配置文件及配置数据
vim /etc/init.d/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="/opt/nginx/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/opt/nginx/conf/nginx.conf"
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
lockfile=/var/lock/subsys/nginx
make_dirs() {
# make required directories
user=`nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
options=`$nginx -V 2>&1 | grep 'configure arguments:'`
for opt in $options; do
if [ `echo $opt | grep '.*-temp-path'` ]; then
value=`echo $opt | cut -d "=" -f 2`
if [ ! -d "$value" ]; then
# echo "creating" $value
mkdir -p $value && chown -R $user $value
fi
fi
done
}
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
make_dirs
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
}
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
5.2 修改nginx文件的执行权限
chmod +x nginx
6、启动nginx
6.1 添加该文件到系统服务中去
chkconfig --add nginx
查看是否添加成功
chkconfig |grep nginx
6.2 启动
service nginx start
配置开机启动
chkconfig nginx on
6.3 浏览器验证启动成功
浏览器输入本机的ip既可以访问nginx首页
http://192.168.8.63/