centos6.8和ubuntu 16.04 安装nginx 1.8.1

来源:互联网 发布:乐高机器人编程语言 编辑:程序博客网 时间:2024/06/06 07:08

nginx是C语言开发,建议在linux上运行,现在我使用的是centos6.8座位安装环境。


安装nginx之前需要安装一系列环境:

centos:

1.gcc 安装,如果没有gcc环境,则需要安装gcc:yum install gcc-c++


2.PCRE pcre-devel 安装:yum install -y pcre pcre-devel

  PCRE(PerlCompartible Regular Expressions)是一个Prel库,包括perl兼容的正则表达式库。nginx的http模块使用pcre来解析正则表达式,所以需要在linux上安装pcre库。

注:pcre-devel是使用pcre开发的一个二次开发库。nginx也需要此库


3.zlib 安装: yum install -y zlib zlib-devel

    zlib库提供了很多压缩和解压的方式,nginx使用zlib对http包的内容进行gzip,所以需要在linux上安装zlib库。


4.OpenSSL 安装:yum install -y openssl openssl-devel

       OpenSSL是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及SSL协议,并提供丰富的应用程序供测试或其它目的使用。

       nginx不仅支持http协议,还支持https(即在ssl协议上传输http),所以需要在linux安装openssl库。

ubuntu:

apt-get install build-essential
apt-get install libtool

安装nginx:

1.可以在网站:http://nginx.org/en/download.html 中下载ngxin:nginx-1.8.1.tar.gz

2.将下载的nginx解压:

     tar -vxf nginx-1.8.1.tar.gz

3.安装nginx:

   进入解压的nginx-1.8.1目录下执行 configure文件:

    使用命令:

./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_spdy_module --with-http_stub_status_module --with-pcre

注意:如果出现这个错误

./configure: error: SSL modules require the OpenSSL library.You can either do not enable the modules, or install the OpenSSL libraryinto the system, or build the OpenSSL library statically from the sourcewith nginx by using --with-openssl=<path> option.
解决办法:

apt-get  install opensslapt-get install libssl-dev

然后:make

再然后:make install

就好了


也可以直接:

1)下载  wget http://nginx.org/download/nginx-1.8.1.tar.gz
2) 解压  tar -zxvf nginx-1.8.1.tar.gz
3)cd nginx-1.8.1
4)./configure --with-http_ssl_module --with-http_spdy_module --with-http_stub_status_module --with-pcre
5)make && make install


启动nginx以及停止:

1.启动

进入/usr/lcoal/nginx/sbin中,里面存在一个nginx文件,执行命令:

./nginx

没有任何翻译就是正常的了
可以使用命令查看进程:

ps aux|grep nginx

然后看见:

root       4260  0.0  0.1  44628  1148 ?        Ss   02:55   0:00 nginx: master process ./nginxnobody     4261  0.0  0.1  45076  1732 ?        S    02:55   0:00 nginx: worker processroot       4325  0.0  0.0 103328   892 pts/0    S+   03:06   0:00 grep nginx


2.停止


在sbin目录下使用:

./nginx -s stop


3.配置文件:

路径:/usr/local/nginx/conf下的nginx.conf


4.刷新配置:

./nginx -s reload

修改配置文件后不用重启nginx,直接执行上命令,就可以刷新配置生效

验证是否安装成功可以使用命令:

curl http://127.0.0.1

注意:如果无法访问,可能是防火墙的问题,可以使用一下命令:

查看防火墙状态:service iptables status

关闭命令:  service iptables stop 
永久关闭防火墙:chkconfig iptables off


如果无效,可以使用以下命令:

将iptables和selinux关闭

以下4条命令清除iptables的配置

iptables -F

iptables -F -t nat

iptables -X

iptables -X -t nat

setenforce 0 #关闭selinux


或者使用命令:

vi /etc/sysconfig/iptables

在文件中将涉及的80端口打开

添加:-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT

然后:service iptables restart


如果还存在问题,可以根据如下链接解决:

http://h2ofly.blog.51cto.com/6834926/1324588