Nginx之手工编译安装及设置

Nginx之手工编译安装及设置

前言

前面我们认识了Nginx,也知道了其对于静态网页的处理能力是相当之强,我们接下来手工编译安装,及编译安装完后的一些的简单优化操作。

工具包:

链接:https://pan.baidu.com/s/1P4kbLumaDOFHp56tp7KEVw
提取码:yyb3

实验环境

centos7虚拟机一台

win10主机一台

手工编译安装Nginx

1.将 nginx-1.12.2.tar.gz 解压到/opt/目录下面

[root@localhost ~]# ls
anaconda-ks.cfg  initial-setup-ks.cfg  LNMP-C7  公共  模板  视频  图片  文档  下载  音乐  桌面
[root@localhost ~]# cd LNMP-C7/
[root@localhost LNMP-C7]# ls
Discuz_X3.4_SC_UTF8.zip    nginx-1.12.2.tar.gz  php-7.1.20.tar.bz2
mysql-boost-5.7.20.tar.gz  php-5.6.11.tar.bz2   php-7.1.20.tar.gz
ncurses-5.6.tar.gz         php-7.1.10.tar.bz2   zend-loader-php5.6-linux-x86_64_update1.tar.gz
[root@localhost LNMP-C7]# tar -zxvf nginx-1.12.2.tar.gz -C /opt/
[root@localhost LNMP-C7]# cd /opt/
[root@localhost opt]# ls
nginx-1.12.2  rh
[root@localhost opt]# cd nginx-1.12.2/
[root@localhost nginx-1.12.2]# ls
auto  CHANGES  CHANGES.ru  conf  configure  contrib  html  LICENSE  man  README  src

2.下载手工编译的包

[root@localhost nginx-1.12.2]# yum -y install gcc gcc-c++ pcre pcre-devel zlib-devel expat-devel
[root@localhost nginx-1.12.2]# rpm -q gcc
gcc-4.8.5-39.el7.x86_64
[root@localhost nginx-1.12.2]# rpm -q gcc-c++
gcc-c++-4.8.5-39.el7.x86_64
[root@localhost nginx-1.12.2]# rpm -q pcre
pcre-8.32-17.el7.x86_64
[root@localhost nginx-1.12.2]# rpm -q pcre-devel
pcre-devel-8.32-17.el7.x86_64
[root@localhost nginx-1.12.2]# rpm -q expat-devel
expat-devel-2.1.0-10.el7_3.x86_64
[root@localhost nginx-1.12.2]# rpm -q zlib-devel
zlib-devel-1.2.7-18.el7.x86_64

3.创建程序型用户nginx,并开始编译安装

[root@localhost nginx-1.12.2]# useradd -M -s /sbin/nologin nginx
[root@localhost nginx-1.12.2]# id nginx
uid=1001(nginx) gid=1001(nginx) 组=1001(nginx)
[root@localhost nginx-1.12.2]# ./configure \
> --prefix=/usr/local/nginx \
> --user=nginx \
> --group=nginx \
> --with-http_stub_status_module
[root@localhost nginx-1.12.2]# make && make install
[root@localhost nginx-1.12.2]# cd /usr/local/
[root@localhost local]# ls
bin  etc  games  include  lib  lib64  libexec  nginx  sbin  share  src
[root@localhost local]# cd nginx/
[root@localhost nginx]# ls
conf  html  logs  sbin
[root@localhost nginx]# cd html/
[root@localhost html]# ls
50x.html  index.html

PS:

–with-http_stub_status_module 状态应用控制模块

index.html是默认的网页

50x.html是服务器的错误网页

4.对Nginx进行一些简单测试

[root@localhost html]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
[root@localhost html]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost html]# nginx 
[root@localhost html]# netstat -ntap|grep 80
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      40244/nginx: maste
[root@localhost html]# systemctl stop firewalld
[root@localhost html]# setenforce 0
[root@localhost html]# yum -y install elinks
[root@localhost html]# rpm -q elinks 
elinks-0.12-0.37.pre6.el7.0.1.x86_64
[root@localhost html]# elinks http://localhost

