Linux环境安装 - Nginx

概述

Nginx是一款轻量级的网页服务器、反向代理服务器。相较于Apache、lighttpd具有占有内存少,稳定性高等优势。它最常的用途是提供反向代理服务

安装

1.安装依赖
yum install gcc
yum install pcre-devel
yum install zlib zlib-devel
yum install openssl openssl-devel
//一键安装上面4个依赖
yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel

2.下载Nginx的tar包

创建一个文件夹
cd /usr/local/
mkdir nginx
cd nginx
// 下载tar包
wget http://nginx.org/download/nginx-1.13.7.tar.gz
tar -xvf nginx-1.13.7.tar.g

3.安装Nginx

//进入nginx目录
cd /usr/local/nginx
//执行命令
./configure --prefix=/usr/local/nginx \
--prefix=/usr/local/nginx \
--conf-path=/usr/local/nginx/conf/nginx.conf \
--user=nginx \
--group=nginx \
--error-log-path=/logs/nginx/errpr.log \
--http-log-path=/logs/nginx/access.log \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--with-http_ssl_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--with-http_mp4_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/fastcgi

参数解释

–prefix=/usr/local/nginx ==>安装部署后的根目录,默认为/usr/local/nginx
–conf-path=/etc/nginx/nginx/nginx.conf ==>配置文件的放置路径,默认/conf/nginx.conf
–user=nginx ==>指定worker进程运行时所属的用户
–group=nginx ==>指定worker进程运行是所属的组
–error-log-path=/var/log/nginx/errpr.log ==>error日志放置位置
–http-log-path=/var/log/nginx/access.log ==>access日志放置的位置
–pid-path=/var/run/nginx/nginx.pid ==>pid文件的存放路径;默认/logs/nginx.pid
–lock-path=/var/lock/nginx.lock ==>lock文件的放置路径;默认/logs/nginx.lock
–with-http_ssl_module ==>提供HTTPS服务;该模块的安装依赖于OpenSSL开源软件
–with-http_stub_status_module ==>能够获取Nginx自上次启动以来的工作状态
–with-http_gzip_static_module ==> 如果采用gzip 模块把一些文档进行gzip 格式压缩后再返回给客户端,那么对同一个文件每次都会重新压缩,这是比较消耗服务器CPU 资源的. gzip static 模块可以在做gzip 压缩前,先查看相同位置是否有已经做过gzip 压缩的.gz 文件,如果有,就直接返回。这样就可以预先在服务器上做好文档的压缩,给CPU 减负
–wiht-http_flv_modle ==>可以在向客户端返回响应肘,对FLV 格式的视频文件在header 头做一些处理,使得客户端可以观看、拖动FLV 视频
–with-http_mp4_module ==>使客户端可以观看、拖动MP4 视频
–http-client-body-temp-path=/var/tmp/nginx/client ==>set path to store http client request body temporary files
–http-proxy-temp-path=/var/tmp/nginx/proxy ==>Nginx 作为HTTP 反向代理服务器时,上游服务器产生的HTTP 包体在需要临时存放到磁盘文件时,这样的临时文件将放到该路径下;默认/proxy _temp
–http-fastcgi-temp-path=/var/tmp/nginx/fastcgi ==>Fastcgi 所使用临时文件的放置目;默认/fastcgi_temp

// 执行make命令
make
// 执行make install命令
make install

4.启动Nginx

//测试配置文件
/nginx/sbin/nginx -t
//启动命令
/nginx/sbin/nginx
//停止命令
/nginx/sbin/nginx -s stop
//重启命令
/nginx/sbin/nginx -s reload

5.Nginx设置开机启动

//创建nginx执行文件
vim /etc/init.d/nginx

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=/logs/nginx/lock/nginx
make_dirs() {
   # make required directories
   user=`$nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
   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
}
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

脚本地址 http://wiki.nginx.org/RedHatNginxInitScript
记得修改文件中命令为自己的路径

//启动/停止脚本
/etc/init.d/nginx start
/etc/init.d/nginx stop
// 将nginx服务加入chkconfig管理列表中
chkconfig --add /etc/init.d/nginx
// service对nginx进行操作 
service nginx start
service nginx stop
//设置终端模式开机启动
chkconfig nginx on

安装问题

1.如何查看Nginx启动使用的配置文件

/nginx/sbin/nginx -t

这里写图片描述

2.错误提示 make[1]:leaving directory ‘/usr/local/nginx

如果存在该目录,可以不用管

3.访问php页面,变成直接下载

查找Nginx使用的配置文件 /nginx/sbin/nginx -t (因为安装后在/usr/local/nginx/conf下也有配置文件nginx.conf),然后检查nginx.conf的配置是否已经开启对.php的解析,配置如下:

4.设置了开机启动nginx服务后,service nginx status 显示启动失败
解决方法:
需要先把/sbin/nginx启动的进程kill掉,然后再使用service/systemctl 命令进行启动,如果还是出现问题,建议重启阿里云服务器在进行操作

5.nginx: [emerg] getpwnam(“nginx”) failed

没有安装nginx用户导致无法启动

useradd -s /sbin/nologin -M nginx

参考

https://www.cnblogs.com/jimisun/p/8057156.html
https://blog.csdn.net/u013870094/article/details/52463026

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值