新手nginx安装教程

1.安装各种依赖

#gcc安装,nginx源码编译需要
yum install gcc-c++

#PCRE pcre-devel 安装,nginx 的 http 模块使用 pcre 来解析正则表达式
yum install -y pcre pcre-devel

#zlib安装,nginx 使用zlib对http包的内容进行gzip
yum install -y zlib zlib-devel

#OpenSSL 安装,强大的安全套接字层密码库,nginx 不仅支持 http 协议,还支持 https(即在ssl协议上传输http)
yum install -y openssl openssl-devel

2.下载nginx

(1)直接官网下载【官网链接

(2)使用wget命令下载(推荐)

#下载版本号可根据目前官网最新稳定版自行调整
wget -c https://nginx.org/download/nginx-1.16.1.tar.gz

3.安装

#根目录使用ls命令可以看到下载的nginx压缩包,然后解压
tar -zxvf nginx-1.16.1.tar.gz

#解压后进入目录
cd nginx-1.16.1

#使用默认配置(必需执行)
./configure

#自定义配置(配置解读

./configure  --prefix=/usr/local/nginx  --sbin-path=/usr/local/nginx/sbin/nginx --conf-path=/usr/local/nginx/conf/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_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

#编译安装
make
make install

#查询nginx主进程号
ps -ef | grep nginx

#测试端口
netstat –na|grep 80
#浏览器中测试
http://ip:80

安装成功了

附:扩展内容

1.安装报错
cp: `conf/koi-win’ and `/usr/local/nginx/conf/koi-win’ 为同一文件。

执行:
./configure --prefix=/usr/local/nginx --conf-path=/usr/local/nginx/nginx.conf

2.自定义的nginx启动停止脚本
[root@unique sbin]# vi /etc/init.d/nginx
把下面的脚本复制进去然后保存
#! /bin/sh
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: starts the nginx web server
 
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DESC="nginx daemon"
NAME=nginx
DAEMON=/usr/local/nginx/sbin/$NAME
CONFIGFILE=/usr/local/nginx/conf/$NAME.conf
PIDFILE=/usr/local/nginx/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() {
 kill -INT `cat $PIDFILE` || echo -n "nginx not running"
}
 
do_reload() {
 kill -HUP `cat $PIDFILE` || 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
给文件添加执行权限

[root@unique sbin]
#chmod +x /etc/init.d/nginx
#然后可以通过
#/etc/init.d/nginx start 命令启动nginx
#/etc/init.d/nginx stop 命令停止nginx
#/etc/init.d/nginx restart 命令重启nginx

#重启nginx
[root@unique init.d]# /etc/init.d/nginx restart
Restarting nginx daemon: nginx.

3.配置开机启动

如果需要开机启动服务,保存好 /etc/init.d/nginx文件后,执行以下命令:

[root@unique init.d]#chkconfig --add ningx
[root@unique init.d]#chkconfig --level nginx 2345 on

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值