Nginx multi-site independent virtual host configuration in centos

Source: Internet
Author: User
When multiple php sites are configured on one server, we hope that each site can be stopped separately and the php running permissions between different sites are isolated from each other and cannot be browsed across directories. this article takes CentOS6.5 compilation and installation of Nginx1.70 + PHP5.59 + MySQL5.6.16 as a prerequisite. Suppose there are two sites: 1.blog.1000seo.com? Directory: webda

When multiple php sites are configured on one server, we hope that each site can be stopped/started separately, and the php running permissions between sites are isolated from each other and cannot be browsed across directories. this article takes CentOS6.5 compilation and installation of Nginx1.70 + PHP5.59 + MySQL5.6.16 as a prerequisite. Suppose there are two sites: 1.blog.1000seo.com? Directory:/webda

When multiple php sites are configured on one server, we hope that each site can be stopped/started separately, and the php running permissions between sites are isolated from each other and cannot be browsed across directories. this article takes "CentOS6.5 compilation and installation of Nginx1.70 + PHP5.59 + MySQL5.6.16" as a prerequisite.
Assume that the following two sites exist:
1.blog.1000seo.com? Directory:/webdata/blog.1000seo.com
2.crm.1000seo.com ?? Directory:/webdata/crm.1000seo.com
Procedure
1. Create php-fpm.pid files for each site, in order to be clear and easy to identify, we use domain names as part of the file name
> Cd/usr/local/php5/var/run
> Touch php-fpm-blog.1000seo.com.pid
> Touch php-fpm-crm.1000seo.com.pid
2. Create a php-fpm.conf file for each site.
> Cd/usr/local/php5/etc/
> Cp php-fpm.conf? Php-fpm-blog.1000seo.com.conf
> Cp php-fpm.conf? Php-fpm-crm.1000seo.com.conf
3. Create a php-cgi.sock file for each site.
> Touch/tmp/php-cgi-blog.1000seo.com.sock
> Chown webuser. webuser/tmp/php-cgi-blog.1000seo.com.sock
> Touch/tmp/php-cgi-crm.1000seo.com.sock
> Chown webuser. webuser/tmp/php-cgi-crm.1000seo.com.sock
4. Edit Related Files
> Vi? /Usr/local/php5/etc/php-fpm-blog.1000seo.com.conf
Find this line pid = run/php-fpm.pid changed to: pid = run/php-fpm-blog.1000seo.com.pid
Find this row listen = 127.0.0.1: 9000; changed to: listen =/tmp/php-cgi-blog.1000seo.com.sock
: Wq # Save and exit

> Vi? /Usr/local/php5/etc/php-fpm-crm.1000seo.com.conf
Find this line pid = run/php-fpm.pid changed to: pid = run/php-fpm-crm.1000seo.com.pid
Find this row listen = 127.0.0.1: 9000; changed to: listen =/tmp/php-cgi-crm.1000seo.com.sock
: Wq # Save and exit

> Vi/etc/rc. d/init. d/php-fpm # modify the settings according to the following content:
#! /Bin/sh

### BEGIN INIT INFO
# Provides :????????? Php-fpm
# Required-Start :??? $ Remote_fs $ network
# Required-Stop :???? $ Remote_fs $ network
# Default-Start :???? 2 3 4 5
# Default-Stop :????? 0 1 6
# Short-Description: starts php-fpm
# Description :?????? Starts the PHP FastCGI Process Manager daemon
### END INIT INFO
Vhost = $2
Prefix =/usr/local/php5
Exec_prefix =$ {prefix}

Php_fpm_BIN =$ {exec_prefix}/sbin/php-fpm
Php_fpm_CONF =$ {prefix}/etc/php-fpm-$ vhost. conf
Php_fpm_PID =$ {prefix}/var/run/php-fpm-$ vhost. pid

Php_opts = "-d open_basedir =/webdata/$ vhost/:/tmp/-fpm-config $ php_fpm_CONF-pid $ php_fpm_PID"

Wait_for_pid (){
Try = 0

While test $ try-lt 35; do

Case "$1" in
'Created ')
If [-f "$2"]; then
Try ="
Break
Fi
;;

'Removed ')
If [! -F "$2"]; then
Try ="
Break
Fi
;;
Esac

Echo-n.
Try = 'expr $ try + 1'
Sleep 1

Done

}

Case "$1" in
Start)
Echo-n "Starting php-fpm"

$ Php_fpm_BIN-daemonize $ php_opts

If ["$ ?" ! = 0]; then
Echo "failed"
Exit 1
Fi

