百分百源码网-让建站变得如此简单! 登录 注册 签到领金币!

主页 | 如何升级VIP | TAG标签

当前位置: 主页>网站教程>服务器> Debian编译安装Nginx+MySQL+PHP-FPM环境详解
分享文章到:

Debian编译安装Nginx+MySQL+PHP-FPM环境详解

发布时间:01/15 来源: 浏览: 关键词:
Debian是linux内核环境了,但使用的用户是比较少的,下面我们来看看Debian编译安装Nginx+MySQL+PHP-FPM环境详解,希望文章能够对各位有帮助。


Linux环境:Debian 6 Stable

程序版本选择:

Nginx:Tengine 1.2.4
淘宝出品,基于Nginx并引进了一些新特性

MySQL:5.1.66
5.1系列最新版。不用5.5的原因是5.5开始编译使用CMake,而资料比较少。(事实上wangyan的脚本使用的是5.5,但是当时我还没看到……)

PHP:5.4.9
前几天刚出的Current Stable,自带FPM,与Nginx配合良好。

参考资料:

主要参考军哥的lnmp脚本和wangyan的lanmp脚本。军哥的脚本用的非常广而且口碑很好,但是软件版本都较旧。wangyan的lanmp脚本坚持使用各软件的最新版本(PHP直接是从网页上抓current stable版本号然后下载),而且代码结构非常赞,阅读起来很清晰。

Nginx(Tengine)

1.安装前准备

首先是安装需要的包:

apt-get install build-essential openssl libssl0.9 libssl-dev

其实因为是回顾所以具体有哪些需要的包我也记不清了……基本上就是这些。其实不用担心少了会怎样,做configure的时候会有提示的,缺什么装什么就可以了。

然后准备用户和目录:


groupadd www
useradd -M -s /usr/sbin/nologin -g www www

mkdir -p /home/wwwroot
chmod +w /home/wwwroot
mkdir -p /home/wwwlogs
chmod 777 /home/wwwlogs

2.编译安装pcre


wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.21.tar.gz
tar zxf pcre-8.21.tar.gz
cd pcre-8.21
./configure
make && make install

3.编译安装Tengine


wget http://tengine.taobao.org/download/tengine-1.4.2.tar.gz
tar zxf tengine-1.4.2.tar.gz
cd tengine-1.4.2
./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module
make && make install

编译参数是照抄的军哥的脚本的。wangyan的脚本里使用的参数要多得多。只是目前我觉得这样就够用了

4.配置Tengine

配置文件主要参考军哥的nginx.conf写的,用于fastcgi配置的fcgi.conf则是完全照抄。具体的参数我还没研究作用,等有空的时候再看看。

我的nginx.conf(主配置文件)内容如下,里面有部分参数是Tengine特有,部分配置是我根据我的环境添加修改的,都有注释注明。


user www www;
worker_processes auto; #auto这个值是tengine添加的,原版nginx不存在。可以自动按照CPU核心数确定worker数量
error_log  /home/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;
 }

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;

  #limit_zone  crawler  $binary_remote_addr  10m;

  #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';

server #此段server是禁止通过ip/未配置的域名访问,相当有用
 {
  server_name _;
  return 403;
 }
server #默认段
 {
  listen 80;
  server_name 站点域名;
  index index.html index.htm index.php;
  root  /home/wwwroot;

  location ~ .*\.(php|php5)?$
   {
    try_files $uri =404;
    fastcgi_pass  unix:/tmp/php-fpm.sock; #我的php-fpm配置为unix socket工作方式,如果是监听端口的记得修改配置
    fastcgi_index index.php;
    include fcgi.conf; #此处fcgi.conf配置FastCGI相关的参数,我使用的军哥的版本,nginx默认的是fastcgi.conf
   }

  location /status {
   stub_status on;
   access_log off;
  }

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

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

  access_log  /home/wwwlogs/access.log  access;
 }
