Install and configure Nginx in CentOS 6.5

Source: Internet
Author: User
Tags nginx server

Install and configure Nginx in CentOS 6.5

Install and configure Nginx in CentOS 6.5

I. Preparations

(1) Because nginx needs to access port 80, disable or open the firewall port and selinux first.

Reference command

Disable Firewall:

[Root @ local ~] # Iptables-I INPUT-p tcp -- dport 80-j ACCEPT

[Root @ local ~] # Service iptables save

Disable selinux:

[Root @ local ~] # Setenforce 0

[Root @ local ~] # Vim/etc/selinux/config

Change SELINUX = enforcing to SELINUX = disabled
 

(2) If you use a domain name, build your own DNS service.

Ii. Installation

(1) install pcre, zlib, and other software packages for nginx running.

Pcre = Pcre Compatible Regular Expressions (Chinese pcre Compatible Regular Expression)

For Yum configuration, see:

[Root @ local ~] Yum-y install pcre * zlib * # or compile and install

[Root @ local ~] # Useradd-M-s/sbin/nologin nginx # create an nginx Service
 

Start user

(3) Compile and install nginx,: http://nginx.org/en/download.html installation for the latest stable version of 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 Parameters

[Root @ local nginx-1.8.0] # make

[Root @ local nginx-1.8.0] # make install
 

(4) create a soft connection

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

/Application/nginx
 

(5) Basic use

# Syntax check

[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

# Start the service

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

# Port Check

[Root @ local nginx-1.8.0] # netstat-lnt

# Check the process

[Root @ local nginx-1.8.0] # ps-ef | grep nginx # port information is stored in

/Application/nginx/logs/nginx. pid File

# View processes in use through ports

[Root @ local nginx-1.8.0] # lsof-I: 80

# Error Log

/Application/nginx/logs/error. log
 

3. Compile nginx service scripts

For ease of use, you can use the server to start, disable, enable, and reload the nginx service.

Write nginx service scripts (self-compiled scripts are for reference only !)

[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; 31 m [OK] \ e [0 m "'

No = 'echo-e "\ e [1; 31 m [no] \ e [0 m "'

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 # add as a system service

[Root @ local ~] # Chkconfig nginx on
 

Iv. Simple nginx web site

Nginx's default Site Directory, which is the html under the installation directory. Here is (/application/nginx/html)

Check in the main configuration file/application/nginx/conf/nginx. conf.

You only need to replace index.html in/application/nginx/html /.

Main configuration file description

 

[Root @ local ~] # Egrep-v "# | ^ $"/application/nginx/conf/nginx. conf

Worker_processes 1; # specify the number of processes enabled by Nginx

Events {# Set the Nginx working mode and number of connections online

Worker_connections 1024;

}

Http {

Include mime. types; # Main module command to set all contained files in the configuration file

Default_type application/octet-stream; # belongs to the http core module command.

This method is used when the file type is undefined. For example, PHP is not configured.

Nginx does not parse the environment. When you access the PHP file in a browser, a download window is displayed.

Sendfile on; # Used in efficient File Transfer Mode

Keepalive_timeout 65; set the timeout value for reading the header file of the client request. If the timeout value is exceeded

The Inter-server closes the connection.

Server {# define keywords starting with the VM

Listen 80; # specifies the service port of the VM.

Server_name localhost; used to specify an IP address or domain name. Multiple Domain Names are separated by spaces.

Location /{

Root html;

Index index.html index.htm; # used to set the default homepage for access

}

Error_page 500 502 503 x.html; # Static Page redirection Server Error

Page, such as the page that appears when Ctrip's website crashes

Location =/50x.html {

Root html;

}

}

}

For more Nginx tutorials, see the following:

Deployment of Nginx + MySQL + PHP in CentOS 6.2

Build a WEB server using Nginx

Build a Web server based on Linux6.3 + Nginx1.2 + PHP5 + MySQL5.5

Performance Tuning for Nginx in CentOS 6.3

Configure Nginx to load the ngx_pagespeed module in CentOS 6.3

Install and configure Nginx + Pcre + php-fpm in CentOS 6.4

Nginx installation and configuration instructions

Nginx log filtering using ngx_log_if does not record specific logs

Nginx details: click here
Nginx: click here

This article permanently updates the link address:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.