官方文档
sudo apt update
sudo apt install nginx
官方文档
cat /etc/yum.repos.d/nginx.repo#修改源
[root]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$branch/
gpgcheck=0
enabled=1
/ect/nginx/nginx.conf
user #用户
event.worker_connections#每个工作进程可以启动的连接
http.default_type=application/octet-stream#二进制流
sudo systemctl start nginx
sudo apt-get remove nginx nginx-common
sudo apt-get purge nginx nginx-common
sudo apt-get autoremove
sudo apt-get remove nginx-full nginx-common
systemctl status/start/stop nginx:查看、启动、关闭nginx
nginx -s reload|stop|start:重启、关闭、启动
nginx -V:查看编译参数
/etc/nginx/nginx.conf:默认配置文件
nginx -s reload :修改配置后重新加载生效
注意:配置反向代理要关闭selinux,setenforce 0
[注意] 修改nginx
配置后应该使用nginx -s reload
使配置生效
/etc/nginx/conf.d/*.conf
location = /nginx_status{
stub_status on;
access_log off;
allow 127.0.0.1;#允许本机
deny all;#其它都拒绝
}
stub_status
输出wget http://localhost/nginx_status
#centos
yum install epel-release
yum install python-pip
#ubuntu
sudo apt install python-pip
pip install ngxtop
ngxtop -c /etc/nginx/nginx.conf
ngxtop -c /etc/nginx/nginx.conf -i 'status==200'
ngxtop -c /etc/nginx/nginx.conf - g remote_addr
sudo apt-get install php php-gd php-soap php-mbstring php-xmlrpc php-dom php-fpm -y
修改
/etc/php-fpm.d/www.conf
文件中的user
和group
,与nginx.conf
中的user
一致。
user=nginx
group=nginx
注意
sudo find / -name www.conf
查看www.conf
文件位置
php-fpm
服务[ubuntu]
location ~ \.php${
root /usr/share/nginx/html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
include fastcgi_params;
}
nginx -s reload
[centos]
yum install perl rrdtool perl-libwww-perl libwww-perl perl-rrdtool -y
wget http://soft.vpser.net/status/nginx-rrd/nginx-rrd-0.1.4.tgz
tar zxvf nginx-rrd-0.1.4.tgz
cd nginx-rrd-0.1.4
cp usr/sbin/* /usr/sbin # 复制主程序文件到/usr/sbin下
cp etc/nginx-rrd.conf /etc #复制配置文件到/etc下
cp html/index.php /usr/share/nginx/html/
vi /etc/nginx-rrd.conf
RRD_DIR="/usr/share/nginx/html/nginx-rrd";
WWW_DIR="/usr/share/nginx/html";
crontab -e# 新建任务
** *** /bin/sh /usr/sbin/nginx-collect
*/1 * *** /bin/sh /usr/sbin/nginx-graph
crontab -l 查看任务
tail -f /var/log/cron
[centos]
yum -y install httpd-tools
ab -n 10000 -c 10 http://127.0.0.1/index.html
# 10000个请求,并发为10;
nginx默认是一个工作线程,1024个并发连接数。
/etc/nginx/nginx.conf
work_process 4|auto;//CPU个数
events{
worker_connections 10240;//[需要修改系统文件配置]每个工作线程的最大连接数,包含了nginx与客户端和nginx与upstream之间的连接。
multi_accept on;//可以一次建立多个连接
use epoll;
}
/etc/nginx/conf.d/default.conf
upstream
:配置反向代理集群
upstream server_pool{
server localhost:8080 weight=1 max_fails=2 fail_timeouts=30s;
server localhost:8081 weight=1 max_fails=2 fail_timeouts=30s;
keepalive 300;#300个长连接
}
location / {
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_pass http://server_pool/;
}
# 配置压缩
gzip on;
gzip_http_version 1.1;
gzip_disable "MSIE [1-6]\.(?!.*SV1)";
gzip_proxied any;
gzip_types text/plain text/css application/javascript application/x- javascript application/isonapplication/xmlapplication/vnd.ms-
fontobject application/x-font-ttf application/svg+xml application/x-icon;
gzip_vary on;#Vary:Accept-Encoding
gzip_static on;#如果有压缩好的直接使用
配置文件/etc/sysctl.conf
sysctl -w net.ipv4.tcp_syncookies=1#防止一个套接字在有过多试图连接到达时引起过载
sysctl -w net.core.somaxconn=1024#默认128,连接队列
sysctl -w net.ipv4.tcp_fin_timeout=10#timewait的超时时间
sysctl -w net.ipv4.tcp_tw_reuse=1#os直接使用timewait的连接
sysctl -w net.ipv4.tcp_tw_recycle=0#回收禁用
配置文件/etc/security/limits.conf
* hard nofile 204800
* soft nofile 204800
* soft core unlimited
* soft stack 204800
/etc/nginx/nginx.conf
sendfile on;#减少文件再应用和内核之间的拷贝
tcp_nopush on;#当数据包达到一定大小再发送
tcp_nodelay off;#有数据随时发送