nginx是一款非常好用的反向代理服务器,web服务器,很多人都在使用nginx作为网络服务器,要知其然还要知所以然,所以源码安装是一切实践的基础,让我们动手搞起来。
(资料图)
官网下载合适的版本,建议选择稳定版本,我选择的基础镜像是Centos7,nginx版本是nginx-1.20.2。
# 官网地址:https://nginx.orgwget https://nginx.org/download/nginx-1.20.2.tar.gz
wget https://nginx.org/download/nginx-1.20.2.tar.gz \tar zxf nginx-1.20.2.tar.gz && \cd nginx-1.20.2
目录和目录说明:
|-- CHANGES|-- CHANGES.ru|-- LICENSE|-- README|-- auto|-- conf|-- configure|-- contrib|-- html|-- man`-- src
auto目录:用于编译时的文件,以及相关lib库,编译时对对操作系统的判断等,都是为了辅助./configure命令执行的辅助文件。CHANGES文件:就是当前版本的说明信息,比如新增的功能,修复的bug,变更的功能等CHANGES.ru文件:作者是俄罗斯人,生成了一份俄罗斯语言的CHANGE文件conf目录:是nginx编译安装后的默认配置文件或者示列文件,安装时会拷贝到安装的文件夹里面。configure文件:编译安装前的预备执行文件。html目录:编译安装的默认的2个标准web页面,安装后会自动拷贝到nginx的安装目录下的html下。man目录:nginx命令的帮助文档,linux上可以使用man命令查看帮助src:nginx的源码文件开始编译,执行./configure ,不清楚的可以执行./configure --help #查看./configure 支持哪些参数。
# 安装目录--prefix=PATH# 配置文件--conf-path=PATH# 设置用户和工作组--user=USER--group=GROUP
--with-http_ssl_module 表示未开启的模块,如果想启动在编译安装时添加。
--without-http_uwsgi_module 默认是已经安装开启的模块,如果想默认关闭添加参数
备注:日常生产环境使用nginx,编译模块按照nginx官方yum安装的模块,基本能满足95%以上的生产需求。yum安装模块如下,可自行参考:
./configure --prefix=/usr/local/nginx--with-compat \--with-debug \--with-file-aio \--with-google_perftools_module \--with-http_addition_module \--with-http_auth_request_module \--with-http_dav_module \--with-http_degradation_module \--with-http_flv_module \--with-http_gunzip_module \--with-http_gzip_static_module \--with-http_image_filter_module=dynamic \--with-http_mp4_module \--with-http_perl_module=dynamic \--with-http_random_index_module \--with-http_realip_module \--with-http_secure_link_module \--with-http_slice_module \--with-http_ssl_module \--with-http_stub_status_module \--with-http_sub_module \--with-http_v2_module \ --with-http_xslt_module=dynamic \--with-mail=dynamic \ --with-mail_ssl_module --with-pcre \--with-pcre-jit --with-stream=dynamic \--with-stream_ssl_module \--with-stream_ssl_preread_module \--with-threads --with-cc-opt="-O2 -g -pipe -Wall \-Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong \--param=ssp-buffer-size=4 \ -grecord-gcc-switches -m64 -mtune=generic -fPIC" \--with-ld-opt="-Wl,-z,relro -Wl,-z,now -pie"
备注:nginx使用yum源安装,可能导致业务上很多需要的功能模块没有开启,还是按需自己编译比较合适。
以上扩展需要的安装源,先执行依赖,再执行./configure
,然后make install
yum -y update && yum install -y gcc wget tree pcre-devel \zlib-devel openssl openssl-devel \libxml2 libxml2-dev libxslt-devel gd-devel \ncurses-devel perl perl-ExtUtils-Embed \gperftools
安装完成后,添加软链接,加入到环境变量中
ln -s /usr/local/nginx/sbin/nginx /usr/local/bin/nginx
启动nginx
nginx -c /usr/local/nginx/conf/nginx.conf
至此,nginx安装完成了,nginx的安装还是非常简单的,主要的难点在于需要先安装编译所需要的依赖。
过程中的报错信息已经解决办法:
yum -y install libxml2 libxml2-devyum -y install libxslt-devel
yum -y install perl-devel perl-ExtUtils-Embed
yum install –y gperftools
yum -y install gd-devel
yum install -y zlib-devel