编译安装
nginx版本:1.21.6
联网安装
1. 安装依赖
yum -y install gcc gcc-c++ automake pcre pcre-devel zlib zlib-devel openssl openssl-devel
2. 创建安装目录
mkdir /data/nginx
3. 编译安装
上传nginx源码包至系统,解压并进入目录
tar -zxvf nginx-1.21.6.tar.gz
cd nginx-1.21.6/
生成 makefile 文件,指定安装路径、模块
./configure --prefix=/data/nginx --with-http_ssl_module --with-stream
编译 && 安装
make
make install
4. 启动nginx
/data/nginx/sbin/nginx -c /data/nginx/conf/nginx.conf
离线安装
说明:已打包离线yum源,将其上传至附件,附件名为nginx_gcc_packages.tar.gz
nginx版本:1.21.6
1. 配置本地yum源
创建存放目录
mkdir /repo
上传打包好的离线yum源,并解压至/repo目录下
tar -zxvf nginx_gcc_packages.tar.gz -C /repo/
配置本地yum源
# mkdir /etc/yum.repos.d/bak
# mv /etc/yum.repos.d/* /etc/yum.repos.d/bak/
# vim /etc/yum.repos.d/local.repo//文件中添加以下内容
[local]
name=local
baseurl=file:///repo/nginx_gcc_packages/
gpgcheck=0
enabled=1
验证yum源是否可用
# yum clean all
# yum repolist
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
repo id repo namestatus
local local42
repolist: 42
如果repolist的值为0,则yum源存在问题
2. 安装依赖
yum -y install gcc gcc-c++ automake pcre pcre-devel zlib zlib-devel openssl openssl-devel
3. 创建安装目录
mkdir /data/nginx
4. 编译安装
上传nginx源码包至系统,解压并进入目录
tar -zxvf nginx-1.21.6.tar.gz
cd nginx-1.21.6/
生成 makefile 文件,指定安装路径、模块
./configure --prefix=/data/nginx --with-http_ssl_module --with-stream
编译 && 安装
make
make install
5. 启动nginx
/data/nginx/sbin/nginx -c /data/nginx/conf/nginx.conf
yum 安装
联网安装
1. 添加epel源
yum -y install epel-release.noarch
2. 安装nginx,目前版本为1.20.1
yum -y install nginx
3. 启动nginx并设置开机自启
systemctl start nginx
systemctl enable nginx
离线安装
说明:已打包离线yum源,将其上传至附件,附件名为nginx_packages.tar.gz
1. 配置本地yum源
创建存放目录
mkdir /repo
上传打包好的离线yum源,并解压至/repo目录下
tar -zxvf nginx_packages.tar.gz -C /repo/
配置本地yum源
# mkdir /etc/yum.repos.d/bak
# mv /etc/yum.repos.d/* /etc/yum.repos.d/bak/
# vim /etc/yum.repos.d/local.repo//文件中添加以下内容
[local]
name=local
baseurl=file:///repo/nginx_packages/
gpgcheck=0
enabled=1
验证yum源是否可用
# yum clean all
# yum repolist
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
repo id repo namestatus
local local5
repolist: 5
如果repolist的值为0,则yum源存在问题
2. 安装nginx,目前版本为1.20.1
yum -y install nginx
3. 启动nginx并设置开机自启
systemctl start nginx
systemctl enable nginx
Nginx配置优化
编辑nginx主配置文件nginx.conf
# vim /data/nginx/conf/nginx.conf 修改内容如下
#user nobody;
worker_processes 2;
worker_cpu_affinity 01 10;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pidlogs/nginx.pid;
worker_rlimit_nofile 65535;
events {
worker_connections 8192;
}
http {
include mime.types;
......
修改参数释义
worker_processes 2;
设置nginx工作进程数,默认为1,推荐按照cpu数目来指定,一般跟cpu核数相同,如2核就设置为2,4核设置为4。
worker_cpu_affinity 01 10;
将worker与CPU绑定,如果是4核cpu,工作进程数为4的话,配置示例为worker_cpu_affinity 0001 0010 0100 1000;
worker_rlimit_nofile 65535
指定此进程打开的最大文件描述符的值 worker_rlimit_nofile 65535,理论值应该是系统的最多打开文件数(ulimit -n)与nginx进程数相除,但是nginx分配请求并不是那么均匀,所以最好与ulimit -n的值保持一致。
worker_connections 8192;
设置每个工作进程能接受最大的客户端的连接数,默认为1024