Nginx 安装

本文详细介绍如何在Ubuntu上安装Nginx,并针对Nginx内核参数调整系统配置,以提升服务器性能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文的操作参考<Nginx Lua 开发实战>, 刚刚读这本书,感觉还不错。。。。当然由于本书写的稍微早一点,或许有所差异。

1 安装预装依赖

安装gcc 编译软件
sudo apt-get install -y gcc

安装openssl
sudo apt-get install openssl libssl-dev

安装pcre
sudo apt-get install libpcre3 libpcre3-dev

安装zlib
udo apt-get install zlib1g-dev -y

2. 根据Nginx 内核参数修改Ubuntu 内核参数,修改。/etc/sysctl.conf

fs.file-max = 999999
#表示进程(例如一个worker进程)可能同时打开的最大句柄数,直接限制最大并发连接数
 
net.ipv4.tcp_tw_reuse = 1
#1代表允许将状态为TIME-WAIT状态的socket连接重新用于新的连接。对于服务器来说有意义,因为有大量的TIME-WAIT状态的连接
 
net.ipv4.tcp_keepalive_time = 600
#当keepalive启用时,TCP发送keepalive消息的频率。默认是2个小时。将其调小一些,可以更快的清除无用的连接
 
net.ipv4.tcp_fin_timeout = 30
#当服务器主动关闭链接时,socket保持FN-WAIT-2状态的最大时间
 
net.ipv4.tcp_max_tw_buckets = 5000
#允许TIME-WAIT套接字数量的最大值。超过些数字,TIME-WAIT套接字将立刻被清除同时打印警告信息。默认是180000,过多的TIME-WAIT套接字会使webserver变慢
 
net.ipv4.ip_local_port_range = 1024  61000
#UDP和TCP连接中本地端口(不包括连接的远端)的取值范围
 
net.ipv4.tcp_rmem = 4096  32768  262142
net.ipv4.tcp_wmem = 4096  32768  262142
#TCP接收/发送缓存的最小值、默认值、最大值
 
net.core.netdev_max_backlog = 8096
#当网卡接收的数据包的速度大于内核处理的速度时,会有一个队列保存这些数据包。这个参数就是这个队列的最大值。
 
net.core.rmem_default = 262144
net.core.wmem_default = 262144
#内核套接字接收/发送缓存区的默认值
 
net.core.rmem_max = 2097152
net.core.wmem_max = 2097152
#内核套接字接收/发送缓存区的最大值
 
net.ipv4.tcp_syncookies = 1
#解决TCP的SYN攻击。与性能无关
 
net.ipv4.tcp_max_syn_backlog = 1024
#三次握手建立阶段SYN请求队列的最大长度,默认是1024。设置大一些可以在繁忙时将来不及处理的请求放入队列,而不至于丢失客户端的请求
net.ipv4.tcp_timestamps=1
#net.ipv4.tcp_tw_recycle=1
net.ipv4.ip_local_port_range = 1024   61000

执行systcl -p,使修改生效。

三、安装nginx

到官网下载稳定版本的nginx,然后解压,进入文件目录

###执行
./configure

#得到默认的安装信息
nginx path prefix: "/usr/local/nginx"         安装的根目录
  nginx binary file: "/usr/local/nginx/sbin/nginx" 二进制
  nginx modules path: "/usr/local/nginx/modules"
  nginx configuration prefix: "/usr/local/nginx/conf"  配置文件 目录
  nginx configuration file: "/usr/local/nginx/conf/nginx.conf" 配置文件
  nginx pid file: "/usr/local/nginx/logs/nginx.pid"
  nginx error log file: "/usr/local/nginx/logs/error.log"
  nginx http access log file: "/usr/local/nginx/logs/access.log"
  nginx http client request body temporary files: "client_body_temp"
  nginx http proxy temporary files: "proxy_temp"
  nginx http fastcgi temporary files: "fastcgi_temp"
  nginx http uwsgi temporary files: "uwsgi_temp"
  nginx http scgi temporary files: "scgi_temp"

当然你也可以 通过
./configure  --prefix=/自定义目录   --sbin-path=/执行文件目录 


最后执行 
make 
make install


ps: 默认安装的话, 因为usr文件的权限问题 需要sudo

四、 测试

#启动nginx

sudo  /usr/local/nginx/sbin/nginx -p /usr/local/nginx/


#验证,可登陆127.0.0.1 进行查看,如果发现如下,表示成功了。。。。
    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.


#停止

sudo  /usr/local/nginx/sbin/nginx -p /usr/local/nginx/ -s stop

#重新启动
sudo  /usr/local/nginx/sbin/nginx -p /usr/local/nginx/ -s reload

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值