include vhost/*.conf; #将其它虚拟主机的配置文件置于vhost子文件夹下即可通过此语句自动生效
}

5.配置启动脚本

nginx启动脚本通用性很强,我直接借用军哥提供的脚本:


wget -c http://soft.vpser.net/lnmp/ext/init.d.nginx
cp init.d.nginx /etc/init.d/nginx
chmod +x /etc/init.d/nginx
update-rc.d -f nginx defaults
配置完成。此时随便往wwwroot下cat >> index.html点内容就可以访问测试了。直接访问http://域名/status也可以,能够看到tengine的状态模块。

MySQL

1.安装前配置

同样是先安装使用的包,再配置用户……然后同样我也把具体依赖哪些包忘掉了=。=还是缺啥补啥就可以了。


apt-get install libncurses5 libncurses5-dev
groupadd mysql
useradd -M -s /usr/sbin/nologin -g mysql mysql

2.编译安装MySQL

先吐个槽……MySQL的源码包下载地址还真是难找,我最后是从5.1.66发布的新闻上找到的,官网我翻了半天愣是没翻到直接的地址。


wget ftp://mirror.switch.ch/mirror/mysql/Downloads/MySQL-5.1/mysql-5.1.66.tar.gz
tar zxf mysql-5.1.66.tar.gz
cd mysql-5.1.66
./configure --prefix=/usr/local/mysql --with-extra-charsets=complex --enable-thread-safe-client --enable-assembler --with-mysqld-ldflags=-all-static --with-charset=utf8 --enable-thread-safe-client --with-big-tables --with-readline --with-ssl --with-embedded-server --enable-local-infile
make && make install
MySQL编译的时间较长,建议开screen编译然后去喝杯咖啡之类的……配置参数上可以看出来我没有安装innodb。要装的话需要加上—with-plugins=innobase。早期MySQL默认是编译有debug信息的版本,自5.1.16后默认编译无debug信息的版本了(需要的话要加—with-debug)。两者之间的性能差距据说有5%-10%。另外—with-mysqld-ldflags=-all-static也是一个对性能提升比较重要的参数。

3.配置MySQL


cp /usr/local/mysql/share/mysql/my-medium.cnf /etc/my.cnf
sed -i 's/skip-locking/skip-external-locking/g' /etc/my.cnf

主配置文件。第二句的作用是把skip-locking改为skip-external-locking。MySQL5.5将废弃skip-locking选项,使用的话会出错。而现在5.1系列会提示建议用skip-external-locking来替换。

接下来安装MySQL数据库:

/usr/local/mysql/bin/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/var

配置启动脚本:

cp /usr/local/mysql/share/mysql/mysql.server /etc/init.d/mysql
chmod 755 /etc/init.d/mysql

执行安全安装清理:

/usr/local/mysql/bin/mysql_secure_installation

刚安装完MySQL,root是没有密码的。执行这个脚本会协助你为root设置密码,并删除数据库中的匿名账户和测试数据库。
 
然后清理权限和符号链接,此部分来源于军哥的lnmp脚本,具体作用我还不是很清楚:


chown -R mysql /usr/local/mysql/var
chgrp -R mysql /usr/local/mysql/.

cat > /etc/ld.so.conf.d/mysql.conf<<EOF
/usr/local/mysql/lib/mysql
/usr/local/lib
EOF
ldconfig

ln -s /usr/local/mysql/share/mysql /usr/share/
ln -s /usr/local/mysql/lib/mysql /usr/lib/mysql
ln -s /usr/local/mysql/include/mysql /usr/include/mysql

ln -s /usr/local/mysql/bin/mysql /usr/bin/mysql
ln -s /usr/local/mysql/bin/mysqldump /usr/bin/mysqldump
ln -s /usr/local/mysql/bin/myisamchk /usr/bin/myisamchk
MySQL安装完成,可以用/etc/init.d/mysql start来测试一下了。

PHP-FPM

好了重头戏来了……这部分我基本上是一行一行照着wangyan的脚本执行的,没有多少自己尝试的成分。而且同之前一样,具体需要安装哪些包我也忘了……

1.安装准备


apt-get -y install zlibc zlib1g zlib1g-dev
apt-get -y install libfreetype6 libfreetype6-dev
apt-get -y install libxml2 libxml2-dev
apt-get -y install libmhash2 libmhash-dev
apt-get -y install curl libcurl3 libcurl4-openssl-dev
apt-get -y install libxmlrpc-core-c3 libxmlrpc-core-c3-dev

2.编译依赖

需要编译的依赖主要包括libpng、libjpeg、libiconv(这三者gd要用)、libmcrypt、mhash、mcrypt。编译的过程非常一致:


wget 源码包地址
tar zxf 源码包
cd 源码目录
./configure --prefix=/usr/local
make && make install
其中地址为:
http://src-mirror.googlecode.com/files/libpng-1.5.13.tar.gz http://src-mirror.googlecode.com/files/jpegsrc.v8d.tar.gz http://src-mirror.googlecode.com/files/libiconv-1.14.tar.gz http://src-mirror.googlecode.com/files/libmcrypt-2.5.8.tar.gz http://src-mirror.googlecode.com/files/mhash-0.9.9.9.tar.gz http://src-mirror.googlecode.com/files/mcrypt-2.6.8.tar.gz

mhash编译完成后需要执行一次ldconfig。

3.编译PHP

因为选用的版本是5.4.9,所以PHP-FPM的支持是自带的,不需要像5.3之前的版本那样下载php-fpm-diff来手动patch源码。

安装过程如下:


wget http://www.php.net/distributions/php-5.4.9.tar.gz
tar zxf php-5.4.9
cd php-5.4.9
./configure --prefix=/usr/local/php --with-curl --with-curlwrappers --with-freetype-dir --with-gettext --with-gd --with-iconv-dir --with-jpeg-dir --with-libxml-dir --with-mcrypt --with-mhash --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-openssl --with-pear --with-png-dir --with-xmlrpc --with-zlib --enable-bcmath --enable-calendar --enable-exif --enable-fpm --enable-ftp --enable-gd-native-ttf --enable-inline-optimization --enable-mbregex --enable-mbstring --enable-pcntl --enable-shmop --enable-soap --enable-sockets --enable-sysvsem --enable-sysvshm --enable-xml --enable-zip
make ZEND_EXTRA_LIBS='-liconv'
make install

超长的configure命令有木有。wangyan原版脚本里面参数是用\续行符分成一个参数一行的,一个屏幕只显示了这一个命令……
具体的参数意义我就不讲了,基本上都能从名字上看出来。比较关键的—enable-fpm别漏了。

4.安装部分PHP扩展

包括PDO MySQL和memcache扩展。

PHP扩展的安装方式非常统一,都是到指定目录下执行phpize,然后再进行常规编译安装即可。
下面的命令开始前工作目录在PHP的源码目录下。


cd ext/pdo_mysql/
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config --with-pdo-mysql=/usr/local/mysql
make && make install

cd ../../..
wget http://src-mirror.googlecode.com/files/memcache-2.2.6.tgz
tar zxf memcache-2.2.6.tgz
cd memcache*
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config --with-zlib-dir --enable-memcache
make && make install

5.PHP配置

继续照抄wangyan的脚本。配置主要包括两个方面,一个是php.ini的配置,一个是php-fpm的配置


for i in `ls /usr/local/php/bin`; do ln -s /usr/local/php/bin/$i /usr/bin/$i; done #为php的执行文件做软连接

cp php-*/php.ini-production /usr/local/php/lib/php.ini #此处的php-*指php源码目录,根据情况进行调整
sed -i 's#; extension_dir = "./"#extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/"\nextension = "memcache.so"\nextension = "pdo_mysql.so"\n#g' /usr/local/php/lib/php.ini #修改扩展的目录,启用memcache和pdo_mysql

