Chinaunix首页 | 论坛 | 博客
  • 博客访问: 164781
  • 博文数量: 52
  • 博客积分: 2295
  • 博客等级: 大尉
  • 技术积分: 540
  • 用 户 组: 普通用户
  • 注册时间: 2009-11-22 17:42
文章分类

全部博文(52)

文章存档

2013年(1)

2012年(11)

2011年(1)

2010年(31)

2009年(8)

我的朋友

分类: LINUX

2010-04-20 15:53:15

一、CentOS准备工作.
# 更新所有已安装软件包
yum -y update # 安装必要的开发工具
yum -y install 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

1. 创建nginx运行的用户.

groupadd nginx
useradd nginx -g nginx

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

mkdir /var/www
chmod +w /var/www
chown -R nginx:nginx /var/www

3. 下载Nginx源码包【】.

cd /work/soft
wget
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

vi /work/webServer/nginxStart.sh
#!/bin/sh
/usr/sbin/nginx

vi /work/webServer/nginxRestart.sh
#!/bin/sh
killall -9 nginx
/usr/sbin/nginx

chmod +x /work/webServer/nginxStart.sh
chmod +x /work/webServer/nginxRestart.sh

5. 启动 nginx.

/usr/sbin/nginx

/work/webServer/nginxStart.sh

6. 访问一下看看.

 看到 Welcome to nginx! 安装便算OK了!

三、编译安装MySQL.

cd /work/soft
/usr/sbin/groupadd mysql
/usr/sbin/useradd -g mysql mysql

# 自己看看哪个下载的速度好就用哪个吧,由于我的服务器在国外,国内的下载反而慢一些
wget http://dev.mysql.com/get/Downloads/MySQL-5.5/mysql-5.5.0-m2.tar.gz/from/http://opensource.become.com/mysql/
tar zxvf mysql-5.5.0-m2.tar.gz
cd mysql-5.5.0-m2/

# 国内s135的服务器,当然你也可以去s135.com上看看,那里的文章一直以来我都认为挺不错的
wget http://blog.s135.com/soft/linux/nginx_php/mysql/mysql-5.5.2-m2.tar.gz
tar zxvf mysql-5.5.2-m2.tar.gz
cd mysql-5.5.2-m2/

./configure --prefix=/work/webServer/mysql/ --enable-assembler --with-extra-charsets=complex --enable-thread-safe-client --with-big-tables --with-readline --with-ssl --with-embedded-server --enable-local-infile --with-plugins=partition,innobase,myisammrg
make && make install
chmod +w /work/webServer/mysql
chown -R mysql:mysql /work/webServer/mysql

①、创建MySQL数据库存放目录

mkdir -p /work/webServer/mysqlData/data/
mkdir -p /work/webServer/mysqlData/binlog/
mkdir -p /work/webServer/mysqlData/relaylog/
chown -R mysql:mysql /work/webServer/mysqlData/

②、以mysql用户帐号的身份建立数据表:

/work/webServer/mysql/bin/mysql_install_db --basedir=/work/webServer/mysql --datadir=/work/webServer/mysqlData/data --user=mysql

③、创建my.cnf配置文件: vi /work/webServer/my.cnf

输入以下内容:

[client]
character-set-server = utf8
port    = 3306
socket  = /tmp/mysql.sock

[mysqld]
character-set-server = utf8
replicate-ignore-db = mysql
replicate-ignore-db = test
replicate-ignore-db = information_schema
user    = mysql
port    = 3306
socket  = /tmp/mysql.sock
basedir = /work/webServer/mysql
datadir = /work/webServer/mysqlData/data
log-error = /work/webServer/mysqlData/mysql_error.log
pid-file = /work/webServer/mysqlData/mysql.pid
open_files_limit    = 10240
back_log = 600
max_connections = 5000
max_connect_errors = 6000
table_cache = 614
external-locking = FALSE
max_allowed_packet = 32M
sort_buffer_size = 1M
join_buffer_size = 1M
thread_cache_size = 300
thread_concurrency = 8
query_cache_size = 512M
query_cache_limit = 2M
query_cache_min_res_unit = 2k
default-storage-engine = MyISAM
thread_stack = 192K
transaction_isolation = READ-COMMITTED
tmp_table_size = 246M
max_heap_table_size = 246M
long_query_time = 3
log-slave-updates
log-bin = /work/webServer/mysqlData/binlog/binlog
binlog_cache_size = 4M
binlog_format = MIXED
max_binlog_cache_size = 8M
max_binlog_size = 1G
relay-log-index = /work/webServer/mysqlData/relaylog/relaylog
relay-log-info-file = /work/webServer/mysqlData/relaylog/relaylog
relay-log = /work/webServer/mysqlData/relaylog/relaylog
expire_logs_days = 30
key_buffer_size = 256M
read_buffer_size = 1M
read_rnd_buffer_size = 16M
bulk_insert_buffer_size = 64M
myisam_sort_buffer_size = 128M
myisam_max_sort_file_size = 10G
myisam_repair_threads = 1
myisam_recover

interactive_timeout = 120
wait_timeout = 120

skip-name-resolve
master-connect-retry = 10
slave-skip-errors = 1032,1062,126,1114,1146,1048,1396

#master-host     =   192.168.1.2
#master-user     =   username
#master-password =   password
#master-port     =  3306

server-id = 1

