阿里云centos环境之nginx安装(四) - 日理万妓 2022-06-07 12:17 336阅读 0赞 阿里云centos环境之nging安装<四> ### 文章目录 ### * 一、前言 * 二、nginx依赖库 * * 2.1 PCRE * 2.2 zlib库安装 * 2.3 openssl * 三、安装nginx * * 动态安装模块 * * 安装官方支持的模块 * 安装第三方模块 * 四、启动nginx * 五、修改默认80端口 * * 六、基于nginx的配置网站密码认证 * 六、总结 # 一、前言 # Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like 协议下发行。其特点是占有内存少,并发能力强,事实上nginx的并发能力确实在同类型的网页服务器中表现较好,中国大陆使用nginx网站用户有:百度、京东、新浪、网易、腾讯、淘宝等。 # 二、nginx依赖库 # ## 2.1 PCRE ## 从ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/下载pcre二进制源码或者使用wget命令下载,wget命令用来从指定的URL下载文件.如下图![这里写图片描述][SouthEast] 使用wget命令下载如下: ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.39.tar.gz > 一般我们将软件安装在/usr/local目录下,故我一般在local下创建个目录soft用于保存下载的软件 > 知道下载地址是怎么拼出来的吗?呵呵 解压: tar -zxvf pcre-8.39.tar.gz -C ../ 解压pcre到上一目录即“/usr/local/”下。 进入pcre-8.39目录,执行命令: ./configure make make install > ./configure:是用来检测你的安装平台的目标特征的,这一步一般用来生成 Makefile,为下一步的编译做准备 > make:是大多数的源代码包都经过这一步进行编译,make 的作用是开始进行源代码编译,以及一些功能的提供,这些功能由他的 Makefile 设置文件提供相关的功能,比如 make install 一般表示进行安装,make uninstall 是卸载,不加参数就是默认的进行源代码编译 > makeinstall:这条命令来进行安装 ps:如果提示“configure: error: You need a C++ compiler for C++ support.”错误,说明你的linux操作系统没安装C++库,执行 yum install -y gcc gcc-c++ 安装c++库即可。 ## 2.2 zlib库安装 ## 下载 wget http://zlib.net/zlib-1.2.11.tar.gz 解压到/usr/local下 tar -zxvf zlib-1.2.11.tar.gz -C ../ 生成makeFile ./configure 编译 make 安装 make install(卸载命令:make uninstall) ## 2.3 openssl ## 下载 wget https://www.openssl.org/source/openssl-1.0.1t.tar.gz 其实访问https://www.openssl.org/source结果如下: ![这里写图片描述][SouthEast 1] 故下载地址由:[https://www.openssl.org/source][https_www.openssl.org_source] \+ 版本组成的 解压 tar -zxvf 配置 ./configure或./config 编译 make 安装 make install # 三、安装nginx # 下载 wget http://nginx.org/download/nginx-1.15.2.tar.gz 生成makefile ./configure --with-pcre=/usr/local/pcre-8.39 --with-zlib=/usr/local/zlib-1.2.11 --with-openssl=/usr/local/openssl-1.0.1t --with-http_stub_status_module --with-http_ssl_module 后面的 --with-http_stub_status_module --with-http_ssl_module 是ssl证书需要的模块,不需要的可不引用。 结果如下: ![这里写图片描述][SouthEast 2] 编译:make 安装:make install 命令执行完毕后进入 /usr/local/ingnx目录下 ![这里写图片描述][SouthEast 3] 说明安装成功了 ## 动态安装模块 ## 初次安装nginx时,没安装其它模块,nginx运行一段时间后,我想动态安装这个模块。怎么办呢? 安装任何新功能的时候,一定要先查看现有的 查看已经安装模块:nginx -V configure arguments: --with-pcre=/home/soft/pcre-8.39 --with-zlib=/home/soft/zlib-1.2.11 --with-openssl=/home/soft/openssl-1.0.1t --with-http_stub_status_module --with-http_ssl_module `动态安装模块时,要带上已经安装的模块`。 ### 安装官方支持的模块 ### [官文支持的所有模块][Link 1],找到需要安装的模块, 比如ngx\_http\_stub\_status\_module 模块,进入网页,发现安装模块的配置是–with-http\_stub\_status\_module 进入nginx目录,执行如下命令,注意加了–with-http\_stub\_status\_module。ake完之后在objs目录下就多了个nginx,这个就是新版本的程序了。注意不要执行make install ./configure --with-pcre=/usr/local/pcre-8.39 --with-zlib=/usr/local/zlib-1.2.11 --with-openssl=/usr/local/openssl-1.0.1t --with-http_stub_status_module --with-http_ssl_module --with-http_stub_status_module make cp sbin/nginx sbin/nginx.bak cp objs/nginx sbin/ ### 安装第三方模块 ### 下载第三方模块,比如[ngx\_http\_google\_filter\_module][ngx_http_google_filter_module]。使用\-–add-module添加模块,一次添加多个模块可配置多个\-–add-module。 git clone https://github.com/cuber/ngx_http_google_filter_module git clone https://github.com/yaoweibin/ngx_http_substitutions_filter_module ./configure --with-pcre=/home/soft/pcre-8.39 --with-zlib=/home/soft/zlib-1.2.11 --with-openssl=/home/soft/openssl-1.0.1t --with-http_stub_status_module --with-http_ssl_module --with-http_stub_status_module --add-module=/home/soft/ngx_http_google_filter_module --add-module=/home/soft/ngx_http_substitutions_filter_module #不要执行make install命令 make cp sbin/nginx sbin/nginx.bak cp objs/nginx sbin/ 它和安装官方支持的模块唯一区别是一个可直接编译模块,一个要下载后使用\-–add-module编译模块。 # 四、启动nginx # 在‘/usr/local/nginx/sbin/’目录下执行sh脚本./nginx 没报错说明启动成功了,默认开的是80端口,访问地址:[http://101.132.154.219/][http_101.132.154.219] 结果如下: ![这里写图片描述][SouthEast 4] 说明安装的访问成功了 # 五、修改默认80端口 # nginx默认是80端口的,如果想修改成其它端口,则进入nginx安装目录/usr/local/nginx/conf,使用命令:vi nginx.conf修改如下: ![这里写图片描述][SouthEast 5] 把80修改成9000端口,重启nginx服务,注意这里是在阿里云上的,所以需要将9000端口添加到安全组里如下图: ![这里写图片描述][SouthEast 6] 这里访问:[http://101.132.154.219:9000/][http_101.132.154.219_9000] 得到结果如下: ![这里写图片描述][SouthEast 7] ## 六、基于nginx的配置网站密码认证 ## 参考[基于nginx的配置网站密码认证][nginx] 修改配置文件/etc/nginx/conf.d/default.conf,增加两行登录验证配置: location / { proxy_pass http://10.0.0.102:5601$request_uri; #加上下面两行内容: auth_basic "登陆验证"; auth_basic_user_file /etc/nginx/htpasswd; #/etc/nginx/htpasswd是密码文件,路径自定义 } 然后使用htpasswd命令生成密码文件: [root@test102 conf.d]# htpasswd -cm /etc/nginx/htpasswd crystal #/etc/nginx/htpasswd就是配置文件里面配置的密码文件,crystal就是用户名 New password: #输入密码 Re-type new password: #再次输入密码,回车 Adding password for user crystal ## 六、总结 ## 其实安装nginx不是很复杂,就是需要安装一些依赖库,使用二进制源码需要编译和安装。nginx里面的使用及介绍需要单独学习。 [SouthEast]: /images/20220607/bdfb5f7b7ce34a8dafceac4144e47c66.png [SouthEast 1]: /images/20220607/2a2c81e957fa473aad4dcee838f28503.png [https_www.openssl.org_source]: https://www.openssl.org/source [SouthEast 2]: /images/20220607/4d732ef624c047e294581eec57149c21.png [SouthEast 3]: /images/20220607/35b20a106bf249a3813303bd481f2710.png [Link 1]: http://nginx.org/en/docs/ [ngx_http_google_filter_module]: https://github.com/cuber/ngx_http_google_filter_module [http_101.132.154.219]: http://101.132.154.219/ [SouthEast 4]: /images/20220607/9f57f576875d4e95b77391bdf6268ae5.png [SouthEast 5]: /images/20220607/02ae0d7dc7dd49709ee4abce3b939eb6.png [SouthEast 6]: /images/20220607/100db0ecada741f3aeee743bf66a8e53.png [http_101.132.154.219_9000]: http://101.132.154.219:9000/ [SouthEast 7]: /images/20220607/9e44fc6b0c024cf684c5eee3e842ac92.png [nginx]: https://www.cnblogs.com/eeexu123/p/9243298.html
还没有评论,来说两句吧...