输入elinks http://localhost

在这里插入图片描述

在win10主机里面进行访问

在这里插入图片描述

5.开启nginx服务和关闭Nginx服务

[root@localhost html]# killall -s QUIT nginx
[root@localhost html]# netstat -ntap |grep 80
[root@localhost html]# nginx
[root@localhost html]# killall -s HUP nginx
[root@localhost html]# netstat -ntap |grep 80
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      40470/nginx: master
[root@localhost html]# cd

6.创建Nginx的管理脚本

[root@localhost ~]# vim /etc/init.d/nginx
#!/bin/bash
# chkconfig: - 99 20
# description: Nginx Service Control Script
PROG="/usr/local/nginx/sbin/nginx"
PIDF="/usr/local/nginx/logs/nginx.pid"
case "$1" in
  start)
    $PROG
    ;;
  stop)
    kill -s QUIT $(cat $PIDF)
    ;;
  restart)
    $0 stop
    $0 start
    ;;
  reload)
    kill -s HUP $(cat $PIDF)
    ;;
  *)
    echo "Usage: $0 {start|stop|restart|reload}"
    exit 1
esac
exit 0
[root@localhost ~]# cd /etc/init.d/
[root@localhost init.d]# ls
functions  netconsole  network  nginx  README
[root@localhost init.d]# chkconfig --add nginx
[root@localhost init.d]# service nginx stop
[root@localhost init.d]# netstat -ntap|grep 80
[root@localhost init.d]# service nginx start
[root@localhost init.d]# netstat -ntap | grep 80
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      2203/nginx: master

7.修改Nginx的主配置文件

[root@localhost init.d]# cd /usr/local/nginx/
[root@localhost nginx]# ls
client_body_temp  conf  fastcgi_temp  html  logs  proxy_temp  sbin  scgi_temp  uwsgi_temp
[root@localhost nginx]# cd conf/
[root@localhost conf]# ls
fastcgi.conf            koi-utf             nginx.conf           uwsgi_params
fastcgi.conf.default    koi-win             nginx.conf.default   uwsgi_params.default
fastcgi_params          mime.types          scgi_params          win-utf
fastcgi_params.default  mime.types.default  scgi_params.default
[root@localhost conf]# vim nginx.conf
    server {
        listen       80;
        server_name  www.hello.com;

        charset utf-8;
        
        #access_log  logs/host.access.log;
        

        location / {
            root   html;
            index  index.html index.htm index.php;
        }
        location /status {
            stub_status on;
            access log off;
        }
}

8.配置dns服务

[root@localhost conf]# yum -y install bind
[root@localhost conf]# vim /etc/named.conf
	listen-on port 53 { any; };
	allow-query     { any; };
[root@localhost conf]# vim /etc/named.rfc1912.zones
zone "hello.com" IN {
        type master;
        file "hello.com.zone";
        allow-update { none; };
};
[root@localhost conf]# cd /var/named
[root@localhost named]# ls
data  dynamic  named.ca  named.empty  named.localhost  named.loopback  slaves
[root@localhost named]# cp -p named.localhost hello.com.zone
[root@localhost named]# vim hello.com.zone 
$TTL 1D
@       IN SOA  @ rname.invalid. (
                                        0       ; serial
                                        1D      ; refresh
                                        1H      ; retry
                                        1W      ; expire
                                        3H )    ; minimum
        NS      @
        A       127.0.0.1
www IN  A       192.168.73.170
[root@localhost named]# systemctl restart named
[root@localhost named]# service nginx restart

9.在win10主机里面测试页面是否能够正常访问

先将win10主机的dns改成服务器的地址

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

然后一直点击确定即可

下面我们在win10看看是否能够解析www.hello.com

在这里插入图片描述

在win10主机浏览器里面输入www.hello.com
在这里插入图片描述

在浏览器网址中输入www.hello.com/status

在这里插入图片描述

发布了95 篇原创文章 · 获赞 39 · 访问量 6135

猜你喜欢

转载自blog.csdn.net/double_happy111/article/details/103646156