文章目录
在各种网站服务器软件中,除了Apache HTTP Server外,还有一款轻量级的HTTP服务器软件–Nginx,其稳定,高效的特性逐渐被越来越多的用户认可
其将源代码以类BSD许可证的形式发布,因它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名
其特点是:占有内存少,并发能力强
中国大陆使用nginx网站用户有:百度、京东、新浪、网易、腾讯、淘宝等。
Nginx服务基础
Nginx概述
一款高性能、轻量级Web服务软件
●稳定性高
●系统资源消耗低
●对HTTP并发连接的处理能力高
●单台物理服务器可支持30 000 ~ 50000个并发请求
●占用内存少,并发能力强
Nginx编译安装
[root@localhost ~]# cd /opt
[root@localhost opt]# yum -y install gcc gcc-c++ pcre pcre-devel zlib-devel
nginx-1.12.2.tar.gz
[root@localhost opt]# tar xzvf nginx-1.12.2.tar.gz
[root@localhost opt]# cd nginx-1.12.2/
[root@localhost nginx-1.12.2]# useradd -M -s /sbin/nologin 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]# ln -s /usr/local/nginx/sbin/nginx /usr/local/bin/
[root@localhost nginx-1.12.2]# 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
使用service工具进行管理
[root@localhost nginx-1.12.2]# vim /etc/init.d/nginx
#!/bin/bash
# chkconfig: - 99 20
# description: Nginx Server 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 nginx-1.12.2]# chmod +x /etc/init.d/nginx
[root@localhost nginx-1.12.2]# chkconfig --add nginx
[root@localhost nginx-1.12.2]# service nginx start
[root@localhost nginx-1.12.2]# service nginx stop
[root@localhost nginx-1.12.2]# netstat -ntap | grep nginx
[root@localhost nginx-1.12.2]# service nginx start
[root@localhost nginx-1.12.2]# netstat -ntap | grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 79280/nginx: master
使用systemctl工具进行管理
[root@localhost nginx-1.12.2]# vim /lib/systemd/system/nginx.service
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
PIDFile =/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/bin/kill -S HUP $MAINPID
ExecStop=/usr/bin/kill -S QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
[root@localhost nginx-1.12.2]# chmod 754 /lib/systemd/system/nginx.service
[root@localhost nginx-1.12.2]# cd /usr/local/nginx/conf/
[root@localhost init.d]# cd /usr/local/nginx/conf/
[root@localhost conf]# mv nginx.conf nginx.conf.back
[root@localhost conf]# grep -v "#" nginx.conf.back > nginx.conf
[root@localhost conf]# vim nginx.conf
47 location /status {
48 stub_status on;
49 access_log off;
50 }
Nginx的访问状态统计
[root@localhost conf]# service nginx start
[root@localhost conf]# 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 successf
[root@localhost conf]# systemctl stop firewalld.service
[root@localhost conf]# iptables -F
[root@localhost conf]# setenforce 0
Nginx虚拟主机
Nginx虚拟主机应用
Nginx支持的虚拟主机有三种
●基于域名的虚拟主机
●基于IP的虚拟主机
●基于端口的虚拟主机
通过"server{}" 配置段实现
[root@localhost conf]# cd /var/
[root@localhost var]# mkdir www
[root@localhost var]# cd www
[root@localhost www]# mkdir hui fang
[root@localhost www]# cd hui
[root@localhost hui]# vim index.html
<h1>this is hui</h1>
[root@localhost hui]# cd ../fang
[root@localhost fang]# vim index.html
<h1>this is fang</h1>
[root@localhost fang]# yum -y install tree
[root@localhost fang]# cd ..
[root@localhost www]# tree ./
./
├── fang
│ └── index.html
└── hui
└── index.html
2 directories, 2 files
[root@localhost www]# yum -y install bind
[root@localhost www]# vim /etc/named.conf
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; };
[root@localhost www]# vim /etc/named.rfc1912.zones
zone "hui.com" IN {
type master;
file "hui.com.zone";
allow-update { none; };
};
zone "fang.com" IN {
type master;
file "fang.com.zone";
allow-update { none; };
};
[root@localhost www]# cd /var/named/
[root@localhost named]# cp -p named.localhost hui.com.zone
[root@localhost named]# vim hui.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.20.20
[root@localhost named]# cp -p hui.com.zone fang.com.zone
[root@localhost named]# systemctl start named
[root@localhost named]# vim /usr/local/nginx/conf/nginx.conf
120 server {
121 server_name www.hui.com;
122 location / {
123 root /var/www/hui;
124 index index.html index.php;
125 }
126 }
127 server {
128 server_name www.fang.com;
129 location / {
130 root /var/www/fang;
131 index index.html index.php;
132 }
133 }
134 }
[root@localhost named]# 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 named]# service nginx stop
[root@localhost named]# service nginx start
Nginx访问控制
[root@localhost named]# yum -y install httpd
[root@localhost named]# which htpasswd
/usr/bin/htpasswd
[root@localhost named]# htpasswd -c /usr/local/nginx/passwd.db huifang
New password:
Re-type new password:
Adding password for user huifang
[root@localhost named]# chown nginx /usr/local/nginx/passwd.db
[root@localhost named]# chmod 400 /usr/local/nginx/passwd.db
[root@localhost named]# vim /usr/local/nginx/conf/nginx.conf
120 server {
121 server_name www.hui.com;
122 location / {
123 auth_basic "secret";
124 auth_basic_user_file /usr/local/nginx/passwd.db;
125 root /var/www/hui;
126 index index.html index.php;
127 }
128 }
[root@localhost named]# service nginx stop
[root@localhost named]# service nginx start
基于客户端的访问控制
通过客户端IP地址,决定是否允许对页面访问
[root@localhost named]# vim /usr/local/nginx/conf/nginx.conf
129 server {
130 server_name www.fang.com;
131 location / {
132 deny 192.168.20.12;
133 allow all;
134 root /var/www/fang;
135 index index.html index.php;
136 }
137 }
[root@localhost named]# service nginx stop
[root@localhost named]# service nginx start
Nginx动静分离
[root@localhost ~]# yum -y install httpd httpd-devel
[root@localhost ~]# systemctl start httpd
[root@localhost ~]# firewall-cmd --permanent --zone=public --add-service=http
success
[root@localhost ~]# firewall-cmd --permanent --zone=public --add-service=https
success
[root@localhost ~]# firewall-cmd --reload
success
[root@localhost ~]# yum install mariadb mariadb-server mariadb-libs mariadb-devel -y
[root@localhost ~]# systemctl start mariadb
[root@localhost ~]# mysql_secure_installation
Enter current password for root (enter for none):
Set root password? [Y/n] y
New password:
Re-enter new password:
Remove anonymous users? [Y/n] n
Disallow root login remotely? [Y/n] n
Remove test database and access to it? [Y/n] n
Reload privilege tables now? [Y/n] y
[root@localhost ~]# yum -y install php
[root@localhost ~]# yum install php-mysql -y
[root@localhost ~]# yum install -y php-gd php-ldap php-odbc php-pear php-xml php-xmlrpc php-mbstring php-snmp php-soap curl curl-devel php-bcmath.x86_64
[root@localhost ~]# cd /var/www/html
[root@localhost html]# vim index.php
<?php
phpinfo();
?>
[root@localhost html]# systemctl restart httpd
[root@localhost html]# vim index.php
<?php
echo "this is apache"
?>
[root@localhost ~]# hostnamectl set-hostname nginx
[root@localhost ~]# su
[root@nginx ~]# cd /opt
[root@nginx opt]# mkdir nginx
[root@nginx opt]# cd nginx
[root@nginx nginx]# ls
nginx-1.12.2.tar.gz
[root@nginx nginx]# tar xzvf nginx-1.12.2.tar.gz
[root@nginx nginx]# cd nginx-1.12.2/
[root@nginx nginx-1.12.2]# useradd -M -s /sbin/nologin nginx
[root@nginx nginx-1.12.2]# yum -y install gcc gcc-c++ zlib-devel pcre pcre-devel
[root@nginx nginx-1.12.2]# ./configure \
> --prefix=/usr/local/nginx \
> --user=nginx \
> --group=nginx \
> --with-http_stub_status_module
[root@nginx nginx-1.12.2]# make && make install
[root@nginx nginx-1.12.2]# ln -s /usr/local/nginx/sbin/nginx /usr/local/bin/
[root@nginx nginx-1.12.2]# vim /etc/init.d/nginx
#!/bin/bash
# chkconfig: - 99 20
# description: Nginx Server 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@nginx nginx-1.12.2]# chmod +x /etc/init.d/nginx
[root@nginx nginx-1.12.2]# chkconfig --add nginx
[root@nginx nginx-1.12.2]# service nginx start
[root@nginx nginx-1.12.2]# netstat -ntap | grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 13068/nginx: master
[root@nginx nginx-1.12.2]# systemctl stop firewalld.service
[root@nginx nginx-1.12.2]# iptables -F
[root@nginx nginx-1.12.2]# setenforce 0
[root@nginx nginx-1.12.2]# vim /usr/local/nginx/conf/nginx.conf
59 location ~ \.php$ {
60 proxy_pass http://192.168.20.10;
61 }
62
[root@nginx nginx-1.12.2]# service nginx restart