CentOS安装配置 Nginx(0.8.33)+PHP(5.3.1)+php-fpm+MySQL 全过程

服务器 2717 0 2012-03-28

CentOS安装配置 Nginx(0.8.33)+PHP(5.3.1)+php-fpm+MySQL 全过程
CentOS准备工作:

# 更新所有已安装软件包 yum -y update# 安装必要的开发工具 yum -y install \
glibc \gcc gcc-c++ autoconf make \
libjpeg libjpeg-devel libpng libpng-devel \
freetype \
freetype-devel \
zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel \
ncurses ncurses-devel \
curl curl-devel \
e2fsprogs e2fsprogs-devel \
krb5 krb5-devel \
libidn libidn-devel \
openssl openssl-devel \
openldap openldap-devel nss_ldap openldap-clients openldap-servers \
libxml2 libxml2-devel \patch \
pcre-devel# 上面安装的东东,像gcc, make, autoconf是必要的编译工具 # 像libjpeg,freetype,zlib等,编译PHP时用得到 # 像patch, libxml2等,在使用php-fpm对php打补顶时用得着 # 像pcre-dev等,在编译Nginx服务器时用得着

【一】编译安装Nginx服务器

0. 确保安装了如下软件

yum install gcc openssl-devel pcre-devel zlib-devel

PS:如果在执行这步的时候报:“No Package Matching glibc.i686”的错误,原因及处理办法如下:

原因:

    可能是你的CentOS是64位的,64位不带32位的版本仓库

解决办法:

看看这个文件存在吗
/etc/yum.repos.d/CentOS-Base.repo

存在就copy一分,名字为CentOS32-Base.repo在同样的路径下,然后
yum install glibc glibc.i386 --enablerepo=c532*

然后就解决了,yum正常了

1. 创建nginx运行的用户

groupadd nginx
useradd nginx -g nginx

2. 创建网页文件存储目录

mkdir /var/wwwchmod +w /var/wwwchown -R nginx:nginx /var/www

3. 下载Nginx源码包【Nginx官方维基】

wget http://sysoev.ru/nginx/nginx-0.8.33.tar.gz
 tar -zxvf nginx-0.8.33.tar.gz
 cd nginx-0.8.33
 
./configure \--prefix=/usr \--sbin-path=/usr/sbin/nginx \--conf-path=/etc/nginx/nginx.conf \--error-log-path=/var/log/nginx/error.log \--pid-path=/var/run/nginx/nginx.pid  \--lock-path=/var/lock/nginx.lock \--user=nginx \--group=nginx \
--with-http_ssl_module \
--with-http_flv_module \
--with-http_gzip_static_module \--http-log-path=/var/log/nginx/access.log \--http-client-body-temp-path=/var/tmp/nginx/client/ \--http-proxy-temp-path=/var/tmp/nginx/proxy/ \--http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \
--with-http_stub_status_module
 make make install

# with-http_stub_status_module 模块可用来统计当前连接数 【更多Nginx模块】
# 添加指定的 Nginx 扩展模块只需要 configure 时带上 –with-模块名 即可
# 小技巧:如已经安装好了Nginx,好添加一个新模块,只需要重新配置,重新 configure && make 但别 make install, 直接将objs/nginx 拷贝到{$prefix}/sbin下即可,【注意备份原来的】

4. 创建nginx需要的文件夹

mkdir -p /var/tmp/nginx

5. 启动 nginx

/usr/sbin/nginx

6. 访问一下看看
看到 Welcome to nginx! 安装便算OK了。

7. 设置nginx 开机自启动

vim /etc/rc.local# 增加 /usr/sbin/nginx

【二】编译安装MySQL

1. 增加用于运行mysql的用户和组

groupadd mysql
useradd -g mysql mysql

2. 配置并安装

./configure \--prefix=/usr/local \--enable-assembler \--with-extra-charsets=complex \--enable-thread-safe-client \
--with-named-curses-libs=/usr/lib/libncursesw.so.5 \--with-big-tables \--with-readline \--with-ssl \--with-embedded-server \--enable-local-infile \--with-plugins=innobase

3. 复制mysql配置文件

cp support-files/my-medium.cnf /etc/my.cnf

4. 安装数据库文件

/usr/bin/mysql_install_db --user=mysql

5. 启动mysql服务器

/usr/bin/mysqld_safe --user=mysql &

6. 设置root帐号密码

/usr/bin/mysqladmin -u root password 'new-password'

