阿里云centos7服务器nginx配置及常见问题解答

前言:html

本文参考了jackyzm的博客:https://www.cnblogs.com/jackyzm/p/9600738.html,进行了内容的更新,并请注意这里适用的版本是centos7的版本。而且本文的配置方式曾经在版本8上失败过,所以查看本文前最好先肯定服务器的版本。nginx

而关于nginx部分问题的处理,则是参考了文章:http://www.mamicode.com/info-detail-3008792.html进行,其中包括的部分错误以下:c++

1.make[1]: *** [objs/Makefile:473: objs/src/core/ngx_murmurhash.o] Error 1vim

2.make[1]: *** [objs/Makefile:774: objs/src/os/unix/ngx_user.o] Error 1centos

3.make[1]: *** [objs/Makefile:769: objs/src/event/ngx_event_openssl.o] Error 1浏览器

本文针对这些问题和一些其余问题的解决方案都进行了阐述和流程上的改良安全

 

内容流程:服务器

0.前置说明,1.安装gcc环境,2.安装pcre库,3.安装ssl库,4.安装zlib库,5.安装nginx,附:nginx在make过程当中可能出现的问题,6.nginx启动与使用,7.nginx启动后没法访问的问题解决测试

 

具体内容:ui

0.前置说明

在centos系统中,yum源不直接提供nginx的安装,所以能够经过切换yum源的方法进行nginx的安装,也能够经过直接下载依赖库和nginx安装包的方法来实现,本文介绍的是后者。

一些必要的库和nginx的关系:nginx 中gzip模块须要 zlib 库,rewrite模块须要 pcre 库,ssl 功能须要openssl库

而后本文将选定/usr/local为安装目录,一些库的具体版本号各位可根据实际改变,但因为互相依赖,因此最新的可能存在一些问题,谨慎选择。

且如下命令均需在root权限下执行

1.安装gcc gcc-c++(如新环境,未安装请先安装)

$ yum install -y gcc gcc-c++

 

2.安装pcre库

$ cd /usr/local/

$ wget https://sourceforge.net/projects/pcre/files/pcre/8.36/pcre-8.36.tar.gz

