Centos上安装nginx
近期因为项目原因须要使用nginx,所以看了一下。先从安装和配置開始。
(1) 安装依赖
依赖库直接使用yum安装a) 安装基本依赖工具[root@localhost ~]# yum -y install gcc gcc-c++ automake autoconf libtool makeb) 安装prec
[root@localhost ~]# yum install pcre.x86_64 pcre-devel.x86_64c) 安装zlib
[root@localhost ~]# yum install zlib.x86_64 zlib-devel.x86_64d) 安装openssl
[root@localhost ~]# yum install openssl.x86_64 openssl-devel.x86_64
(2) 编译安装nginx
a) 下载安装包[root@localhost ~]# wget http://nginx.org/download/nginx-1.4.7.tar.gzb) 解压
[root@localhost ~]# tar -xvf nginx-1.4.7.tar.gz [root@localhost ~]# cd nginx-1.4.7c) 安装i. 指定安装文件夹
[root@localhost ~]# ./configure --prefix=/usr/local/nginx/cache/ii. make & make install
[root@localhost ~]# make [root@localhost ~]# make install
(3) 配置和启动
a) 配置文件使用默认的配置文件在安装文件夹下:conf/nginx.conf。默认使用80port。须要先使用netstat开一下80port是否已经被占用[root@localhost ~]# netstat –nltp | grep 80b) root启动nginx,
[root@localhost ~]# /usr/local/nginx/cache/sbin/nginx -c /usr/local/nginx/cache/conf/nginx.conf
(4) 改动防火墙规则
Centos的防火墙默认是打开的。须要加入对应的规则打开80port。a) 在另外一台机器上測试port,发现80port不通[root@localhost ~]# telnet 10.237.92.30 80 Trying 10.237.92.30... telnet: Unable to connect to remote host: No route to hostb) Centos的防火墙默认是打开的,查看本机防火墙配置
[root@localhost ~]# service iptables status Table: filter Chain INPUT (policy ACCEPT) num target prot opt source destination 1 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED 2 ACCEPT icmp -- 0.0.0.0/0 0.0.0.0/0 3 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 4 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:22 5 REJECT all -- 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited Chain FORWARD (policy ACCEPT) num target prot opt source destination 1 REJECT all -- 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited Chain OUTPUT (policy ACCEPT) num target prot opt source destinationc) 打开80port
[root@localhost ~]# iptables -I INPUT -p tcp --dport 80 -j ACCEPT查看port会发现port通了
guojun1@guojun1-OptiPlex-9020:~$ telnet 10.237.92.30 80 Trying 10.237.92.30... Connected to 10.237.92.30. Escape character is '^]'. ^]d) 改动iptables配置文件使用iptables命令添加的规则在重新启动之后就失效了,要想规则在重新启动之后任然有效, 须要改动iptables配置文件/etc/sysconfig/iptables,添加以下的行。
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT