Gentoo 下搭建 Nginx+ MySQL + PHP (fastcgi) 环境

安装 Nginx

* 一条命令搞定:

USE=fastcgi emerge nginx

* 新建用户和组:

groupadd www

useradd www -g www

Nginx 安装好后默认会添加 nginx 组和 nginx 用户,不过我本身还是习惯新建个 www 组和 www 用户来做 HTTP 服务用户。若今后 HTTP 服务器更换为 apache 或是 lighttpd 时,用户名和用户组可以不变。

安装 MySQL

在装 PHP 前必须先装 MySQL,因为 PHP 里的 MySQL 操作函数需要 MySQL 头文件和库的支持。

emerge dev-db/mysql

* 初始化数据库:

我不习惯把数据库安装在默认路径 /var/lib/mysql 中,我把它放在 /work/db/3306/data 中。

mkdir -p /work/db/3306/data

mysql_install_db –basedir=/usr –datadir=/work/db/3306/data –user=mysql

* 修改配置文件:

vim /etc/mysql/my.cnf

将 datadir 修改为:

datadir = /work/db/3306/data

* 启动 MySQL:

/etc/init.d/mysql start

* 修改 root 密码:

mysqladmin -uroot password hily

* 测试数据库:

mysql -uroot -p


gentoo setup # mysql -uroot -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.0.84-log Gentoo Linux mysql-5.0.84-r1

Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement.



安装 PHP

以 fastcgi 方式来运行 PHP,需要安装 PHP-FPM

目前最后一个需要以 patch 形式安装 PHP-FPM 的 PHP 版本是 5.3.0,PHP 5.3.2 版本中将可能直接集成 PHP-FPM。

这里我就使用 PHP 5.3.0 来安装。

因为 Gentoo 中目录还没有集成 PHP-FPM 的 Portage,所以下面直接通过源码编译形式进行安装。

* 下载 PHP 5.3.0:


* 下载 PHP-FPM 补丁:


* 解压 PHP 并打 FPM 补丁:

tar jxf php-5.3.0.tar.bz2

gzip -cd php-5.3.0-fpm-0.5.12.diff.gz | patch -d php-5.3.0 -p1

* 安装 PHP 需要的库(根据自身需要):

emerge libpng

emerge jpeg

emerge freetype

USE=”png jpeg truetype” emerge gd


USE=”png jpeg truetype” emerge gd

* 配置并编译 PHP(根据自身需要):

cd php-5.3.0

./configure –prefix=/usr/local/php –with-config-file-path=/usr/local/php/etc –with-mysql=/usr –with-mysqli=/usr/bin/mysql_config –enable-fpm –enable-sockets –enable-pdo –with-pdo-mysql=/usr –with-gd –with-jpeg-dir –with-png-dir –with-freetype-dir –with-zlib

make && make install

* PHP 配置文件:

cp php.ini-production /usr/local/php/etc/php.ini

* PHP-FPM 配置文件:

vim /usr/local/php/etc/php-fpm.conf

修改 listen_address 为 socket 地址(socket 比 IP:Port 高效):

<value name=”listen_address”>/tmp/php-fpm.sock</value>


Unix user of processes
<value name=”user”>www</value>

Unix group of processes
<value name=”group”>www</value>

修改 PHP-FPM 运行模式为 Apache-Like 模式:

<value name=”style”>apache-like</value>
<value name=”StartServers”>1</value>
<value name=”MinSpareServers”>1</value>
<value name=”MaxSpareServers”>5</value>

StartServers、MinSpareServers 和 MaxSpareServers 根据实际需要设置,我这里是虚拟机,没必要太大。

* PHP-FPM 启动脚本:

cp /usr/local/php/sbin/php-fpm /etc/init.d/php-fpm

* 启动 PHP-FPM

/etc/init.d/php-fpm start


rc-update add nginx default

rc-update add mysql default

rc-update add php-fpm default

测试 Nginx+PHP

* 添加测试站点目录:

mkdir -p /work/www/test

echo “<?php phpinfo(); ?>” > /work/www/test/index.php

* 添加测试站点的 Nginx 配置:

vim /etc/nginx/nginx.conf

注释掉 server 段,在 http 段尾部加上:

include sites/*.enable;

之后每个站点的配置文件都以一个独立的文件保存在 /etc/nginx/sites 目录下,方便管理和维护。

mkdir /etc/nginx/sites

vim /etc/nginx/test.enable

test.enable 配置如下:

server {
listen       80;
server_name  test.local;

access_log  /work/www/logs/test.access.log  main;
error_log  /work/www/logs/test.error.log;

location / {
root   /work/www/test;
index  index.html index.htm index.php;

location ~ \.php$ {
root           /work/www/test;
fastcgi_index  index.php;
fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
include        fastcgi_params;
fastcgi_pass   unix:/tmp/php-fpm.sock;

* 新建存储日志目录:

mkdir /work/www/logs

* 本地 hosts 中添加记录:    test.local 是我这台 Gentoo 机器的 IP。

* 重新加载 Nginx 配置

/etc/init.d/nginx reload

* 访问:


显示正常的 phpinfo 信息,则安装完成!

— EOF —

《Gentoo 下搭建 Nginx+ MySQL + PHP (fastcgi) 环境》有20个想法

  1. I do like the manner in which you have presented this specific matter and it really does give me personally some fodder for consideration. However, coming from what I have experienced, I just simply wish as the actual remarks pack on that men and women stay on point and in no way get started upon a soap box associated with some other news du jour. Still, thank you for this outstanding piece and though I can not concur with it in totality, I respect your point of view.

  2. Thank you for the sensible critique. Me & my neighbor were just preparing to do a little research on this. We got a grab a book from our local library but I think I learned more from this post. I’m very glad to see such wonderful information being shared freely out there.

  3. As I website possessor I believe the content matter here is rattling magnificent , appreciate it for your hard work. You should keep it up forever! Best of luck.

  4. I’m just writing to make you be aware of of the nice discovery my wife’s princess had studying your web page. She came to find so many things, most notably what it’s like to possess a very effective coaching mindset to let the rest effortlessly gain knowledge of specified extremely tough matters. You actually did more than visitors’ desires. Many thanks for churning out those priceless, trusted, informative not to mention unique thoughts on the topic to Janet.

  5. Satisfy clothier as well as her completely new hubby Nike jordans, which committed around the sixth connected with Sept in 2010 within an outside the house service, as well as drinks in addition to video games and a nighttime underneath new Hugo boss. I just now adore exactly how with nearly all photo Jess can be often giggling and also happy fit to be able to broke, or maybe wiping content crying away- it’s and so splendid!

  6. cambogia garcinia and colon cleanse forskolin at gnc green coffee supreme acai anti frizz conditioner mangosteen benefit health hoodia gordonii bioptimax 24-7 psyllium and bentonite chitosan at walmart difference between guar gum and xanthan gum lipozene active ingredient glucomannan and garcinia cambogia

  7. slimming garcinia and slimming cleanse scottsdale az forskolin 125 milligrams dietary supplement pure garcinia & absolute green coffee cleanse nekter acai bowl nutritional value acai lose weight hoodia gordonii culturemap tastemaker metamucil en francais chitosan journal times guar gum KST hydroxycut in walmart is resveratrol in grape juice


电子邮件地址不会被公开。 必填项已用*标注