Wait_for_pid created $ php_fpm_PID

If [-n "$ try"]; then
Echo "failed"
Exit 1
Else
Echo "done"
Fi
;;

Stop)
Echo-n "Gracefully shutting down php-fpm"

If [! -R $ php_fpm_PID]; then
Echo "warning, no pid file found-php-fpm is not running ?"
Exit 1
Fi

Kill-QUIT 'cat $ php_fpm_PID'

Wait_for_pid removed $ php_fpm_PID

If [-n "$ try"]; then
Echo "failed. Use force-quit"
Exit 1
Else
Echo "done"
Fi
;;

Status)
If [! -R $ php_fpm_PID]; then
Echo "php-fpm is stopped"
Exit 0
Fi

PID = 'cat $ php_fpm_PID'
If ps-p $ PID | grep-q $ PID; then
Echo "php-fpm (pid $ PID) is running ..."
Else
Echo "php-fpm dead but pid file exists"
Fi
;;

Force-quit)
Echo-n "Terminating php-fpm"

If [! -R $ php_fpm_PID]; then
Echo "warning, no pid file found-php-fpm is not running ?"
Exit 1
Fi

Kill-TERM 'cat $ php_fpm_PID'

Wait_for_pid removed $ php_fpm_PID

If [-n "$ try"]; then
Echo "failed"
Exit 1
Else
Echo "done"
Fi
;;

Restart)
$0 stop
$0 start
;;

Reload)

Echo-n "Reload service php-fpm"

If [! -R $ php_fpm_PID]; then
Echo "warning, no pid file found-php-fpm is not running ?"
Exit 1
Fi

Kill-USR2 'cat $ php_fpm_PID'

Echo "done"
;;

*)
Echo "Usage: $0 {start | stop | force-quit | restart | reload | status }"
Exit 1
;;

Esac
: Wq # Save and exit
> Vi/usr/local/nginx/conf/vhost/blog.1000seo.com. conf
Server
{
Listen ?????? 80;
Server_name blog.1000seo.com;
Index. php index.html index.htm default.html default.htm default. php;
Root? /Webdata/blog.1000seo.com;
Location ~ . * \. (Php | php5 )? $
{
Fastcgi_pass? Unix:/tmp/php-cgi-blog.1000seo.com.sock;
Fastcgi_index index. php;
Fcinclude gi. conf;
}
Location/status {
Stub_status on;
Access_log ?? Off;
}

Location ~ . * \. (Gif | jpg | jpeg | png | bmp | swf) $
{
Expires ????? 30d;
}
Location ~ . * \. (Js | css )? $
{
Expires ????? 12 h;
}

Access_log off;
}

: Wq # Save and exit

> Vi/usr/local/nginx/conf/vhost/crm.1000seo.com. conf
Server
{
Listen ?????? 80;
Server_name crm.1000seo.com;
Index. php index.html index.htm default.html default.htm default. php;
Root? /Webdata/crm.1000seo.com;
Location ~ . * \. (Php | php5 )? $
{
Fastcgi_pass? Unix:/tmp/php-cgi-crm.1000seo.com.sock;
Fastcgi_index index. php;
Fcinclude gi. conf;
}
Location/status {
Stub_status on;
Access_log ?? Off;
}

Location ~ . * \. (Gif | jpg | jpeg | png | bmp | swf) $
{
Expires ????? 30d;
}
Location ~ . * \. (Js | css )? $
{
Expires ????? 12 h;
}

Access_log off;
}
: Wq # Save and exit

> Vi/usr/local/nginx/conf/nginx. conf # Please refer to the following content to modify

User? Webuser;
Worker_processes? 1;

# Error_log? Logs/error. log;
# Error_log? Logs/error. log? Notice;
# Error_log? Logs/error. log? Info;

# Pid ??????? Logs/nginx. pid;

Events {
Use epoll;
Worker_connections? 1024;
}

