1、依赖包准备
Nginx官方文档:https://www.nginx.com/resources/wiki/start/
依赖包可在此网站下搜索:http://vault.centos.org/7.4.1708/os/x86_64/Packages/
cmake-2.8.12.2-2.el7.x86_64.rpm
make-3.82-24.el7.x86_64.rpm
gcc-4.8.5-39.el7.x86_64.rpm
gcc-c++-4.8.5-39.el7.x86_64.rpm
pcre-devel-8.32-17.el7.x86_64.rpm
pcre2-10.23-2.el7.x86_64.rpm
zlib-devel-1.2.7-18.el7.x86_64.rpm
openssl-devel-1.0.2k-8.el7.x86_64.rpm
2、安装包准备
nginx-1.6.2.tar.gz
3、依赖安装
mkdir /usr/local/software
cd /usr/local/software
rpm -Uvh *.rpm --nodeps
这里已经安装过了,yum安装执行
yum -y install cmake make gcc gcc-c++ openssl-devel pcre-devel pcre2 zlib-devel
4、Nginx安装
tar zxvf nginx-1.6.2.tar.gz -C /usr/local/
cd /usr/local/nginx-1.6.2/ && ./configure --prefix=/usr/local/nginx
make && make install
cd /usr/local/nginx && ls
5、启动Nginx
/usr/local/nginx/sbin/nginx
ps -ef | grep nginx
6、防火墙端口通行
firewall-cmd --add-port=80/tcp --permanent
firewall-cmd --reload
firewall-cmd --list-ports
7、web访问Nginx
如果访问出现 403 Forbiden ,需要修改配置文件
vim /usr/local/nginx/conf/nginx.conf
在默认配置 #user nobody; 下添加
user root;
赋予 /usr/local/nginx/html/index.html 文件 644权限
chmod 644 /usr/local/nginx/html/index.html
8、Nginx服务配置
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="/usr/sbin/nginx"
nginx="/usr/local/nginx/sbin/nginx"
prog=$(basename $nginx)
# NGINX_CONF_FILE="/etc/nginx/nginx.conf"
NGINX_CONF_FILE="/usr/local/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:.*--user=" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
if [ -n "$user" ]; then
if [ -z "`grep $user /etc/passwd`" ]; then
useradd -M -s /bin/nologin $user
fi
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
fi
}
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 $prog -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
给 /etc/init.d/nginx 文件赋予执行权限
chmod +x /etc/init.d/nginx
创建pid文件
echo 10000 > /var/run/nginx.pid
将启动脚本配置到chkconfig服务
chkconfig --add /etc/init.d/nginx
chkconfig --list
开机自启
chkconfig nginx on
或者在/etc/rc.d/rc.local文件下添加
/usr/local/nginx/sbin/nginx
启动和关闭命令
/usr/local/nginx/sbin/nginx
/usr/local/nginx/sbin/nginx -s stop
/usr/local/nginx/sbin/nginx -s reload
service nginx status
service nginx start
service nginx stop
service nginx restart
systemctl status nginx
systemctl start nginx
systemctl stop nginx
systemctl restart nginx
9、常见报错
./configure: error: the HTTP gzip module requires the zlib library.
You can either disable the module by using --without-http_gzip_module
option, or install the zlib library into the system, or build the zlib library
statically from the source with nginx by using --with-zlib=<path> option.
解决:以下依赖,如果需要其他依赖则使用 --nodeps 不依靠依赖安装
rpm -Uvh pcre-devel-8.32-17.el7.x86_64.rpm
rpm -Uvh pcre2-10.23-2.el7.x86_64.rpm
rpm -Uvh zlib-devel-1.2.7-18.el7.x86_64.rpm
rpm -Uvh openssl-devel-1.0.2k-8.el7.x86_64.rpm