转载

使用 Nginx 搭建站点

为什要使用nginx?因为apache太耗内存了,使用nginx可以节约内存。 采用 nginx + php-fpm + mysql 搭建。

 删除 apache2

使用putty登录vps,执行如下命令: 输入如下命令关闭 apache2
/etc/init.d/apache2 stop
如果输入正确,会提示如下信息,表示apache2已经停止
root@241541:~# /etc/init.d/apache2 stop
 * Stopping web server apache2                                                  apache2: Could not reliably determine the server's fully qualified domain name, using 0.3.175.133 for ServerName
 ... waiting                                                             [ OK ]
输入如下命令删除 apache2
apt-get remove apache2 apache2.2-common
出现类似如下提示时,输入 Y 并回车。
0 upgraded, 1 newly installed, 7 to remove and 26 not upgraded.
Need to get 5837kB of archives.
After this operation, 14.5MB disk space will be freed.
Do you want to continue [Y/n]?
最后会出现如下提示,表示已经删除 apache2
Removing libapache2-mod-php5 ...
Module php5 disabled.
Run '/etc/init.d/apache2 restart' to activate new configuration!
Removing apache2-mpm-prefork ...
 * Stopping web server apache2                                                  apache2: Could not reliably determine the  server's fully qualified domain name, using 0.3.175.133 for ServerName
 ... waiting                                                             [ OK ]
Removing apache2.2-common ...
Removing apache2-utils ...
Removing apache2.2-bin ...

安装 php5-fpm php5-cgi

由于 Ubuntu 10.04 没有 php5-fpm 软件,需要增加第三方源,所以请先检查下 Ubuntu 的版本,使用putty登录vps执行如下命令来检查:
cat /etc/issue
如果看到,执行结果如下所示:
root@195669:~# cat /etc/issue
Ubuntu 10.04 LTS \n \l
则表示你当前的版本的 10.04 ,需要执行如下步骤,增加第三方源,否则略过。
apt-get -y install python-software-properties
add-apt-repository ppa:nginx/stable
add-apt-repository ppa:brianmercer/php
apt-get -y update
和前面一样,如果碰到 “Do you want to continue [Y/n]? ” 的提示,请输入 Y 回车继续。 Ubuntu 10.10 或 执行完毕上述命令的 Ubuntu 10.04 系统可以开始安装 php5-fpm 了,执行如下命令:
apt-get install php5-cgi php5-fpm php5-gd php5-mysql php5-curl php5-mcrypt php5-common php-apc
出现下面的提示表示安装成功
root@241541:~# apt-get install php5-cgi php5-fpm php5-gd php5-mysql php5-mcrypt php5-common php-apc
Reading package lists... Done
Building dependency tree
Reading state information... Done
php5-cgi is already the newest version.
php5-cgi set to manually installed.
Suggested packages:
  php-pear
The following NEW packages will be installed:
  libevent-1.4-2 php5-fpm
0 upgraded, 2 newly installed, 0 to remove and 26 not upgraded.
Need to get 3002kB of archives.
After this operation, 7967kB of additional disk space will be used.
Get:1 http://archive.ubuntu.com/ubuntu/ maverick/main libevent-1.4-2 i386 1.4.13-stable-1 [56.2kB]
Get:2 http://archive.ubuntu.com/ubuntu/ maverick-updates/universe php5-fpm i386 5.3.3-1ubuntu9.5 [2946kB]
Fetched 3002kB in 1s (2181kB/s)
Selecting previously deselected package libevent-1.4-2.
(Reading database ... 21902 files and directories currently installed.)
Unpacking libevent-1.4-2 (from .../libevent-1.4-2_1.4.13-stable-1_i386.deb) ...
Selecting previously deselected package php5-fpm.
Unpacking php5-fpm (from .../php5-fpm_5.3.3-1ubuntu9.5_i386.deb) ...
Setting up libevent-1.4-2 (1.4.13-stable-1) ...
Setting up php5-fpm (5.3.3-1ubuntu9.5) ...
update-rc.d: warning: php5-fpm stop runlevel arguments (0 1 6) do not match LSB Default-Stop values (none)
 * Starting PHP5 FPM...                                                         Aug 13 06:25:50.352643 [WARNING] [pool  www] pm.start_servers is not set. It's been set to 20.
                                                                        [ OK ]

Creating config file /etc/php5/fpm/php.ini with new version
Processing triggers for libc-bin ...
ldconfig deferred processing now taking place
安装完毕后,重启下 php5-fpm,执行如下命令重启
/etc/init.d/php5-fpm restart
出现如下提示,表示重启成功
root@241541:~# /etc/init.d/php5-fpm restart
* Stopping PHP5 FPM...                                                  [ OK ]
* Starting PHP5 FPM...                                                  [ OK ]
Jun 25 19:43:58.592184 [WARNING] [pool www] pm.start_servers is not set. It's been set to 20.

 安装 mysql

