现在很多人都有用到二级域名,我介绍下Linux下lnmp(Linux+nginx+mysql+php)虚拟主机的配置:

1.首先,在你购买域名的地方,解析二级域名,比如万网:

原域名:www.test.com

二级域名1:mtest.test.com
二级域名2:ztest.test.com

(如果是本地测试,请修改host文件,Linux:/etc/hosts;)

2.配置虚拟主机:

网站根目录:/var/www

站点www.test.com 目录  /var/www/test

站点mtest.test.com 目录  /var/www/mytest

站点ztest.test.com 目录  /var/www/ztest

设置目录所有者 www是nginx用户

chown www.www /var/www/ -R

设置目录权限

chmod 777 -R /var/www

修改nginx连接fastcgi方式为unix domain socket

建立php-cgi.sock文件

touch /tmp/php-cgi.sock

设置文件所有者与nginx运行用户一致

chown www.www /tmp/php-cgi.sock

编辑nginx.conf文件

修改 fastcgi_pass 127.0.0.1:9000; 为 fastcgi_pass unix /tmp/php-cgi.sock

编辑php-fpm配置文件

修改:listen = 127.0.0.1:9000; 为 listen = /tmp/php-cgi.sock

添加虚拟主机目录文件

进入/usr/local/nginx/conf 目录下

执行命令 mkdir vhost

在nginx.conf   http部分末尾添加

include vhost/*.conf

nginx配置文件参考如下:

user  www www;

worker_processes 1;

error_log  /var/www/wwwlogs/nginx_error.log  crit;

pid        /usr/local/nginx/logs/nginx.pid;

#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 51200;

events
{
	use epoll;
	worker_connections 51200;
	multi_accept on;
}

http
{
	include       mime.types;
	default_type  application/octet-stream;

	server_names_hash_bucket_size 128;
	client_header_buffer_size 32k;
	large_client_header_buffers 4 32k;
	client_max_body_size 50m;

	sendfile on;
	tcp_nopush     on;

	keepalive_timeout 60;

	tcp_nodelay on;

	fastcgi_connect_timeout 300;
	fastcgi_send_timeout 300;
	fastcgi_read_timeout 300;
	fastcgi_buffer_size 64k;
	fastcgi_buffers 4 64k;
	fastcgi_busy_buffers_size 128k;
	fastcgi_temp_file_write_size 256k;

	gzip on;
	gzip_min_length  1k;
	gzip_buffers     4 16k;
	gzip_http_version 1.0;
	gzip_comp_level 2;
	gzip_types       text/plain application/x-javascript text/css application/xml;
	gzip_vary on;
	gzip_proxied        expired no-cache no-store private auth;
	gzip_disable        "MSIE [1-6]\.";

	#limit_conn_zone $binary_remote_addr zone=perip:10m;
	##If enable limit_conn_zone,add "limit_conn perip 10;" to server section.

	server_tokens off;
	#log format
	log_format  access  '$remote_addr - $remote_user [$time_local] "$request" '
         '$status $body_bytes_sent "$http_referer" '
         '"$http_user_agent" $http_x_forwarded_for';


include vhost/*.conf;
}

在/usr/local/nginx/conf目录下添加(fcgi.conf):

fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;

fastcgi_param  QUERY_STRING       $query_string;
fastcgi_param  REQUEST_METHOD     $request_method;
fastcgi_param  CONTENT_TYPE       $content_type;
fastcgi_param  CONTENT_LENGTH     $content_length;

fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
fastcgi_param  REQUEST_URI        $request_uri;
fastcgi_param  DOCUMENT_URI       $document_uri;
fastcgi_param  DOCUMENT_ROOT      $document_root;
fastcgi_param  SERVER_PROTOCOL    $server_protocol;

fastcgi_param  REMOTE_ADDR        $remote_addr;
fastcgi_param  REMOTE_PORT        $remote_port;
fastcgi_param  SERVER_ADDR        $server_addr;
fastcgi_param  SERVER_PORT        $server_port;
fastcgi_param  SERVER_NAME        $server_name;

#PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param  REDIRECT_STATUS    200;

添加虚拟主机配置文件(在vhost目录下添加文件,命名随意,最好是以网站名称命名方便维护修改):

mtest.test.com配置文件:

server
{
	listen 80;
	#listen [::]:80;
	server_name mtest.test.com;
	index index.html index.htm index.php default.html default.htm default.php;
	root  /var/www/mtest;
	location / {
		try_files $uri $uri/ /index.php?$query_string;
	}

	#error_page   404   /404.html;
	location ~ .*\.(php|php5)?$ {
		fastcgi_pass unix:/tmp/php-cgi.sock;
		fastcgi_index index.php;
		include fcgi.conf;
	}

	location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
		{
			expires      30d;
		}

	location ~ .*\.(js|css)?$
		{
			expires      12h;
		}

	access_log  /var/www/wwwlogs/mtest.test.com.log  access;
}

添加ztest.test.com配置文件:

server
{
	listen 80;
	#listen [::]:80;
	server_name ztest.test.com;
	index index.html index.htm index.php default.html default.htm default.php;
	root  /var/www/ztest;
	location / {
		try_files $uri $uri/ /index.php?$query_string;
	}

	#error_page   404   /404.html;
	location ~ .*\.(php|php5)?$ {
		fastcgi_pass unix:/tmp/php-cgi.sock;
		fastcgi_index index.php;
		include fcgi.conf;
	}

	location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
		{
			expires      30d;
		}

	location ~ .*\.(js|css)?$
		{
			expires      12h;
		}

	access_log  /var/www/wwwlogs/ztest.test.com.log  access;
}