阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

CentOS 6.5安装及简单配置Nginx

133次阅读
没有评论

共计 4149 个字符,预计需要花费 11 分钟才能阅读完成。

CentOS 6.5 安装及简单配置 Nginx

一、准备事项

(1)因为 nginx 需要访问 80 端口所以请先关闭或者开放防火墙端口,和 selinux。

参考命令

关闭防火墙:

[root@local ~]# iptables -I INPUT -p tcp –dport 80 -j ACCEPT

[root@local ~]# service iptables save

关闭 selinux:

[root@local ~]# setenforce 0

[root@local ~]# vim /etc/selinux/config

将 SELINUX=enforcing 改为 SELINUX=disabled
 

(2)如果用到域名请自行构建 DNS 服务

二、安装

(1)因为 nginx 的运行需要安装 pcre、zlib 等软件包,因此我们进行安装

Pcre=Pcre Compatible Regular Expressions(中文 pcre 兼容正则表达式)

Yum 配置请参考:http://www.linuxidc.com/Linux/2015-11/125332.htm

[root@local ~] yum -y install pcre* zlib* #或者进行编译安装

[root@local ~]# useradd -M -s /sbin/nologin nginx #创建 nginx 服务
 

启动用户

(3)编译安装 nginx,下载地址:http://nginx.org/en/download.html 此次安装为最新稳定版 nginx-1.8.0

[root@local ~]# tar zxf nginx-1.8.0.tar.gz

[root@local ~]# cd nginx-1.8.0

[root@local nginx-1.8.0]# ls

auto CHANGES.ru configure html Makefile objs src

CHANGES conf contrib LICENSE man README

[root@local nginx-1.8.0]# ./configure –user=nginx –group=nginx –prefix=/application/nginx-1.8.0 –with-http_stub_status_module –with-http_ssl_module #./configure –help 参数详解

[root@local nginx-1.8.0]# make

[root@local nginx-1.8.0]# make install
 

(4)制作软连接

[root@local nginx-1.8.0]#ln –a /application/nginx-1.8.0/

/application/nginx
 

(5)基本使用

# 语法检查

[root@local nginx-1.8.0]# /application/nginx/sbin/nginx –t

nginx: the configuration file /application/nginx-1.8.0/conf/nginx.conf syntax is ok

nginx: configuration file /application/nginx-1.8.0/conf/nginx.conf test is successful

# 启动服务

[root@local nginx-1.8.0]# /application/nginx/sbin/nginx

# 端口检查

[root@local nginx-1.8.0]# netstat –lnt

# 检查进程

[root@local nginx-1.8.0]# ps -ef | grep nginx #端口信息保存在

/application/nginx/logs/ nginx.pid 文件中

# 通过端口查看占用进程

[root@local nginx-1.8.0]# lsof -i :80

# 错误日志

/application/nginx/logs/error.log
 

三、编写 nginx 服务脚本

为了方便使用习惯,通过 server 来启动、关闭、开启、重载 nginx 服务所以我们来编

写 nginx 的服务脚本(自己编写的脚本仅供参考!)

[root@local ~]# vim /etc/init.d/nginx

#!/bin/bash

#chkconfig: – 99 20

#description:Nginx Server Contorl Script

PROG=”/application/nginx/sbin/nginx”

PIDF=”/application/nginx/logs/nginx.pid”

ok=`echo -e “\e[1;31m [ok] \e[0m”`

no=`echo -e “\e[1;31m [no] \e[0m”`

detection=`/application/nginx/sbin/nginx -t 2>&1`

screen_1=`echo $detection | awk ‘{print $6,$7,$8}’`

screen_2=`echo $detection | awk ‘{print $13,$14,$15}’`

if [“$screen_1” = “syntax is ok”] && [“$screen_2” = “test is successful”];

then

    case “$1” in

    start)

      $PROG

      echo “Nginx Is starting state $ok”

      ;;

    stop)

      kill -s QUIT $(cat $PIDF)

      echo “Nginx Is closing state $ok”

      ;;

    restart)

      $0 stop

      $0 start

      echo “Nginx Is to restart state $ok”

      ;;

    reload)

      kill -s HUP $(cat $PIDF)

      echo “Nginx Is overloaded state $ok”

      ;;

    *)

      echo “Usage: $0 (start|stop|restart|reload)”

      exit 1

      esac

else

  echo “Nginx check state $no “

  echo “Please check the configuration file”

  echo “$detection”

fi

exit 0
 
 
[root@local ~]# chmod +x /etc/init.d/nginx

[root@local ~]# chkconfig –add nginx #添加为系统服务

[root@local ~]# chkconfig nginx on
 

四、简单的 nginx web 站点

Nginx 的默认站点目录,是安装目录下的 html 这里是(/application/nginx/html)

在主配置文件 /application/nginx/conf/nginx.conf 中查看,对于重新部署 web 页面

只需将 /application/nginx/html/ 中的 index.html 替换即可

主配置文件讲解

 

[root@local ~]# egrep -v “#|^$” /application/nginx/conf/nginx.conf

worker_processes 1; #指定 Nginx 开启的进程数

events {#设定 Nginx 的工作模式及连接数上线

worker_connections 1024;

}

http {

include mime.types; #主模块命令,实现对配置文件所有包含文件的设置

default_type application/octet-stream; #属于 http 核心模块命令,这里设

置类型为二进制流,也就是当文件类型未定义时使用这种方式,例如,没有配置 PHP

环境时,nginx 是不给予解析的,此时,用浏览器访问 PHP 文件就会出现下载窗口。

sendfile on; #用于高效文件传输模式

keepalive_timeout 65; 设置客户端请求头文件读取超时时间,如果超过这个时

间服务器会关闭该连接。

server {#定义虚拟主机开始的关键字

listen 80; #用于指定虚拟主机的服务端口

server_name localhost; 用于指定 ip 地址或者域名,多个域名用空格隔开

location / {

root html;

index index.html index.htm; #用于设定访问的默认首页

}

error_page 500 502 503 504 /50x.html;# 静态页面重定向服务器错误

页面,例如携程的网站崩溃出现的页面

location = /50x.html {

root html;

}

}

}

更多 Nginx 相关教程见以下内容

CentOS 6.2 实战部署 Nginx+MySQL+PHP http://www.linuxidc.com/Linux/2013-09/90020.htm

使用 Nginx 搭建 WEB 服务器 http://www.linuxidc.com/Linux/2013-09/89768.htm

搭建基于 Linux6.3+Nginx1.2+PHP5+MySQL5.5 的 Web 服务器全过程 http://www.linuxidc.com/Linux/2013-09/89692.htm

CentOS 6.3 下 Nginx 性能调优 http://www.linuxidc.com/Linux/2013-09/89656.htm

CentOS 6.3 下配置 Nginx 加载 ngx_pagespeed 模块 http://www.linuxidc.com/Linux/2013-09/89657.htm

CentOS 6.4 安装配置 Nginx+Pcre+php-fpm http://www.linuxidc.com/Linux/2013-08/88984.htm

Nginx 安装配置使用详细笔记 http://www.linuxidc.com/Linux/2014-07/104499.htm

Nginx 日志过滤 使用 ngx_log_if 不记录特定日志 http://www.linuxidc.com/Linux/2014-07/104686.htm

Nginx 的详细介绍:请点这里
Nginx 的下载地址:请点这里

本文永久更新链接地址:http://www.linuxidc.com/Linux/2015-12/125870.htm

正文完
星哥说事-微信公众号
post-qrcode
 
星锅
版权声明:本站原创文章,由 星锅 2022-01-21发表,共计4149字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中