innodb_additional_mem_pool_size = 16M
innodb_buffer_pool_size = 512M
innodb_data_file_path = ibdata1:256M:autoextend
innodb_file_io_threads = 4
innodb_thread_concurrency = 8
innodb_flush_log_at_trx_commit = 2
innodb_log_buffer_size = 16M
innodb_log_file_size = 128M
innodb_log_files_in_group = 3
innodb_max_dirty_pages_pct = 90
innodb_lock_wait_timeout = 120
innodb_file_per_table = 0

#log-slow-queries = /work/webServer/mysqlData/slow.log
#long_query_time = 10

[mysqldump]
quick
max_allowed_packet = 32M

④、创建管理MySQL数据库的shell脚本: vi /work/webServer/mysql.sh

输入以下内容
#!/bin/sh
printf "Starting MySQL...\n"
/bin/sh /work/webServer/mysql/bin/mysqld_safe --defaults-file=/work/my.cnf 2>&1 > /dev/null &

⑤、赋予shell脚本可执行权限:

mkdir -p /work/webServer/mysql/var/
chmod +x /work/webServer/mysql.sh

⑥、启动MySQL

/work/webServer/mysql.sh

⑦、通过命令行登录管理MySQL服务器(提示输入密码时直接回车):

/work/webServer/mysql/bin/mysql -u root -p -S /tmp/mysql.sock

⑧、输入以下SQL语句,创建一个具有root权限的用户

GRANT ALL PRIVILEGES ON *.* TO 'li'@'localhost' IDENTIFIED BY '111111' with grant option;

⑨、(可选)停止MySQL

/work/webServer/mysqlData/mysql stop


四、编译安装PHP (FastCGI模式)使用php-fpm管理方式.

1.安装 libiconv.

选择最新的, 解包,安装

cd /work/soft
wget libiconv-1.13.1.tar.gz
tar zxvf libiconv-1.13.1.tar.gz
cd libiconv-1.13.1
./configure --prefix=/usr && make && make install

2.安装mhash.

cd /work/soft
wget http://blog.s135.com/soft/linux/nginx_php/mhash/mhash-0.9.9.9.tar.gz
tar -zxvf mhash-0.9.9.9.tar.gz
cd mhash-0.9.9.9
./configure --prefix=/usr && make && make install

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

3.安装mcrypt.

cd /work/soft
wget http://blog.s135.com/soft/linux/nginx_php/mcrypt/libmcrypt-2.5.8.tar.gz
tar -zxvf libmcrypt-2.5.8.tar.gz
cd libmcrypt-2.5.8
./configure && make && make install
/sbin/ldconfig
cd libltdl/
./configure --enable-ltdl-install && make && make install

cd /work/soft
wget http://blog.s135.com/soft/linux/nginx_php/mcrypt/mcrypt-2.6.8.tar.gz
tar -zxvf mcrypt-2.6.8.tar.gz
cd mcrypt-2.6.8
LD_LIBRARY_PATH=/usr/local/lib ./configure --prefix=/usr && make && make install

4. 编译安装libevent.

下载 ~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源码包进行编译安装]

cd /work/soft
wget ~provos/libevent-1.4.13-stable.tar.gz
tar zxvf libevent-1.4.13-stable.tar.gz
cd libevent-1.4.13-stable
./configure --prefix=/usr && make && make install

4. 下载PHP源码包.

cd /work/soft
wget
tar -zxvf php-5.3.1.tar.gz

5. 下载php-fpm.

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

cd /work/soft
wget ~5.3.1.tar.gz
tar -zxvf php-fpm-0.6~5.3.1.tar.gz

# 生成补丁

php-fpm-0.6-5.3.1/generate-fpm-patch
cd php-5.3.1

# 对php源码打上php-fpm的补丁()

patch -p1 < ../fpm.patch

./buildconf --force
mkdir fpm-build
cd fpm-build

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

../configure --prefix=/usr \
--with-config-file-path=/etc/php5 \
--with-mysql=/work/webServer/mysql \
--with-mysqli=/work/webServer/mysql/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-fpm \
--with-libevent=/usr/lib\
--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 clean
make ZEND_EXTRA_LIBS='-liconv'
make install
# 应当可以看到这一行
# Installing PHP SAPI module: fpm
# …
# 并且存在 /usr/bin/php-fpm 即代表安装成功

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

安装PEAR
curl | /usr/bin/php

启动php-fpm
/etc/init.d/php-fpm start


五、配置与优化.
添加开机服务:

vim /etc/rc.local

/usr/sbin/nginx
/work/webServer/mysql.sh
/etc/init.d/php-fpm start

重启命令:

killall -9 nginx

/usr/sbin/nginx


大概说明:

现在,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:
No input file specified.
注意如下两个参数的值
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;

 

建立一些日志文件:

vi /work/webServer/NginxAccess.log

 

Nginx.conf例子:

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
#pid        logs/nginx.pid;

events 
{
    worker_connections  1024;
}


http 
{
# 设定mime类型
    include mime.types;
    default_type application/octet-stream;
# 设定日志格式
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
# 设定access log
    access_log /work/webServer/NginxAccess.log main;
    sendfile on;
    keepalive_timeout 65;

    server 
    {
        listen      80;
        server_name *.vcn2008.com;
charset UTF-8;

        location / 
        {
            root /work/vcn2008.com;
            index index.html index.htm index.php;
        }

        location ~ \.php$ 
        {
            root           /work/vcn2008.com;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /work/vcn2008.com$fastcgi_script_name;
            include        fastcgi_params;
        }
    }
}


阅读(2236) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2011-01-05 15:07:58

朋友,能不能写一篇centos5.4下安装PHP5.3.4的,centos不熟悉,安装起来麻烦,全是命令。不像windows那样,解压下来就能用。