Linux服务器架设篇,Nginx服务器的架设

1.安装 nginx依赖包

(1)安装pcre

yum install pcre-devel

(2)安装openssl

yum -y install openssl-devel

(3)安装zlib

yum install zlib-devel

2.安装nginx软件

(1)从 http://nginx.org 官网下载nginx源码包

(2)解包nginx软件

tar -zxvf nginx-1.16.1.tar.gz 

注意:具体解压的文件名依据你所下载的文件

(3)添加nginx软件管理用户nginx

useradd nginx
passwd nginx

(4)安装nginx软件

./configure \
> --user=nginx \
> --group=nginx \
> --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 \
> --http-client-body-temp-path=/tmp/nginx/client_body \
> --http-proxy-temp-path=/tmp/nginx/proxy \
> --http-fastcgi-temp-path=/tmp/nginx/fastcgi \
> --pid-path=/var/run/nginx.pid \
> --lock-path=/var/lock/subsys/nginx \
> --with-http_stub_status_module \
> --with-http_ssl_module \
> --with-http_gzip_static_module



注意:
--user ===> 启动程序所属用户
--group ===> 启动程序所属组
--prefix ===> 安装路径
--sbin-path ===> 设置二进制文件路径名
--conf-path ===> 配置文件路径
--error-log-path ===> 错误日志文件路径
--http-log-path ===> 访问日志文件路径
--http-client-body-temp-path ===> 存储HTTP客户端请求主体的临时文件路径
--http-proxy-temp-path ===> 存储HTTP代理临时文件的路径
--http-fastcgi-temp-path ===> 存储HTTP fastcgi的临时文件路径
--pid-path ===> nginx.pid文件路径
--lock-path ===> nginx.lock文件路径
--with-http_stub_status_module \ ===> 安装可以监控Nginx状态的模块
--with-http_ssl_module \ ===> 启用SSL支持
--with-http_gzip_static_module ===> 启用gzip压缩
./configure  --user=nginx  --group=nginx  --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  --http-client-body-temp-path=/tmp/nginx/client_body  --http-proxy-temp-path=/tmp/nginx/proxy  --http-fastcgi-temp-path=/tmp/nginx/fastcgi  --pid-path=/var/run/nginx.pid  --lock-path=/var/lock/subsys/nginx  --with-http_stub_status_module  --with-http_ssl_module  --with-http_gzip_static_module
make && make install


注意:
这里可能会报很多诸如 leaving xxx directory 之类的信息。这里先不要管它。下一步用
nginx -t
根据报错信息修改即可。

(5)检测Nginx配置文件是否出错

nginx -t

根据报错信息修改配置。

(6)代开浏览器,输入 127.0.0.1,如果出现下面页面即代表成功。

(7)查看80端口占用情况

2.配置文件详解

 /etc/nginx/nginx.conf

该配置文件主要包含四部分

  • main ===> 全局设置(设置将会影响其他所有的设置)
  • server ===> 主机设置(用户指定主机或端口)
  • upstream ===> 负载均衡服务器设置(设置一系列的后端服务器来维持负载均衡)
  • location ===> URL匹配特定位置的设置(用于匹配网页位置)

注意:

location继承server

server继承main

(1)配置文件

#=================================参数设置================================
#user  nobody;
#<设置运行用户和组群>

worker_processes  1;
#<设置进程数量,通常设置和CPU数量一样>

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
#<设置全局错误日志文件。类型有:debug,info,notice,warn,error,crit>


#pid        logs/nginx.pid;
#<设置pid文件>

#=================================设置最大连接数================================
events {
    worker_connections  1024;
}
#<单个进程最大并发连接数量>
#================================================================================

#====================================Web服务器设置================================
http {
    include       mime.types;
#<设置文件扩展名与文件类型映射表>
    default_type  application/octet-stream;
#<设置默认文件类型>
#==============================设置访问日志文件格式==============================

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;
#<设置Nginx访问日志文件>

    sendfile        on;
#<设置高效文件传输模式>

    #tcp_nopush     on;
#<在一个数据包里发送所有的头文件,而不是一个接一个的发送>

    #keepalive_timeout  0;
    keepalive_timeout  65;
#<客户端超时连接时间设置>

    #gzip  on;
#<是否开启gzip压缩>
#====================================设置HTTP服务器================================

    server {
        listen       80;
#<设置监听端口>

        server_name  localhost;
#<绑定主机名或域名或IP地址>

        #charset koi8-r;
#<设置字符编码格式>

        #access_log  logs/host.access.log  main;
#<设置虚拟主机访问日志文件>
#====================================默认请求================================

        location / {
            root   html;
#<设置Nginx服务器默认网站的根目录位置>

            index  index.html index.htm;
#<设置首页文件>
     
#=====================设置虚拟主机的错误信息返回页面============================
        #error_page  404              /404.html;
#<设置错误页面,注意必须大于512KB,否则会被替换>

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
#===================代理PHP脚本到Apache侦听127.0.0.1:80=======================
        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}
#===========PHP脚本请求全部转发到FastCGI处理,使用FastCGI默认配置=================
        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}
#===============================禁止访问.htaccess文件===========================
        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }

#==================================设置虚拟主机================================
    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

#=================================设置HTTPS服务器===============================
    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
#<指定SSL证书>

    #    ssl_certificate_key  cert.key;
#<指定SSL证书秘钥>

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

3.控制Nginx服务器

nginx
-p <前缀> ===> 设置前缀路径,默认为/opt/nginx
-c <文件名> ===> 设置配置文件,默认为/etc/nginx/nginx.conf
-v ===> 显示版本
-V ===> 显示版本信息和配置选项目
-t ===> 测试配置并退出
-q ===> 测试配置过程中抑制非错误信息
-s [stop | quit | reopen | reload]

4.设置服务开机自启动

注意:

新安装的nginx不可以用service和chkconfig命令控制nginx服务。因为/etc/rc.d/init.d目录中没有脚本文件。因此可以通过创建

/etc/rc.d/init.d/nginx

文件来实现service和chkconfig命令来控制。

(1)编辑启动文件

vim /etc/rc.d/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"
prog=$(basename $nginx)

NGINX_CONF_FILE="/etc/nginx/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 $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

(2)给予执行权限

chmod u+x /etc/rc.d/init.d/nginx 

(3)使用service管理Nginx服务

5.添加统计功能

(1)基于所有用户的管理

在配置文件中添加统计代码

vim /etc/nginx/nginx.conf
location /tongji {
    stub_status on;
}

如图:

重启服务,访问如下页面

(2)基于特定用户的管理

①新建用户认证文件

touch /opt/nginx/html/a.psd

②添加用户并且设置密码

htpasswd -c /opt/nginx/html/a.psd zhangsan

③修改配置文件

vim /etc/nginx/nginx.conf
auth_basic "welcome BJ";
auth_basic_user_file /opt/nginx/html/a.psd;

如图:

④访问测试

发布了304 篇原创文章 · 获赞 74 · 访问量 18万+

猜你喜欢

转载自blog.csdn.net/ITlanyue/article/details/103375597