0
点赞
收藏
分享

微信扫一扫

Linux 如何安装nginx,配置nginx,优化nginx

爱情锦囊 2022-11-22 阅读 80


参考
​​​nginx​​

一. 安装

1.使用管理员root身份
su
2.新建并打开一个repo文件
vim /etc/yum.repos.d/nginx.repo
3.按i进入insert模式,输入

[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/6/$basearch/
gpgcheck=0
enabled=1

其中,centos和6可以根据版本写成centos/7
:wq保存退出

4.安装:
​​​yum -y install nginx​​​ 开机启用:
​sudo systemctl enable nginx.service​

5.启动:
​​​service nginx start​

6.火狐浏览器输入localhost:80

Welcome to nginx!

If you see this page, the nginx web server is successfully installed and working.
Further configuration is required.

For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.

Thank you for using nginx.

二.优化 nginx.conf

1. worker数量匹配cpu数量

​lscpu​​​ 找到第四行,获得CPU(s):4,将nginx worker置为4最佳,或者auto将自动获取最大。
配置文件的位置:/etc/nginx/nginx.conf
​​​worker_processes auto;​​ (此句应写在nginx.conf里,而非linux命令)

2. 设置自动唤醒锁和worker多接

events{
accept_mutex on; 如果是off,则每个请求都会触发多个worker唤醒,浪费
multi_accept on; 如果是off,则会多worker逐个接受指令.
}

3. 文件最大句柄限制解放

默认的nginx.conf的文件句柄三项为:
worker:1, worker_connections 1024,ulimit -n
而正式的linux生产环境远远大于这样的配置
​​​cat /proc/sys/fs/file-max​​​ linux命令,查看linux服务器能打开的最大文件句柄,我的是44603
则配置
​​​worker_rlimit_nofile 30000;​​​(此句应写在nginx.conf里,而非linux命令)
将rlimit设置为30000,约等于44603的三分之二,预留一些给其他进程

4.user设置为root,避免一些苦逼的权限问题

​user root​​ (写进nginx.conf里)

最终结果是这样的:
nginx.conf

# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/

user root;
worker_processes auto;
worker_rlimit_nofile 30000;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
worker_connections 1024;
accept_mutex on;
multi_accept on;
}

http {
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 /var/log/nginx/access.log main;

sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;

include /etc/nginx/mime.types;
default_type application/octet-stream;

# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;

server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /usr/share/nginx/html;

# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;

location / {
}

error_page 404 /404.html;
location = /40x.html {
}

error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}

# Settings for a TLS enabled server.
#
# server {
# listen 443 ssl http2 default_server;
# listen [::]:443 ssl http2 default_server;
# server_name _;
# root /usr/share/nginx/html;
#
# ssl_certificate "/etc/pki/nginx/server.crt";
# ssl_certificate_key "/etc/pki/nginx/private/server.key";
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 10m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
#
# # Load configuration files for the default server block.
# include /etc/nginx/default.d/*.conf;
#
# location / {
# }
#
# error_page 404 /404.html;
# location = /40x.html {
# }
#
# error_page 500 502 503 504 /50x.html;
# location = /50x.html {
# }
# }

}

三. 使用supervisor守护nginx

虽然已经设置了开机自动开启,但是还是害怕linux在运行时,nginx崩溃。使用supervisor守护nginx
输入:

[program:nginx]
command = /usr/sbin/nginx -g 'daemon off;'
startsecs=0
autostart=true
autorestart=true
stdout_logfile=/var/log/nginx_sup.log
stopasgroup=true
killasgroup=true

​sudo supervisorctl reload​​​​sudo supervisorctl update​​ 以上,正式结束,supervisor的安装与开机自动开启不做介绍。
​sudo supervisorctl restart nginx​​ 执行它看看会出现什么效果吧.

四. FAQ

1.如果出现了80端口被占用的情况,该作何处理呢?改端口,还是替换.

​sudo netstat -lnp|grep 80​​ 举个占用80端口的例子。

tcp6       0      0 :::80                   :::*                    LISTEN      12123/httpd

对http请求,大多数服务器的端口都是80,
httpd 是apache服务的http管理框架,如果需要改用nginx,那么httpd确实已经没有用的必要性了,所以选择替换而不是改端口,httpd可以卸载,或者搁置不用.
​​​sudo systemctl disable httpd.service​​​ 取消自动启用httpd
​​​sudo systemctl stop httpd.service​​​ 关闭httpd
再次查看
​​​sudo netstat -lnp|grep 80​​​ 已经没了,重启nginx
​sudo systemctl start nginx.service​​ 设置自动开启
​sudo systemctl enable nginx.service​


举报

相关推荐

0 条评论