见前面的安装 mysql 的步骤。

 安装 nginx

使用putty登录vps,执行如下命令:
apt-get install nginx
出现如下提示,表示已经安装完毕:
root@241541:~# apt-get install nginx
Reading package lists... Done
Building dependency tree
Reading state information... Done
Suggested packages:
  ufw
The following NEW packages will be installed:
  nginx
0 upgraded, 1 newly installed, 0 to remove and 26 not upgraded.
Need to get 341kB of archives.
After this operation, 827kB of additional disk space will be used.
Get:1 http://archive.ubuntu.com/ubuntu/ maverick/universe nginx i386 0.7.67-3ubuntu1 [341kB]
Fetched 341kB in 0s (437kB/s)
Selecting previously deselected package nginx.
(Reading database ... 21918 files and directories currently installed.)
Unpacking nginx (from .../nginx_0.7.67-3ubuntu1_i386.deb) ...
Setting up nginx (0.7.67-3ubuntu1) ...
再执行下面的命令启动nginx服务:
/etc/init.d/nginx start
结果如下所示,表示nginx顺利启动:
root@241541:~# /etc/init.d/nginx start
Starting nginx: nginx.

 测试站点

由于nginx默认的站点不支持php,所以需要修改默认站点。 编辑文件名为 default.txt ,注意下面的 server_name 修改为你的vps的ip地址,可以写一个IP,也可以都写上,IP和IP之间有空格。 配置文件中的 root 就是站点的目录了,可以更改为你的站点的路径。
server	{
	listen       80;
	server_name 184.82.9.30 184.82.9.31;
	index index.html index.htm index.php;
	root  /var/www/;
	location ~ \.php$ {
		include /etc/nginx/fastcgi_params;
		fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
		fastcgi_pass  127.0.0.1:9000;
		}
	}
将 default.txt 文件使用 filezilla 上传到 /etc/nginx/sites-enabled 目录下 使用 putty 登录vps,输入如下命令重启 nginx 服务
/etc/init.d/nginx restart
重启结果如下所示:
root@241541:~# /etc/init.d/nginx restart
Restarting nginx: nginx.

安装phpsysinfo

按照前面apache2讲述的安装

安装 php 探针

按照前面apache2讲述的安装

 安装 phpmyadmin

照前面 Apache2 安装 phpmyadmin, 注意第二步不要再按空格选择 apache2 , 直接回车下一步。 如果已经之前已经安装过 phpmyadmin ,不需要删除,继续使用。 安装完毕 phpmyadmin 后, 继续在 putty 里面执行如下命令即可:
cd /var/www
ln -s /usr/share/phpmyadmin .
执行情况如下所示:
root@241541:~# cd /var/www
root@241541:/var/www# ln -s /usr/share/phpmyadmin .
root@241541:/var/www#
然后就可以在浏览器里面访问 phpmyadmin 了。 访问地址: http://184.82.9.30/phpmyadmin/

 配置虚拟站点

以上运行环境均已经安装完毕,下面讲如何配置 nginx 的多域名。 假设前提: 你有一个域名 test.com ,你希望搭建两个站点,第一个站点是 www.test.com 和 test.com 为主页,同时希望提供 bbs.test.com 为论坛。 下面的文档对多个不同的域名也是适用的。

 配置域名

参考上面 apache2 的配置域名部分。

 上传程序

参考上面 apache2 的上传程序部分。

 多域名配置

这里推荐编辑器直接使用记事本,不要使用其他的高级编辑器。 编辑www.test.com的站点配置文件 test.com.txt
server	{
	listen       80;
	server_name www.test.com test.com;
	index index.html index.htm index.php;
	root  /var/www/test/;
	location ~ \.php$ {
		include /etc/nginx/fastcgi_params;
		fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
		fastcgi_pass  127.0.0.1:9000;
		}
	}
编辑bbs.test.com的站点配置文件 bbs.test.com.txt
server	{
	listen       80;
	server_name bbs.test.com;
	index index.html index.htm index.php;
	root  /var/www/bbs/;
	location ~ \.php$ {
		include /etc/nginx/fastcgi_params;
		fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
		fastcgi_pass  127.0.0.1:9000;
		}
	}
将这两个文件使用 filezilla 上传到 /etc/nginx/sites-enabled/ 目录
正文到此结束
该篇文章的评论功能已被站长关闭
Loading...