Nginx服务
nginx与Apache对比
安装nginx
- 安装依赖包
yum -y install pcre-devel zlib-devel
-
useradd -M -s /sbin/nologin nginx
- 上官网,下载压缩包,安装至Linux上,进入目录执行configure
[root@www opt]# tar zxf nginx-1.12.2 [root@www opt]# cd nginx-1.12.2/ ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module
-
编译
make && make install
- 对命令路径进行优化
ln -s /usr/local/nginx/sbin/* /usr/local/sbin/
- 检查文件配置的是不是正确,使用nginx -t
[root@www 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
-
启动服务
systemctl start nginx [root@www nginx-1.12.2]# netstat -antp | grep 80 tcp 0 0 12.0.0.9:80 0.0.0.0:* LISTEN 127041/nginx: maste tcp 0 0 12.0.0.3:80 0.0.0.0:* LISTEN 127041/nginx: maste
vim /etc/init.d/nginx #!/bin/bash # chkconfig: - 99 20 # description: Nginx Service Control Script COM="/usr/local/nginx/sbin/nginx" PID="/usr/local/nginx/logs/nginx.pid" case "$1" in start) $COM ;; stop) kill -s QUIT $(cat $PID) ;; restart) $0 stop $0 start ;; reload) kill -s HUP $(cat $PID) ;; *) echo "Usage: $0 {start|stop|restart|reload}" exit 1 esac exit 0
chmod +x /etc/init.d/nginx chkconfig --add nginx [root@www nginx-1.12.2]# service nginx stop [root@www nginx-1.12.2]# service nginx start [root@www nginx-1.12.2]# netstat -antp | grep 80 tcp 0 0 12.0.0.9:80 0.0.0.0:* LISTEN 127714/nginx: maste tcp 0 0 12.0.0.3:80 0.0.0.0:* LISTEN 127714/nginx: maste
vim /usr/lib/systemd/system/nginx.service [Unit] Description=nginx After=network.target //描述服务类型 [Service] Type=forking //后台运行类型 PIDFile =/usr/local/nginx/logs/nginx.pid //pid的文件位置 ExecStart=/usr/local/nginx/sbin/nginx //设置启动服务路径 ExecReload=/bin/kill -s HUP $MAINPID //根据pid设置重载配置 ExecStop=/bin/kill -s QUIT $MAINPID //根据pid设置终止进程 PrivateTmp=true [Install] WantedBy=multi-user.target //启动级别
chmod 754 /lib/systemd/system/nginx.service [root@www nginx-1.12.2]# systemctl start nginx
修改nginx文件
vim /etc/hosts
12.0.0.3 www.jd.com
vim /usr/local/nginx/conf/nginx.conf
server {
listen 12.0.0.3:80;
server_name www.jd.com;
location / {
root /var/www/html/jd;
index index.html index.htm;
location /status {
stub_status on; //打开状态统计功能
access_log off; //关闭日志记录
}
基于授权访问
yum -y install httpd-tools
[root@www ~]# htpasswd -c /usr/local/nginx/passwd.db zhangsan
New password:
Re-type new password:
Adding password for user zhangsan
chown nginx /usr/local/nginx/passwd.db
chmod 400 /usr/local/nginx/passwd.db //为了安全只给可读权限
location / {
auth_basic "secret"; //添加认证
auth_basic_user_file /usr/local/nginx/passwd.db; //认证的文件路径
root /var/www/html/jd;
index index.html index.htm;
基于客户端访问控制
- 可以通过控制ip地址来控制其他客户端不允许访问nginx页面
-
location / { root /var/www/html/jd; index index.html index.htm; deny 192.168.3.2; allow all;
nginx的虚拟主机
基于域名的虚拟主机
-
vim /etc/hosts 12.0.0.3 www.jd.com 12.0.0.3 www.taobao.com
[root@www html]# ls jd taobao [root@www html]# pwd /var/www/html [root@www jd]# cat index.html <h1>www.jd.com</h1> [root@www taobao]# cat index.html <h1>www.taobao.com</h1>
server { listen 12.0.0.3:80; server_name www.jd.com; charset utf-8; access_log logs/jd.access.log; location / { root /var/www/html/jd; index index.html index.htm; location /status { stub_status on; access_log off; } } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } server { listen 12.0.0.3:80; server_name www.taobao.com; charset utf-8; access_log logs/taobao.access.log; location / { root /var/www/html/taobao; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } }
-
基于端口的虚拟主机
-
[root@www html]# ls jd jd8080 taobao [root@www html]# cd jd8080/ [root@www jd8080]# cat index.html <h1> www.jd8080.com</h1> server { listen 12.0.0.3:8080; //端口改为8080 server_name www.jd.com; charset utf-8; access_log logs/jd.access.log; location / { root /var/www/html/jd8080; //站点目录改为jd8080 index index.html index.htm; location /status { stub_status on; access_log off; } } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } server { listen 12.0.0.3:80; server_name www.taobao.com; charset utf-8; access_log logs/taobao.access.log; location / { root /var/www/html/taobao; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } }
12.0.0.3:8080 www.jd.com 12.0.0.3 www.taobao.com
基于不同ip的虚拟主机
-
12.0.0.9 www.jd.com 12.0.0.3 www.taobao.com
ifconfig ens33:0 12.0.0.9 netmask 255.0.0.0 ens33:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 12.0.0.9 netmask 255.0.0.0 broadcast 12.255.255.255 ether 00:0c:29:37:9b:9a txqueuelen 1000 (Ethernet)
-
[root@www ~]# vim /usr/local/nginx/conf/nginx.conf server { listen 12.0.0.3:80; server_name www.jd.com; charset utf-8; access_log logs/jd.access.log; location / { root /var/www/html/jd; index index.html index.htm; location /status { stub_status on; access_log off; } } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } server { listen 12.0.0.9:80; //修改了一个ip server_name www.taobao.com; charset utf-8; access_log logs/taobao.access.log; location / { root /var/www/html/taobao; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } [root@www ~]# nginx -t //验证nginx配置文件语法问题 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