Http {
Include ?????? Mime. types;
Default_type? Application/octet-stream;

# Log_format? Main? '$ Remote_addr-$ remote_user [$ time_local] "$ request "'
#????????????????? '$ Status $ body_bytes_sent' $ http_referer "'
#????????????????? '"$ Http_user_agent" "$ http_x_forwarded_for "';

# Access_log? Logs/access. log? Main;
Server_names_hash_bucket_size 128;
Client_header_buffer_size 32 k;
Large_client_header_buffers 4 32 k;
Client_max_body_size 300 m;
Sendfile ??????? On;
Tcp_nopush ???? On;
Fastcgi_connect_timeout 300;
Fastcgi_send_timeout 300;
Fastcgi_read_timeout 300;
Fastcgi_buffer_size 64 k;
Fastcgi_buffers 4 64 k;
Fastcgi_busy_buffers_size 128 k;
Fastcgi_temp_file_write_size 128 k;
# Keepalive_timeout? 0;
Keepalive_timeout? 65;
Tcp_nodelay on;
Server_tokens off;
Gzip? On;
Gzip_min_length? 1 k;
Gzip_buffers ???? 4 16 k;
Gzip_http_version 1.1;
Gzip_comp_level 2;
Gzip_types ?????? Text/plain application/x-javascript text/css application/xml;
Gzip_vary on;

Server {
Listen ?????? 80 default;
Server_name? _;

# Charset koi8-r;

# Access_log? Logs/host. access. log? Main;

Location /{
Root ?? Html;
Return 404;
}

# Error_page? 404 ????????????? /404.html;

# Redirect server error pages to the static page/50x.html
#
Error_page ?? 500 502 503 504? /50x.html;
# Location =/50x.html {
#??? Root ?? Html;
#}

# Proxy the PHP scripts to Apache listening on 127.0.0.1: 80
#
# Location ~ \. Php $ {
#??? Proxy_pass ?? Http: // 127.0.0.1;
#}

# Pass the PHP scripts to FastCGI server listening on Fig: 9000
#
# Location ~ \. Php $ {
#??? Root ?????????? Html;
#??? Fastcgi_pass ?? 127.0.0.1: 9000;
#??? Fastcgi_index? Index. php;
#??? Fastcgi_param? SCRIPT_FILENAME? $ Document_root $ fastcgi_script_name;
#??? Include ??????? Fastcgi_params;
#}

# Deny access to. htaccess files, if Apache's document root
# Concurs with nginx's one
#
Location ~ /\. Ht {
Deny? All;
}
}
Server {
Listen ?? ? 80;
Index ?? ? Index.html index.htm index. php;
Location/status {
Stub_status ?? ? On;
Access_log ?? ? Off;
}
Location ~ . * \. (Gif | jpg | png | bmp | swf) $ {
Expires ?? ? 30d;
}
Location ~. C * \. (js | css )? $ {
Expires ?? ? 12 h;
}
Access_log ?? ? Off;
}
Include vhost/*. conf;

# Another virtual host using mix of IP-, name-, and port-based configuration
#
# Server {
#??? Listen ?????? 8000;
#??? Listen ?????? Somename: 8080;
#??? Server_name? Somename? Alias? Another. alias;

#??? Location /{
#??????? Root ?? Html;
#??????? Index? Index.html index.htm;
#??? }
#}

# HTTPS server
#
# Server {
#??? Listen ?????? 443 ssl;
#??? Server_name? Localhost;

#??? Ssl_certificate ????? Cert. pem;
#??? Ssl_certificate_key? Cert. key;

#??? Ssl_session_cache ??? Shared: SSL: 1 m;
#??? Ssl_session_timeout? 5 m;

#??? Ssl_ciphers? HIGH :! ANULL :! MD5;
#??? Ssl_prefer_server_ciphers? On;

#??? Location /{
#??????? Root ?? Html;
#??????? Index? Index.html index.htm;
#??? }
#}

}
: Wq # Save and exit

> Cd/home
> Vi start. sh
#! /Bin/bash
Actop = $1
/Bin/bash/etc/rc. d/init. d/php-fpm $ actop blog.1000seo.com
/Bin/bash/etc/rc. d/init. d/php-fpm $ actop crm.1000seo.com
: Wq # Save and exit
> Chmod + x start. sh

> Vi/etc/rc. local
#! /Bin/sh
#
# This script will be executed * after * all the other init scripts.
# You can put your own initialization stuff in here if you don't
# Want to do the full Sys V style init stuff.

Touch/var/lock/subsys/local
Sh/home/start. sh start
: Wq # Save and exit
>/Home/start. sh start
> Service nginx restart
Related operation commands
Start all sites
/Home/start. sh start
Stop all sites
/Home/start. sh stop
Start site
>/Etc/rc. d/init. d/php-fpm start blog.1000seo.com
>/Etc/rc. d/init. d/php-fpm start crm.1000seo.com
Stop site
>/Etc/rc. d/init. d/php-fpm stop blog.1000seo.com
>/Etc/rc. d/init. d/php-fpm stop crm.1000seo.com

Original article address: nginx multi-site independent virtual host configuration in centos, thanks to the original author for sharing.

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.