一款高性能、輕量級Web服務軟件php
穩定性高html
系統資源消耗低nginx
單臺物理服務器可支持30000 ~ 50000個併發請求c++
[root@localhost opt] yum install gcc gcc-c++ make pcre-devel zlib-devel -y
[root@localhost opt] useradd -M -s /sbin/nologin nginx
[root@localhost ~] mount.cifs //192.168.100.10/lnmp /mnt/ //將本地nginx源碼包掛載到mnt目錄下 [root@localhost ~] cd /mnt/ [root@localhost mnt] tar zxvf nginx-1.12.2.tar.gz -C /opt/ //解壓源碼包到opt目錄 [root@localhost mnt] cd /opt [root@localhost opt] cd nginx-1.12.2/ //進入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/nginx/sbin //進入nginx安裝後的命令目錄 [root@localhost sbin] ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/ //創建軟連接到sbin目錄 [root@localhost sbin] nginx //執行開啓服務 [root@localhost conf]# netstat -ntap | grep 80 //查看服務端口是否開啓 tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 31755/nginx: master [root@localhost sbin] systemctl stop firewalld.service //關閉防火牆 [root@localhost sbin] setenforce 0 //關閉加強性安全功能 [root@localhost sbin] yum install elinks -y //安裝檢測網站工具 [root@localhost sbin] elinks http://localhost //檢測網站是否開啓
[root@localhost sbin]# 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 :wq [root@localhost sbin]# cd /etc/init.d [root@localhost init.d]# chmod +x nginx //給腳本添加執行權限 [root@localhost init.d]# chkconfig --add nginx //將腳本添加到系統環境 [root@localhost init.d]# service nginx start //使用service控制服務開啓
#user nobody; worker processes 1; #error_ log logs/error.log; #pid logs/nginx.pid;
I/0
事件配置events { use epoll; worker_connections 4096; }
HTTP
配置http { ...... access_log logs/access.log main; sendfile on; ...... keepalive_timeout 65; server { listen 80; server name www.bt.com; charset utf-8; location / { root html; index index.html index.php; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html;}} }
--with-http_ stub_ status_ module
nginx -V
查看已安裝的Nginx
是否包含HTTP_ STUB_ _STATUS
模塊[root@localhost init.d]# cd /usr/local/nginx/conf/ //進入配置文件目錄 [root@localhost conf]# vim nginx.conf //編輯配置文件 ...//省略部份內容... server { listen 80; server_name www.kgc.com; //編輯域名 charset utf-8; //編輯字符串格式爲utf-8 #access_log logs/host.access.log; //註釋access日誌文件 location / { root html; index index.html index.htm; } location /status { stub_status on; //開啓功能統計模塊 access_log off; } ...//省略部份內容... :wq //保存退出 [root@localhost conf]# yum install bind -y //安裝DNS服務 [root@localhost conf]# vim /etc/named.conf //常規配置DNS功能 ...//省略部份內容... options { listen-on port 53 { any; }; listen-on-v6 port 53 { ::1; }; directory "/var/named"; dump-file "/var/named/data/cache_dump.db"; statistics-file "/var/named/data/named_stats.txt"; memstatistics-file "/var/named/data/named_mem_stats.txt"; recursing-file "/var/named/data/named.recursing"; secroots-file "/var/named/data/named.secroots"; allow-query { any; }; ...//省略部份內容... :wq [root@localhost conf]# vim /etc/named.rfc1912.zones ...//省略部份內容... zone "kgc.com" IN { type master; file "kgc.com.zone"; allow-update { none; }; }; ...//省略部份內容... :wq [root@localhost conf]# cd /var/named/ [root@localhost named]# cp -p named.localhost kgc.com.zone [root@localhost named]# vim kgc.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.144.133 :wq [root@localhost named]# systemctl start named [root@localhost named]# service nginx stop [root@localhost named]# service nginx start
www.kgc.com/status
訪問網站統計記錄[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf server { listen 80; server_name www.kgc.com; charset utf-8; #access_log logs/host.access.log; location / { auth_basic "secret"; //插入設置密碼驗證 auth_basic_user_file /usr/local/nginx/passwd.db; //添加條目設置密碼驗證文件存放目錄 root html; index index.html index.htm; } location /status { stub_status on; access_log off; } :wq [root@localhost ~]# yum install httpd-tools -y //安裝生成密碼設置文件 已加載插件:fastestmirror, langpacks base | 3.6 kB 00:00:00 extras | 2.9 kB 00:00:00 ...//省略部份內容... 已安裝: httpd-tools.x86_64 0:2.4.6-90.el7.centos 做爲依賴被安裝: apr.x86_64 0:1.4.8-5.el7 apr-util.x86_64 0:1.5.2-6.el7 完畢! [root@localhost ~]# htpasswd -c /usr/local/nginx/passwd.db test01 //建立密碼驗證文件 New password: //輸入密碼 Re-type new password: //再次輸入 Adding password for user test01 //建立用戶成功 [root@localhost ~]# cat /usr/local/nginx/passwd.db //查看建立的密碼文件 test01:$apr1$T22NmXdU$yE8iwUxpaHcR95ZNosCUK0 //用戶密碼信息 [root@localhost ~]# service nginx stop //中止nginx服務 [root@localhost ~]# service nginx start //啓動nginx服務 [root@localhost ~]# netstat -ntap | grep 80 //查看端口是否開啓 tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 8624/nginx: