不得不了解的web服务,错过了损失一个YI.带你详解nginx服务!
Posted 龙少。
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了不得不了解的web服务,错过了损失一个YI.带你详解nginx服务!相关的知识,希望对你有一定的参考价值。
一.nginx服务
nginx是一款高性能,轻量级的web服务软件。
nginx稳定性高,系统消耗资源低,对HTTP并发连接的处理能力高,单台物理服务器可支持3万到5万个并发请求。
二.编译安装nginx服务
1.准备安装环境
关闭防火墙,并将nignx的安装软件包上传至/opt目录下
[root@localhost ~]# cd /opt
[root@localhost opt]# ls
nginx-1.12.2.tar.gz rh
[root@localhost opt]# systemctl stop firewalld.service
[root@localhost opt]# systemctl disable firewalld.service
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@localhost opt]# setenforce 0
[root@localhost opt]#vim /etc/resolv.conf
[root@localhost ~]# ping www.baidu.com
PING www.a.shifen.com (36.152.44.95) 56(84) bytes of data.
64 bytes from 36.152.44.95 (36.152.44.95): icmp_seq=1 ttl=128 time=4.15 ms
64 bytes from 36.152.44.95 (36.152.44.95): icmp_seq=2 ttl=128 time=4.36 ms
64 bytes from 36.152.44.95 (36.152.44.95): icmp_seq=3 ttl=128 time=5.38 ms
64 bytes from 36.152.44.95 (36.152.44.95): icmp_seq=4 ttl=128 time=3.85 ms
64 bytes from 36.152.44.95 (36.152.44.95): icmp_seq=5 ttl=128 time=5.46 ms
^C
--- www.a.shifen.com ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4008ms
rtt min/avg/max/mdev = 3.856/4.644/5.466/0.658 ms
2.安装软件依赖包
[root@localhost opt]#yum -y install gcc gcc-c++ pcre-devel zlib-devel make
3.编译安装nginx
(1)解压软件包到/opt目录下
[root@localhost ~]# cd /opt
[root@localhost opt]# ls
nginx-1.12.2.tar.gz rh
[root@localhost opt]# tar zxvf nginx-1.12.2.tar.gz -C /opt
[root@localhost opt]# ls
nginx-1.12.2 nginx-1.12.2.tar.gz rh
(2)自定义详细相关配置
[root@localhost nginx-1.12.2]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module
cd /opt/nginx-1.12.2./
./configure \\
--prefix=/usr/local/nginx \\
--user=nginx \\ //管理用户的身份
--group=nginx \\ //管理用户的组
--with-http_stub_status_module //http状态统计模块
(3)make编译并make install安装
make && make install
(4)创建软链接
创建软链接对nginx命令执行路径优化
[root@localhost nginx-1.12.2]# ln -s /usr/local/nginx/sbin/* /usr/local/sbin
(5)创建nginx的程序用户
创建程序用户不指定家目录
[root@localhost nginx-1.12.2]# useradd -M -s /sbin/nologin nginx
三.nginx运行控制
1.检查服务,启动服务
nginx -t 检查配置文件是否配置正确
nginx 启动服务
[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
[root@localhost nginx-1.12.2]# nginx
[root@localhost nginx-1.12.2]# netstat -antp | grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 33752/nginx: master
[root@localhost nginx-1.12.2]#
查看占用端口的服务
[root@localhost nginx-1.12.2]# lsof -i:80
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
nginx 33752 root 6u IPv4 297426 0t0 TCP *:http (LISTEN)
nginx 33753 nginx 6u IPv4 297426 0t0 TCP *:http (LISTEN)
[root@localhost nginx-1.12.2]#
2.停止服务,重载服务
(1)停止服务
可以使用杀进程的方式
首先查看nginx的PID号
cat /usr/local/nginx/logs/nginx.pid
暴力杀死
kill -3. < PID号>
友好杀死
kill -s QUI’T <PID号>
killall -3 nginx
killall -s QUIT nginx
(2)重载服务
kill -1 <PID号>
kill -s HUP <PID号>
killall -1 nginx
killall -s HUP <PID号>
选项说明
-s:指定信号种类
HUP:重载配置
QUIT:退出进程
3.添加系统管理
添加nginx系统服务的两种方式
(1)service管理
[root@localhost opt]# cd /etc/init.d/
[root@localhost init.d]# ls
functions netconsole network README
[root@localhost init.d]# vim /etc/init.d/nginx
[root@localhost init.d]# ls
functions netconsole network nginx README
[root@localhost init.d]chmod +x /etc/init.d/nginx //给权限
[root@localhost init.d]chkconfig --add nginx //添加为系统服务
[root@localhost init.d]# systemctl stop nginx
[root@localhost init.d]# systemctl start nginx
[root@localhost init.d]# systemctl status nginx
● nginx.service - SYSV: Nginx Service Control Script
Loaded: loaded (/etc/rc.d/init.d/nginx; bad; vendor preset: disabled)
Active: active (running) since 二 2021-06-22 22:53:34 CST; 2s ago
Docs: man:systemd-sysv-generator(8)
Process: 36101 ExecStart=/etc/rc.d/init.d/nginx start (code=exited, status=0/SUCCESS)
Tasks: 2
CGroup: /system.slice/nginx.service
├─36103 nginx: master process /usr/local/nginx/sbin/nginx
└─36104 nginx: worker process
6月 22 22:53:34 localhost.localdomain systemd[1]: Starting SYSV: Nginx Servi...
6月 22 22:53:34 localhost.localdomain systemd[1]: Started SYSV: Nginx Servic...
Hint: Some lines were ellipsized, use -l to show in full.
[root@localhost init.d]#
可在/etc/rc.d/init.d目录下查看到nginx服务
(2)systemctl管理
vim /usr/lib/systemd/system/nginx.service
[root@localhost init.d]# chmod 754 /lib/systemd/system/nginx.service //设置754权限是一种安全优化
[root@localhost init.d]# systemctl start nginx.service
[root@localhost init.d]# systemctl enable nginx.service
[root@localhost init.d]# systemctl status nginx.service
● nginx.service - SYSV: Nginx Service Control Script
Loaded: loaded (/etc/rc.d/init.d/nginx; bad; vendor preset: disabled)
Active: active (running) since 二 2021-06-22 23:11:46 CST; 41min ago
Docs: man:systemd-sysv-generator(8)
Process: 36493 ExecStart=/etc/rc.d/init.d/nginx start (code=exited, status=0/SUCCESS)
Tasks: 2
CGroup: /system.slice/nginx.service
├─36495 nginx: master process /usr/local/nginx/sbin/nginx
└─36496 nginx: worker process
6月 22 23:11:46 localhost.localdomain systemd[1]: Starting SYSV: Nginx Servi...
6月 22 23:11:46 localhost.localdomain systemd[1]: Started SYSV: Nginx Servic...
Hint: Some lines were ellipsized, use -l to show in full.
四.nginx的配置文件
1.配置文件的位置/usr/local/nginx/conf/nginx.conf
[root@localhost ~]# cd /usr/local/nginx/conf/
[root@localhost conf]# ls
fastcgi.conf koi-win scgi_params
fastcgi.conf.default mime.types scgi_params.default
fastcgi_params mime.types.default uwsgi_params
fastcgi_params.default nginx.conf uwsgi_params.default
koi-utf nginx.conf.default win-utf
[root@localhost conf]# cp nginx.conf nginx.conf.bak //将配置文件进行备份
[root@localhost conf]# ls
fastcgi.conf mime.types scgi_params.default
fastcgi.conf.default mime.types.default uwsgi_params
fastcgi_params nginx.conf uwsgi_params.default
fastcgi_params.default nginx.conf.bak win-utf
koi-utf nginx.conf.default
koi-win scgi_params
2.配置文件详解
[root@localhost conf]# cat nginx.conf
#user nobody; //默认运行/管理用户
worker_processes 1; //工作进程运行数量,可配置成服务器内核数*2,如果网站访问量不大,一般设为1
#error_log logs/error.log; //错误日志文件路径/级别
#error_log logs/error.log notice; //这里是相对路径
#error_log logs/error.log info;
#pid logs/nginx.pid; //pid文件位置
events { //事件
worker_connections 1024; //每个进程最多处理的连接数量
}
http { //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; //访问日志位置,默认注释
sendfile on; //支持文件发送(下载)
#tcp_nopush on;
#keepalive_timeout 0; //此项允许或禁止使用socket的 TCP_CORK的选项(发送数据包前先缓存数据),此选项仅在使用 sendfile的时候使用
keepalive_timeout 65; //连接保持超时时间,单位:秒
#gzip on; //压缩模块on表示开启
server { //web服务相关的一些配置
listen 80; //默认监听端口
server_name localhost; //站点域名;在本地没有dns服务下,可以支持域名访问
#charset koi8-r; //字符集支持( 修改为中文) UTF-8
#access_log logs/host.access.log main; //此web服务的主访问日志只保存htpd服务的访问日志
location / { //“/"根目录配置( 浏览器中,www. baidu. com./
root html; //网页的目录文件;网站根目录的位置/usr/local/nginx/html ( 相对路径)
index index.html index.htm; //支持的首页文件格式
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html; //当发生错误的时候能够显示一个预定义的错误页面
location = /50x.html { //错误页面配置
root html;
}
# proxy the php scripts to Apache listening on 127.0.0.1:80 //以下是支持PHP及跳转的配置
#
#location ~ \\.php$ {
# proxy_pass http://127.0.0.1;
#}
# 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;
#}
# 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 server //nginx加密模块:以注释的方式给出模板 https的相关配置
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# 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.文件最大打开数
在Linux平台上,在进行高并发TCP连接处理时,最高的并发数量都要受到系统对用户单一进程同时可打开文件数量的限制,可使用ulimit-a命令查看系统允许当前用户进程打开的文件数限制
最大文件打开数为65535
[root@localhost ~]# ulimit -a
core file size (blocks, -c) 0
data seg size (kbytes, -d) unlimited
scheduling priority (-e) 0
file size (blocks, -f) unlimited
pending signals (-i) 7144
max locked memory (kbytes, -l) 64
max memory size (kbytes, -m) unlimited
open files (-n) 1024
pipe size (512 bytes, -p) 8
POSIX message queues (bytes, -q) 819200
real-time priority (-r) 0
stack size (kbytes, -s) 8192
cpu time (seconds, -t) unlimited
max user processes (-u) 7144
virtual memory (kbytes, -v) unlimited
file locks (-x) unlimited
[root@localhost ~]# ulimit -n 65535 //此命令只是临时修改最大打开文件数
[root@localhost ~]# ulimit -a
core file size (blocks, -c) 0
data seg size (kbytes, -d) unlimited
scheduling priority (-e) 0
file size (blocks, -f) unlimited
pending signals (-i) 7144
max locked memory (kbytes, -l) 64
max memory size (kbytes, -m) unlimited
open files (-n) 65535
pipe size (512 bytes, -p) 8
POSIX message queues (bytes, -q) 819200
real-time priority (-r) 0
stack size (kbytes, -s) 8192
cpu time (seconds, -t) unlimited
max user processes (-u) 7144
virtual memory (kbytes, -v) unlimited
file locks (-x) unlimited
可以到配置文件修改最大文件打开数
vim /usr/local/nginx/conf/nginx.conf
4.配置配置文件和本地映射用域名访问nginx网页
vim /usr/local/nginx/conf/nginx.conf
vim /etc/hosts
浏览器访问
五.访问状态统计
1.查看HTTP_ STUB STATUS 模块
查看nginx是否配置包含HTTP_ STUB STATUS 模块
[root@localhost ~]# nginx -V
nginx version: nginx/1.12.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module
2.修改配置文件,设置HTTP_ STUB STATUS 模块
vim /usr/local/nginx/conf/nginx.conf
[root@localhost ~小曾带你深入浅出机器学习(小白入门必备,近3万字带你了解机器学习)