网站首页 > 技术教程 正文
一、前言
本文中使用本地VM虚机部署测试。
OS:CentOS Linux release 7.8.2003 (Core) 3.10.0-1160.15.2.el7.x86_64
IP:192.168.168.100
虚机配置:2核CPU、4G内存
防火墙设置:CentOS 7.8默认使用firewall,本文中禁用firewall,使用iptables。Nginx服务默认端口是80。
目录说明:
- 软件源码:/data/tools
- 部分软件安装目录:/usr/local
- WEB目录:/data/www
- 日志目录:/data/logs
特别说明:
- 文中的源码软件包除pcre都为最新稳定版本;
- 本文不介绍第三方模块的安装及添加。
二、基础依赖环境
1.基础库文件
rpm -ivh http://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
yum -y install bzip2 bzip2-devel curl curl-devel fontconfig fontconfig-devel freetype freetype-devel gd gd-devel libevent libjpeg libjpeg-devel libpng libpng-devel libwebp libwebp-devel libtiff libtiff-devel libvpx libvpx-devel libXpm libXpm-devel libxml2 libxml2-devel libxslt libxslt-devel perl pcre-devel zlib zlib-devel
2.基础编译工具
## autoconf——一个用于生成shell脚本的工具,可以自动配置软件源代码以适应多种类似POSIX的系统。让软件包在所有的不同系统上都可以进行编译。
cd /data/tools
wget http://ftp.gnu.org/gnu/autoconf/autoconf-2.71.tar.gz
tar xf autoconf-2.71.tar.gz
cd autoconf-2.71
./configure
//若是升级,请指定安装路径 ./configure --prefix=/usr/
make && make install
## automake——一种用于从名为Makefile.am的文件中自动生成Makefile.ins的工具。每个Makefile.am基本上都是一系列的make变量定义1,有时会抛出规则。使生成的Makefile.ins符合GNU Makefile标准。
cd /data/tools
wget http://ftp.gnu.org/gnu/automake/automake-1.16.3.tar.gz
tar xf automake-1.16.3.tar.gz
cd automake-1.16.3
./configure
//若是升级,请指定安装路径 ./configure --prefix=/usr/
make && make install
## libtool——一个通用库支持脚本,将使用动态库的复杂性隐藏在统一、可移植的接口中。主要的一个作用是在编译大型软件的过程中解决了库的依赖问题。
cd /data/tools
wget http://ftp.gnu.org/gnu/libtool/libtool-2.4.6.tar.gz
tar xf libtool-2.4.6.tar.gz
cd libtool-2.4.6
./configure
//若是升级,请指定安装路径 ./configure --prefix=/usr/
make && make install
3.依赖扩展软件
## libgd——GD Graphics Library(libgd)是用来动态创建图像的开源图形软件库,主要用在图像验证码、头像、网络相册等方面,用户使用十分广泛。
cd /data/tools
wget https://github.com/libgd/libgd/releases/download/gd-2.3.2/libgd-2.3.2.tar.gz
tar xf libgd-2.3.2.tar.gz
cd libgd-2.3.2
export PKG_CONFIG_PATH="/usr/local/lib/pkgconfig:${PKG_CONFIG_PATH}"
./configure --bindir=/usr/sbin/ --sbindir=/usr/sbin/ --sysconfdir=/etc/ --libdir=/usr/lib64/ --mandir=/usr/share/man/ --with-zlib --with-fontconfig --with-png --with-freetype --with-jpeg --with-webp --with-tiff
make && make install
## 部分编译参数说明
--with-webp:支持webp格式图片。
--with-tiff:支持tiff格式图片。
## libiconv——基于GNU协议的开源库,主要是解决多语言编码处理转换等应用问题由于历史原因,国际化的文字常常由于语言或者国家的原因使用不同的编码。
cd /data/tools
wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.16.tar.gz
tar xf libiconv-1.16.tar.gz
cd libiconv-1.16
./configure
make && make install
## Pcre——一个轻量级的函数库,包括perl兼容的正则表达式库。nginx的rewrite模块和http核心模块会用到pcre正则表达式语法。
cd /data/tools
wget https://ftp.pcre.org/pub/pcre/pcre-8.44.tar.gz
tar xf pcre-8.44.tar.gz
cd pcre-8.44
./configure --prefix=/usr/local/pcre/ --enable-jit --enable-utf --enable-unicode-properties --enable-pcregrep-libz --enable-pcregrep-libbz2 --enable-pcre16 --enable-pcre32
make && make install
## qrencode——一个用C语言编写的用来解析二维条形码(QR Code)的程序库,用于将数据编码为QR Code符号(二维符号体系),该二维符号体系可以通过手持终端(例如带有CCD的手机)进行扫描。
cd /data/tools
wget https://fukuchi.org/works/qrencode/qrencode-4.1.1.tar.gz
tar xf qrencode-4.1.1.tar.gz
cd qrencode-4.1.1
./configure --enable-thread-safety
make && make install
cp /usr/local/lib/libqrencode.so.4.1.1 /lib64/libqrencode.so.4
## jemalloc——内存分配器,适合多线程下的内存分配管理软件。与其它内存分配器相比,它最大的优势在于多线程情况下的高性能以及内存碎片的减少。
cd /data/tools
wget https://github.com/jemalloc/jemalloc/releases/download/5.2.1/jemalloc-5.2.1.tar.bz2
tar xjf jemalloc-5.2.1.tar.bz2
cd jemalloc-5.2.1
./autogen.sh
make && make install
echo '/usr/local/lib' > /etc/ld.so.conf.d/local.conf
ldconfig
三、安装Nginx
1.添加组及用户
groupadd -g 1000 nginx
useradd -s /bin/bash -u 1000 -g nginx nginx
2.创建相关目录及修改目录属性
## Nginx日志目录
mkdir -p /data/logs/nginx
## Web静态页面目录
mkdir -p /data/www
cd /data
## 修改目录属性
chown -R nginx.nginx www logs
3.下载编译安装Nginx
cd /data/tools/
wget http://nginx.org/download/nginx-1.20.0.tar.gz
tar xf nginx-1.20.0.tar.gz
cd nginx-1.20.0
./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --error-log-path=/data/logs/nginx/error.log --lock-path=/usr/local/nginx/var/nginx.lock --with-http_v2_module --with-http_ssl_module --with-http_flv_module --with-http_addition_module --with-http_stub_status_module --with-http_gzip_static_module --with-http_degradation_module --without-http_memcached_module --with-file-aio --with-http_image_filter_module --with-http_realip_module --with-compat --with-threads --with-http_secure_link_module --with-http_dav_module --with-http_mp4_module --with-http_xslt_module --with-pcre=/data/tools/pcre-8.44 --with-pcre-jit --with-ld-opt="-ljemalloc"
make -j 2
//make这里指定的是CPU内核数量,加快编译速度(根据实际情况修改)
make install
## 部分编译参数说明
--with-http_v2_module:提供对HTTP/2的支持,并取代ngx_http_spdy_module模块。
--with-http_ssl_module:开启SSL支持,可用于https连接。开启此模块需要先安装OpenSSL。
--with-http_flv_module:开启Flv流视频格式的支持,可以通过起始点进行访问请求。
--with-http_addition_module:一个过滤器,用于在响应之前和之后添加文本。
--with-http_stub_status_module:提供对基本状态信息的访问,用来监控 Nginx的当前状态。
--with-http_gzip_static_module:开启预压缩功能,此功能主要是针对静态文件请求后,自动预压缩为.gz文件,返回给客户。
--with-http_degradation_module:允许在内存不足的情况下返回204或444给客户端。
--without-http_memcached_module:禁用ngx_http_memcached_module支持(该模块用来提供简单的缓存,以提高系统效率)。
--with-file-aio:开启文件异步io,如果网站涉及到大量的io操作(例如图片站),使用这个选项会大大提高性能(linux内核需要在2.6.22之后)。
--with-http_image_filter_module:开启图片转换功能,可以对JPEG,GIF,PNG和WebP格式的图像进行转换格式、尺寸等,依赖 libgd 库。
--with-http_realip_module:开启realip模块,从字面理解是真实IP,这里的功能是在反向代理过程中,需要将当前用户的IP地址,通过X-Forwarded-For头部来传递给后端的服务器,这样获取到的IP地址就是访问者的真实IP,而不是Nginx代理服务器的IP。
--with-compat:动态模块的兼容性。
--with-threads:支持多线程。
--with-http_secure_link_module:用于检查请求的链接的真实性,保护资源免受未经授权的访问并限制链接的寿命。
--with-http_dav_module:开启WebDAV功能,可以通过WebDAV协议管理Web内容。该模块处理HTTP和WebDAV方法PUT,DELETE,MKCOL,COPY和MOVE。
--with-http_mp4_module:开启MP4流媒体支持,主要为H.264/AAC编码格式的支持,扩展名可以为.mp4, .m4v, .m4a。可以通过其实位置请求。
--with-http_xslt_module:在响应XML文件时,转为一个或多个XSLT样式。
--with-pcre=/data/tools/pcre-8.44:启用pcre库并指向pcre源代码目录。
--with-pcre-jit:开启此选项能够显著提高正则表达式的处理效率,需要8.20或更新版本的PCRE。
--with-ld-opt="-ljemalloc":使用jemalloc来优化Nginx内存管理。
备注:本文中配置的编译参数仅供参考,正式生产环境根据实际需求进行修改。
四、配置
1.配置启动脚本
## 创建启动脚本
vi /usr/lib/systemd/system/nginx.service
//输入以下内容
[Unit]
Description=Nginx - High Performance Web Server
Documentation=http://nginx.org/en/docs/
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
## 为脚本添加执行权限
chmod a+x /usr/lib/systemd/system/nginx.service
## 重新加载服务配置文件
systemctl daemon-reload
2.修改配置文件nginx.conf
## 创建虚拟主机配置文件目录,方便配置管理
mkdir -p /usr/local/nginx/conf/vhost
## 备份原有的主配置文件nginx.conf
cd /usr/local/nginx/conf/
mv nginx.conf nginx.confbak
## 新建主配置文件nginx.conf
vi nginx.conf
//输入以下内容
user nginx nginx;
#worker_processes 1;
worker_processes 2;
worker_cpu_affinity 0101 1010;
#worker_processes 4;
#worker_cpu_affinity 1000 0100 0010 0001;
#worker_processes 8;
#worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;
error_log /data/logs/nginx/error.log;
pid /usr/local/nginx/logs/nginx.pid;
worker_rlimit_nofile 65535;
events {
use epoll;
worker_connections 65535;
}
http {
include /usr/local/nginx/conf/mime.types;
default_type application/octet-stream;
log_format json '{"@timestamp":"$time_iso8601",'
'"client":"$remote_addr",'
'"user":"$remote_user",'
'"url":"$request",'
'"status":"$status",'
'"cookie":"$http_cookie",'
'"size":"$body_bytes_sent",'
'"referer":"$http_referer",'
'"useragent":"$http_user_agent",'
'"xforwardedfor":"$http_x_forwarded_for",'
'"requesttime":"$request_time",'
'"upstreamresponsetime":"$upstream_response_time",'
'"upstreamaddr":"$upstream_addr",'
'"upstreamstatus":"$upstream_status",'
'}';
access_log /data/logs/nginx/access.log json;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
client_body_buffer_size 128k;
large_client_header_buffers 4 32k;
client_header_timeout 600;
client_body_timeout 1200;
client_max_body_size 2m;
server_tokens off;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
send_timeout 1800;
keepalive_timeout 75;
types_hash_max_size 2048;
###### PHP-FastCGI
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 8 128k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;
include /usr/local/nginx/conf/vhost/*.conf;
}
注:①上述配置文件的参数设置仅供参考,实际环境请根据需求进行修改;
②worker_processes参数的值最好是等于处理器核心数量。
五、测试验证
1.启动Nginx服务并查看服务状态
systemctl start nginx.service
systemctl status nginx.service
2.验证jemalloc优化Nginx是否生效
lsof -n | grep jemalloc
3.访问测试
## 创建测试文件
mkdir -p /data/www/test
vi /data/www/test/index.html
//输入以下内容
Nginx is running!!!
## 修改测试文件属性
chown -R nginx.nginx /data/www/test
## 创建测试虚拟主机配置文件
vi /usr/local/nginx/conf/vhost/test.conf
//输入以下内容
server {
listen 80;
server_name 192.168.168.100;
root /data/www/test;
index index.html index.htm;
}
## 平滑加载Nginx配置
systemctl reload nginx.service
## 浏览器访问测试URL,本文为:http://192.168.168.100
猜你喜欢
- 2024-10-09 Nginx编译安装(nginx编译安装 make)
- 2024-09-09 Nginx学习之简单使用和练习(nginx怎么学)
- 2024-09-09 这套Nginx日志解决方案,真香(nginx日志400)
- 2024-09-09 基于Linux:Nginx安装包的安装与配置详解
- 2024-09-09 一文带你深入浅出读懂Nginx,理论+实战,双管齐下,手把手教会你
- 2024-09-09 跟我学Nginx(三)——Centos8安装Nginx
- 2024-09-09 Nginx 文件压缩!揭秘 nginx-09-compress,提升网站性能的终极秘籍
- 2024-09-09 Nginx源码安装并开启http2.0以及brotli压缩
- 2024-09-09 编译安装Nginx,并使用jemalloc优化内存分配
- 2024-09-09 Nginx简单使用和练习(nginx操作)
你 发表评论:
欢迎- 最近发表
-
- Win11学院:如何在Windows 11上使用WSL安装Ubuntu
- linux移植(Linux移植freemodbus)
- 独家解读:Win10预览版9879为何无法识别硬盘
- 基于Linux系统的本地Yum源搭建与配置(ISO方式、RPM方式)
- Docker镜像瘦身(docker 减小镜像大小)
- 在linux上安装ollama(linux安装locale)
- 渗透测试系统Kali推出Docker镜像(kali linux渗透测试技术详解pdf)
- Linux环境中部署Harbor私有镜像仓库
- linux之间传文件命令之Rsync傻瓜式教程
- 解决ollama在linux中安装或升级时,通过国内镜像缩短安装时长
- 标签列表
-
- 下划线是什么 (87)
- 精美网站 (58)
- qq登录界面 (90)
- nginx 命令 (82)
- nginx .http (73)
- nginx lua (70)
- nginx 重定向 (68)
- Nginx超时 (65)
- nginx 监控 (57)
- odbc (59)
- rar密码破解工具 (62)
- annotation (71)
- 红黑树 (57)
- 智力题 (62)
- php空间申请 (61)
- 按键精灵 注册码 (69)
- 软件测试报告 (59)
- ntcreatefile (64)
- 闪动文字 (56)
- guid (66)
- abap (63)
- mpeg 2 (65)
- column (63)
- dreamweaver教程 (57)
- excel行列转换 (56)
本文暂时没有评论,来添加一个吧(●'◡'●)