7. 移除测试数据库,禁止远程root访问等安装操作

/usr/bin/mysql_secure_installation

8. 创建一个mysql的管理脚本

vim  /etc/init.d/mysql

输入如下内容

#!/bin/sh  mysql_username="root" mysql_password="new-password"  
start_mysql() { printf "start mysql ...\n" /bin/sh /usr/bin/mysqld_safe --user=mysql &}  
stop_mysql() { printf "stop mysql ...\n" /usr/bin/mysqladmin -u${mysql_username} -p${mysql_password} -S /tmp/mysql.sock shutdown}  case "$1" in start) start_mysql;; stop) stop_mysql;; restart) stop_mysql
start_mysql;; *) printf "Usage: {start | stop | restart}\n" esac

9. 同上,在/etc/rc.local中添加mysql自启动

vim /etc/rc.local#添加 /etc/init.d/mysql start

【三】编译安装PHP (FastCGI模式)使用php-fpm管理方式

1.安装 libiconv

http://ftp.gnu.org/pub/gnu/libiconv/

选择最新的

http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.13.1.tar.gz

解包,安装
tar zxvf libiconv-VERSION
./configure –prefix=/usr
make & make install

2.安装mcrypt
参考文章: http://koda.javaeye.com/blog/420991
总共需要下载三个文件 libmcrypt-VERSION.tar.gz, mhash-VERSION.tar.gz 与 mcrypt-VERSION.tar.gz