sed -i 's/short_open_tag = Off/short_open_tag = On/g' /usr/local/php/lib/php.ini
sed -i 's/disable_functions =/disable_functions = system,passthru,exec,shell_exec,popen,symlink,dl/g' /usr/local/php/lib/php.ini
sed -i 's/max_execution_time = 30/max_execution_time = 300/g' /usr/local/php/lib/php.ini
sed -i 's/post_max_size = 8M/post_max_size = 80M/g' /usr/local/php/lib/php.ini
sed -i 's/magic_quotes_gpc = Off/magic_quotes_gpc = On/g' /usr/local/php/lib/php.ini
sed -i 's/;cgi.fix_pathinfo=1/cgi.fix_pathinfo=0/g' /usr/local/php/lib/php.ini
sed -i 's/upload_max_filesize = 2M/upload_max_filesize = 20M/g' /usr/local/php/lib/php.ini
#sed -i 's#;date.timezone =#date.timezone = Asia/Shanghai#g' /usr/local/php/lib/php.ini #这句被我去掉是因为我并不想使用上海时区
sed -i 's#;sendmail_path =#sendmail_path = /usr/sbin/sendmail -t -i#g' /usr/local/php/lib/php.ini
接下来要进行php-fpm的配置。php-fpm的配置文件我使用的是wangyan的版本,他的php-fpm.conf的内容在此处,拷贝到/usr/local/php/etc/目录下。其中对listen参数的配置,他使用的是在9000端口监听的形式来提供fastcgi服务,我把它修改成了/tmp/php-fpm.sock的unix socket形式以与我的nginx配置保持统一。另外他默认的运行用户和组也是www和www,如果有不同需要根据环境进行修改。
然后配置php-fpm的启动脚本(源码自带)

cp php-5.4.*/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm #此处的php-*指php源码目录,需要根据情况进行调整
chmod 755 /etc/init.d/php-fpm
update-rc.d -f php-fpm defaults
/etc/init.d/php-fpm start
此时安装基本完成,可以在wwwroot下写一个php文件填入phpinfo()来测试工作是否正常。

6.安装Zend Guard Loader

虽然我这么写了,但是实际上PHP5.4是没有Zend Guard Loader(之前的Zend Optimizer)的,官方提供的只到5.3为止。wangyan的脚本里虽然将它拷贝并配置好了,但是实际上并不能被加载。查阅一些资料后发现等Zend出新版本的ZGL还要等很久。

因为本身对PHP依赖就不大,所以其它的Xcache、APC之类的加速器我也没装,就此收工了

到此为止,一个完整的Nginx+MySQL+PHP-FPM的环境就编译安装完成了。我挂了了一些监控脚本做测试,各项功能都很正常。

wangyan的lanmp脚本写的非常非常漂亮,比军哥的lnmp脚本可读性强很多。建议有兴趣的童鞋都去看一看。

打赏

打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

百分百源码网 建议打赏1~10元,土豪随意,感谢您的阅读!

共有5人阅读,期待你的评论!发表评论
昵称: 网址: 验证码: 点击我更换图片
最新评论

本文标签

广告赞助

能出一分力是一分吧!

订阅获得更多模板

本文标签

广告赞助

订阅获得更多模板