NickBi blog

centos7.0安装nginx开启ipatables

2017-09-07

本文环境为:virtualbox+centos7.3+nginx

一. nginx安装

1 nginx安装环境

nginx是C语言开发,建议在linux上运行,本教程使用Centos7.3作为安装环境。

n gcc

安装nginx需要先将官网下载的源码进行编译,编译依赖gcc环境,如果没有gcc环境,需要安装gcc:

1
$ yum install gcc-c++ -y

n PCRE

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

1
$ yum install -y pcre pcre-devel

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

n zlib

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

1
$ yum install -y zlib zlib-devel

n openssl

OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及SSL协议,并提供丰富的应用程序供测试或其它目的使用。
nginx不仅支持http协议,还支持https(即在ssl协议上传输http),所以需要在linux安装openssl库。

1
$ yum install -y openssl openssl-devel

2 编译安装

将nginx-1.8.0.tar.gz拷贝至linux服务器。
解压:

1
2
$ tar -zxvf nginx-1.8.0.tar.gz
$ cd nginx-1.8.0

1、 configure

./configure –help查询详细参数(参考本教程附录部分:nginx编译参数)

参数设置如下:

./configure \
–prefix=/usr/local/nginx \
–pid-path=/var/run/nginx/nginx.pid \
–lock-path=/var/lock/nginx.lock \
–error-log-path=/var/log/nginx/error.log \
–http-log-path=/var/log/nginx/access.log \
–with-http_gzip_static_module \
–with-http_ssl_module \
–http-client-body-temp-path=/var/temp/nginx/client \
–http-proxy-temp-path=/var/temp/nginx/proxy \
–http-fastcgi-temp-path=/var/temp/nginx/fastcgi \
–http-uwsgi-temp-path=/var/temp/nginx/uwsgi \
–http-scgi-temp-path=/var/temp/nginx/scgi
–add-module=/opt/fastdfs-nginx-module/src/

注意:上边将临时文件目录指定为/var/temp/nginx,需要在/var下创建temp及nginx目录

2、 编译安装

1
2
$ make
$ make install

安装成功查看安装目录 :

3 启动nginx

1
2
$ cd /usr/local/nginx/sbin/
$ ./nginx

查询nginx进程:

15098是nginx主进程的进程id,15099是nginx工作进程的进程id

注意:执行./nginx启动nginx,这里可以-c指定加载的nginx配置文件,如下:
./nginx -c /usr/local/nginx/conf/nginx.conf
如果不指定-c,nginx在启动时默认加载conf/nginx.conf文件,此文件的地址也可以在编译安装nginx时指定./configure的参数(–conf-path= 指向配置文件(nginx.conf))

4 停止nginx

  • 方式1,快速停止:
    1
    2
    $ cd /usr/local/nginx/sbin
    $ ./nginx -s stop

此方式相当于先查出nginx进程id再使用kill命令强制杀掉进程。

  • 方式2,完整停止(建议使用):
    1
    2
    $ cd /usr/local/nginx/sbin
    $ ./nginx -s quit

此方式停止步骤是待nginx进程处理任务完毕进行停止。

5 重启nginx

  • 方式1,先停止再启动(建议使用):
    对nginx进行重启相当于先停止nginx再启动nginx,即先执行停止命令再执行启动命令。
    如下:
    1
    2
    ./nginx -s quit
    ./nginx
下面开始开启iptables

二. 启动iptables

1.安装iptable iptable-service

1
2
3
4
5
6
7
8
# 先检查是否安装了iptables
$ service iptables status
# 安装iptables
$ yum install -y iptables
# 升级iptables
$ yum update iptables
# 安装iptables.services
$ yum install iptables.services

2.禁用/停止自带的firewalld服务

1
2
3
4
# 停止firewalld服务
$ systemctl stop firewalld
# 禁用firewalld服务
$ systemctl mask firewalld

安装iptables

1
2
yum search iptables
yum install iptables-services

3.设置规则,开启80端口

开启80端口
-A INPUT -p tcp –dport 80 -j ACCEPT

4.开启iptables服务

1
2
3
4
5
6
7
# 注册iptables服务
# 相当于以前的chkconfig iptables on
$ systemctl enable iptables.service
# 开启服务
$ systemctl start iptables.service
# 查看状态
$ systemctl status iptables.service

相关文章:
CentOS7安装iptables防火墙

使用支付宝打赏
使用微信打赏

若你觉得我的文章对你有帮助,欢迎点击上方按钮对我打赏

扫描二维码,分享此文章