下载地址[http://sourceforge.net/projects/mcrypt/files/ http://sourceforge.net/projects/mhash/files]
wget http://sourceforge.net/projects/mcrypt/files/Libmcrypt/2.5.8/libmcrypt-2.5.8.tar.gz/download
wget http://sourceforge.net/projects/mhash/files/mhash/0.9.9.9/mhash-0.9.9.9.tar.gz/download
wget http://sourceforge.net/projects/mcrypt/files/MCrypt/2.6.8/mcrypt-2.6.8.tar.gz/download

2.1首先安装libmcrypt

tar -zxvf libmcrypt-2.5.8.tar.gzcd libmcrypt-2.5.8
./configuremake make install /sbin/ldconfigcd libltdl/ ./configure --enable-ltdl-install make make install cd ../../

2.2接着安装mhash
下载地址 http://sourceforge.net/projects/mhash/files/

tar -zxvf mhash-0.9.9.9.tar.gzcd mhash-0.9.9.9
./configure --prefix=/usrmake make install

2.3最后安装mcrypt

tar -zxvf mcrypt-2.6.8.tar.gzcd mcrypt-2.6.8/sbin/ldconfigLD_LIBRARY_PATH=/usr/local/lib  ./configure --prefix=/usrmake make install

<<上面三个库也可以直接使用 yum install 安装,注意安装 devel版即可>>
## 即 yum install libmcrypt-devel mhash-devel libmcrypt-devel

4. 编译安装libevent
下载 http://www.monkey.org/~provos/libevent/
[建议不要使用yum的方式安装libevent,php-fpm建议Libevent 1.4.12-stable or higher is recommended, and at least libevent 1.4.3-stable is required,因此php-fpm需要1.4.3以上版本libevent的支持,所以去libevent的官网下最新稳定版的libevent源码 包进行编译安装]

wget http://www.monkey.org/~provos/libevent-1.4.13-stable.tar.gztar zxvf libevent-1.4.13-stable.tar.gzcd libevent-1.4.13-stable
./configure --prefix=/usrmake make install

4. 下载PHP源码包
wget http://cn.php.net/get/php-5.3.1.tar.gz/from/this/mirror

tar -zxvf php-5.3.1.tar.gz

5. 下载php-fpm
参考文档【http://php-fpm.org/wiki/Documentation】
说明:根据php-fpm.org官网上所说,在PHP 5.3.2+5.3.4 以后,php-fpm将包含到php core中,不再需要打补丁了,对于目前的5.3.1还是需要通过补丁扩展php-fpm功能。

#下载 wget http://launchpad.net/php-fpm/master/0.6/+download/php-fpm-0.6~5.3.1.tar.gztar -zxvf php-fpm-0.6~5.3.1.tar.gz# 生成补丁 php-fpm-0.6-5.3.1/generate-fpm-patchcd php-5.3.1# 对php源码打上php-fpm的补丁 patch -p1 &lt; ../fpm.patch
 
./buildconf --force mkdir fpm-buildcd fpm-build

# 特别注意以下的配置参数
# 特别注意
# –enable-fastcgi \
# –with-fpm \ (*) 不是 –enable-fpm 而是 –with-fpm
# –with-libevent=/usr/lib\
# 以上这三项,第一个是开启fastcgi, 第二个是开启 php-fpm,第三个是指定php-fpm所需要的libevent的位置

../configure --prefix=/usr \--with-config-file-path=/etc/php5 \--with-mysql=/usr \--with-mysqli=/usr/bin/mysql_config \--with-pdo-mysql=/usr/bin/mysql_config \--with-iconv-dir=/usr \--with-freetype-dir \--with-jpeg-dir \--with-png-dir \--with-zlib \--with-libxml-dir=/usr \--enable-xml \--enable-discard-path \--enable-safe-mode \--enable-bcmath \--enable-shmop \--enable-sysvsem \--enable-inline-optimization \--with-curl \--with-curlwrappers \--enable-mbregex \--enable-fastcgi \--with-libevent=/usr \--with-fpm \--enable-force-cgi-redirect \--enable-mbstring \--with-mcrypt \--with-gd \--enable-gd-native-ttf \--with-openssl \--with-mhash \--enable-pcntl \--enable-sockets \--with-ldap \--with-ldap-sasl \--with-xmlrpc \--enable-zip \--enable-soap \--without-pear  make cleanmake ZEND_EXTRA_LIBS='-liconv' make install

#
# 在64位CentOS上可能遇到找不到libevent,可先执行如下命令
# ln -s /usr/lib/libevent.a /usr/lib64/libevent.a
#
# 安装完成之后,应当可以看到这一行
#
# Installing PHP SAPI module: fpm
# …
# 并且存在 /usr/bin/php-fpm 即代表安装成功

cd ..mkdir /etc/php5cp php.ini-production /etc/php5/php.ini

安装PEAR

curl http://pear.php.net/go-pear  | /usr/bin/php

启动php-fpm
/etc/init.d/php-fpm start
【四】配置与优化

现在,Nginx,Mysql, FastCGI模式的PHP都已经安装完毕了,需要进行的工作是配置优化,首先熟悉一下配置文件的位置

Nginx的配置文件在 /etc/nginx 下面
PHP的配置文件,即熟悉的php.ini 在 /etc/php5 中
php-fpm的配置文件为 /etc/php-fpm.conf
MySQL 的配置文件为 /etc/my.cnf

Nginx日志文件在 /var/log/nginx 下面
php-fpm日志文件在 /var/log/php-fpm.log
[一定要经常查看日志记录,找出系统潜在的问题]

1. 安装eaccelerator
eaccelerator可以有效的提升PHP效率,因为它可以将编译好的PHP脚本中间码OPCODE缓存在内存中,这样,如果下一次请求同样的PHP 脚本时,会先在内存中查找,如果存在,就不需要重复解析了。
官网:http://eaccelerator.net/

wget http://bart.eaccelerator.net/source/0.9.6/eaccelerator-0.9.6.tar.bz2tar jxvf eaccelerator-0.9.6.tar.bz2cd eaccelerator-0.9.6
phpize
./configuremake & make install

创建eaccelerator缓存目录
mkdir -p /var/cache/eaccelerator
chown nginx:nginx /var/cache/eaccelerator

配置 具体配置项意义参考http://eaccelerator.net/wiki/Settings

[eaccelerator] zend_extension="/usr/lib/php/extensions/no-debug-non-zts-20090626/eaccelerator.so" eaccelerator.shm_size="256" eaccelerator.cache_dir="/var/cache/eaccelerator" eaccelerator.enable="1" eaccelerator.optimizer="1" eaccelerator.check_mtime="0" eaccelerator.debug="0" eaccelerator.filter="" eaccelerator.shm_max="0" eaccelerator.shm_ttl="3600" eaccelerator.shm_prune_period="3600" eaccelerator.shm_only="0" eaccelerator.compress="1" eaccelerator.compress_level="9"

附常见错误1:
No input file specified.
注意如下两个参数的值
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;

上一篇:在centos里安装及配置nginx记录

下一篇:轻松搞定CentOS+Nginx+PHP+MySQL标准生产环境

讨论数量:0

请先登录再发表讨论。 2024-04-30

天涯网魂
3 杠 5 星
TA 的文章
TA 的随言
TA 的资源链