(注:pcre版本会实时更新,8.36版本一样是旧版本,若是须要最新版本自行搜索pcre便可,而后选择新版本,不过新版本可能会不太稳定,须要注意。

当前网址获取新版本的方法为访问:https://sourceforge.net/projects/pcre/files/pcre/)

$ tar -zxvf pcre-8.36.tar.gz

$ cd pcre-8.36

$ ./configure

$ make && make install

如报错:configure: error: You need a C++ compiler for C++ support

解决方案:yum install -y gcc gcc-c++

 

3.安装ssl库

$ cd /usr/local/

$ wget https://www.openssl.org/source/openssl-1.0.1j.tar.gz

(注:openssl一样有版本更新,这里为2020.04的版本,当前网址获取新版本的方法为访问:https://www.openssl.org/source/

这里没有采用1.1.x等版本是由于在后面安装nginx出现了没法解决的错误,因此选择了这一较久的版本,若是能够解决,能够进行尝试,若是能告知解决方案或者成功案例就更感谢了)

$ tar -zxvf openssl-1.0.1j.tar.gz

$ cd openssl-1.0.1j

$ ./config

$ make && make install

 

4.安装zlib库

$ cd /usr/local/

$ wget http://zlib.net/zlib-1.2.11.tar.gz

(注:同前,版本更新网址为:http://zlib.net/)

$ tar -zxvf zlib-1.2.11.tar.gz

$ cd zlib-1.2.11

$ ./configure

$ make && make install

 

5.安装nginx

$ cd /usr/local/

$ wget http://nginx.org/download/nginx-1.8.0.tar.gz

(注:同前,获取新版本的网址为:http://nginx.org/download/)

$ tar -zxvf nginx-1.8.0.tar.gz

$ cd nginx-1.8.0

$ ./configure --user=nobody --group=nobody --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_gzip_static_module --with-http_realip_module --with-http_sub_module --with-http_ssl_module --with-pcre=/usr/local/pcre-8.36 --with-zlib=/usr/local/zlib-1.2.11 --with-openssl=/usr/local/openssl-1.0.1j

(注: --with-http_ssl_module:这个不加后面在nginx.conf配置ssl:on后,启动会报nginx: [emerg] unknown directive "ssl" in /opt/nginx/conf/nginx.conf 异常;儿针对pcre、zlib和openssl等等的指定,则是为了保证nginx可以和以前下载的版本匹配一致,注意路径和版本号须要修改成和以前下载对应的样子)

$ make && make install

 

附:nginx在make过程当中可能出现的问题:

报错1:make[1]: *** [objs/Makefile:460: objs/src/core/ngx_murmurhash.o] Error 1

将警告视为报错进行了程序终止

解决方案:进入到nginx安装目录的objs文件夹中,修改Makefile文件:

按下a进入编辑模式,将CFLAGS中的-Werror去除,而后按下esc,输入:wq保存退出

而后再次进行尝试make && make install指令

 

报错2:make[1]: *** [objs/Makefile:747: objs/src/os/unix/ngx_user.o] Error 1

提示咱们struct crypt_data’没有名为‘current_salt’的成员:cd.current_salt[0] = ~salt[0];原文做者说最好的办法是换一个版本,这里开始选择的1.9出现了报错,可是切换成了nginx1.8后仍是没有效果,所以便在nginx1.8的基础上使用了替代方案:代码注释

即在nginx安装目录下经过执行vim src/os/unix/ngx_user.c,对ngx_user.c第36行的代码进行注释,先后以下:

 

而后保存退出,再次尝试make && make install

 

报错3:make[1]: *** [objs/Makefile:769: objs/src/event/ngx_event_openssl.o] Error 1

说明ssl版本没有告知nginx,应该是前面./configure的时候没有指定ssl版本致使的,须要从新configure,即须要在configure时增长ssl的指定。

--with-openssl=/usr/local/openssl-1.0.1j

 

报错4:./configure: error: SSL modules require the OpenSSL library.

能够尝试执行指令:

yum -y install openssl openssl-devel

 

报错5:./configure: error: the HTTP gzip module requires the zlib library

configure时没有制定好另外两个库的路径,在–prefix后面接如下命令(具体使用方式见./configure指令的执行):

--with-pcre=/usr/local/pcre-8.36 指的是pcre-8.36 的源码路径。--with-zlib=/usr/local/zlib-1.2.11 指的是zlib-1.2.11 的源码路径。

 

若是以上报错仍是没法解决问题,最好查看centos版本是不是7,我就是在更换系统版本后直接成功的。若是遇到其余报错,最好进行检索,没法解决十分抱歉。

 

6.nginx启动与使用

$ /usr/local/nginx/sbin/nginx

启动成功的标志:打开浏览器访问服务器的ip地址,若是浏览器出现Welcome to nginx! 则表示 nginx 已经安装并运行成功。

其余的一些命令以下:

重启:

$ /usr/local/nginx/sbin/nginx -s reload

中止:

$ /usr/local/nginx/sbin/nginx -s stop

测试配置文件是否正常:

$ /usr/local/nginx/sbin/nginx -t

强制关闭:

$ pkill nginx

注:这里没有设置软连接,所以只能经过这么长的指令进行管理,若有须要可自行检索处理,后面若是我有时间也会更新到本文中。

 

7.nginx启动后没法访问的问题解决

这里若是还看不到nginx的页面,多是服务器的安全组没有配置致使的,例如阿里云就须要在这里添加http,端口80的配置,以下:

安全组配置以后只需刷新页面便可看到nginx的成功页面。

 

最后祝各位配置顺利。

相关文章
相关标签/搜索