G-framework
一款优秀的国产PHP MVC轻量级开发框架
我的博客
CentOS下搭建LNMP环境

系统:CentOS 6.3 64位
nginx版本:1.4.5
php版本:5.5.9
mysql版本:5.1.55

安装Mysql
#cd /usr/local/src/
通常情况下,我们把安装源放在 /usr/local/src/目录下
下载Mysql安装包
#wget http://syslab.comsenz.com/downloads/linux/mysql-5.1.55-linux-x86_64-glibc23.tar.gz
解压
#tar -zxvf mysql-5.1.55-linux-x86_64-glibc23.tar.gz
把解压完的数据包移动到/usr/local/mysql/目录下
#mv mysql-5.1.55-linux-x86_64-glibc23 /usr/local/mysql
创建mysql用户
#useradd mysql
创建数据库存放目录
#mkdir /data/
#mkdir /data/mysql/
为新创建的mysql目录赋予mysql用户权限
#chown -R mysql:mysql /data/mysql/
初始化数据库
#cd /usr/local/mysql/
#./scripts/mysql_install_db --user=mysql --datadir=/data/mysql
拷贝配置文件
拷贝哪个配置文件取决于mysql服务使用内存大小
64M使用my-small.cnf
128M使用my-medium.cnf
512M使用my-large.cnf
1G-2G使用my-huge.cnf
#cp support-files/my-large.cnf /etc/my.cnf
拷贝启动脚本文件并修改其属性
#cp support-files/mysql.server /etc/init.d/mysqld
#chmod 755 /etc/init.d/mysqld
修改启动脚本
#vi /etc/init.d/mysqld
修改项:
basedir=/usr/local/mysql
datadir=/data/mysql
把启动脚本加入系统服务项,并设定开机启动,启动mysql

#chkconfig --add mysqld

#chkconfig mysqld on

#service mysqld start

看到Starting MySQL. SUCCESS!  ,便启动成功啦!
如果启动不了,可以到/data/mysql下查看错误信息,该文件是主机名.err
可以通过reboot后ps指令查看mysql是否自启动
#reboot
#ps aux |grep mysqld
也可以使用#service mysqld status指令查看mysql运行状态

安装php
创建www用户
# useradd www
进入安装包下载目录
# cd /usr/local/src/
下载源码
# wget http://cn2.php.net/distributions/php-5.5.9.tar.gz
解压
# tar -zxvf php-5.5.9.tar.gz
php依赖很多类库,先把所缺少的类库安装完后再编译安装php
通常下面这个语句会将php所依赖的包安装上

# yum install -y gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5-libs krb5-devel krb5-server libidn libidn-devel openssl openssl-devel openldap openldap-devel nss_ldap openldap-clients openldap-servers

yum能安装上大多数的包,但个别包是安装不上的,有可能找不到源文件。安装不上的包,可以手动编译安装。
手动安装libmcrypt
#cd /usr/local/src/
#wget ftp://mcrypt.hellug.gr/pub/crypto/mcrypt/attic/libmcrypt/libmcrypt-2.5.7.tar.gz
#tar -zxvf libmcrypt-2.5.7.tar.gz
#cd libmcrypt-2.5.7
# ./configure prefix=/usr/local/libmcrypt
#make && make install
安装完依赖的类库后便可以编译php了
进入php安装目录
# cd php-5.5.9
环境检测及配置
./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-mysql=/usr/local/mysql --with-mysql-sock=/tmp --with-libxml-dir --with-gd --with-jpeg-dir --with-png-dir --with-freetype-dir --with-iconv-dir --with-zlib-dir --with-mcrypt=/usr/local/libmcrypt --enable-soap --enable-gd-native-ttf --enable-ftp --enable-mbstring --enable-exif --disable-ipv6 --enable-fpm
看到“Thank you for using PHP.”后,我们就可以进行编译安装了!
#make && make install
安装好php后,我们需要对php进行配置,拷贝php初始化配置文件
#cp php.ini-production /usr/local/php/etc/php.ini
#cd /usr/local/php/etc/
#cp php-fpm.conf.default php-fpm.conf
#vi php-fpm.conf
找到以下项:
user = nobody
group = nobody
修改为:
user = www
group = www
php-fpm进程数量优化,这个根据服务器配置和项目访问量进行设置,具体大家可以百度一下“PHP-FPM 配置优化”这篇文章。
pm = static
pm.max_children = 20
修改php.ini文件
#vi php.ini
修改display_errors = On ,这个是为了调试方便,建议投入生产环境后将此项修改为Off
启动php
#cp /usr/local/src/php-5.5.9/sapi/fpm/init.d.php-fpm /usr/local/php/sbin/init.d.php-fpm
#cd /usr/local/php/sbin/
#chmod 755 init.d.php-fpm 
#/usr/local/php/sbin/init.d.php-fpm start
看到“Starting php-fpm  done”,php便启动成功了。
#ps aux |grep php
可以看到以www用户启动了20个php-fpm进程
加入自启动
#vi /etc/rc.d/rc.local
最后边加入一行
/usr/local/php/sbin/init.d.php-fpm start
然后可以reboot测试下php-fpm是否自启动了。

安装Nginx
# cd /usr/local/src/
# wget http://nginx.org/download/nginx-1.4.5.tar.gz
# tar -zxvf nginx-1.4.5.tar.gz 
编译Nginx前,需要安装所需要的类库
# wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.34.tar.gz
# tar -zxvf pcre-8.34.tar.gz
pcre解压出来就可以,不需要编译安装

进入Nginx目录
# cd /usr/local/src/nginx-1.4.5/
# ./configure --prefix=/usr/local/nginx --sbin-path=/usr/local/nginx/sbin/nginx --conf-path=/usr/local/nginx/conf/nginx.conf --error-log-path=/usr/local/nginx/logs/error.log --http-log-path=/usr/local/nginx/logs/access.log --pid-path=/usr/local/nginx/var/nginx.pid --lock-path=/usr/local/nginx/var/nginx.lock --http-client-body-temp-path=/dev/shm/nginx_temp/client_body --http-proxy-temp-path=/dev/shm/nginx_temp/proxy --http-fastcgi-temp-path=/dev/shm/nginx_temp/fastcgi --user=www --group=www --with-cpu-opt=pentium4F --without-select_module --without-poll_module --with-http_realip_module --with-http_sub_module --with-http_gzip_static_module --with-http_stub_status_module --without-http_ssi_module --without-http_userid_module --without-http_geo_module --without-http_memcached_module --without-http_map_module --without-mail_pop3_module --without-mail_imap_module --without-mail_smtp_module --with-pcre=/usr/local/src/pcre-8.34
编译安装Nginx
# make && make install
# mkdir /dev/shm/nginx_temp

编写nginx的启动脚本,并加入系统服务

# vi /etc/init.d/nginx 写入以下内容:

#!/bin/bash

# chkconfig: - 30 21

# description: http service.

# Source Function Library

. /etc/init.d/functions

# Nginx Settings

NGINX_SBIN="/usr/local/nginx/sbin/nginx"

NGINX_CONF="/usr/local/nginx/conf/nginx.conf"

NGINX_PID="/usr/local/nginx/var/nginx.pid"

RETVAL=0

prog="Nginx"

start() {

       echo -n $"Starting $prog: "

       mkdir -p /dev/shm/nginx_temp

       daemon $NGINX_SBIN -c $NGINX_CONF

       RETVAL=$?

       echo

       return $RETVAL

}

stop() {

       echo -n $"Stopping $prog: "

       killproc -p $NGINX_PID $NGINX_SBIN -TERM

       rm -rf /dev/shm/nginx_temp

       RETVAL=$?

       echo

       return $RETVAL

}

reload(){

       echo -n $"Reloading $prog: "

       killproc -p $NGINX_PID $NGINX_SBIN -HUP

       RETVAL=$?

       echo

       return $RETVAL

}

restart(){

       stop

       start

}

configtest(){

   $NGINX_SBIN -c $NGINX_CONF -t

   return 0

}

case "$1" in

 start)

       start

       ;;

 stop)

       stop

       ;;

 reload)

       reload

       ;;

 restart)

       restart

       ;;

 configtest)

       configtest

       ;;

 *)

       echo $"Usage: $0 {start|stop|reload|restart|configtest}"

       RETVAL=1

esac

exit $RETVAL

保存后,更改/etc/init.d/nginx的权限

#chmod 755 /etc/init.d/nginx

加入自启动

#chkconfig --add nginx

#chkconfig nginx on

修改nginx配置文件
#vi /usr/local/nginx/conf/nginx.conf ,清空文件,然后粘贴以下内容。
user www www;
worker_processes 2;
error_log /usr/local/nginx/logs/nginx_error.log crit;
pid /usr/local/nginx/var/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 6000;
}
http
{
include mime.types;
default_type application/octet-stream;
server_names_hash_bucket_size 2048;
server_names_hash_max_size 4096;
log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_local] '
'$host "$request_uri" $status '
'"$http_referer" "$http_user_agent"';
sendfile on;
tcp_nopush on;
keepalive_timeout 30;
client_header_timeout 3m;
client_body_timeout 3m;
send_timeout 3m;
connection_pool_size 256;
client_header_buffer_size 1k;
large_client_header_buffers 8 4k;
request_pool_size 4k;
output_buffers 4 32k;
postpone_output 1460;
client_max_body_size 10m;
client_body_buffer_size 256k;
client_body_temp_path /usr/local/nginx/client_body_temp;
proxy_temp_path /usr/local/nginx/proxy_temp;
fastcgi_temp_path /usr/local/nginx/fastcgi_temp;
fastcgi_intercept_errors on;
tcp_nodelay on;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 8k;
gzip_comp_level 5;
gzip_http_version 1.1;
gzip_types text/plain application/x-javascript text/css text/htm application/xml;

    server
    {
        listen 80;
        server_name www.a1.com;
        index index.html index.htm index.php;
        root /data/www;
        error_log  /usr/local/nginx/logs/a1.com.log;
        location ~ .php$ {
            include fastcgi_params;
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        }
    }
}
接下来让我们启动Nginx
#/usr/local/nginx/sbin/nginx -t
如果出现syntax is ok 或者 test is successful 之类的提示,那么配置便没有问题了。
#service nginx start
提示“Starting Nginx: [  OK  ]”那么便可以了。
对Nginx操作
#/etc/init.d/nginx {start|stop|reload|restart|configtest}
也可以使用
#service nginx {start|stop|reload|restart|configtest}
上一篇:没有了
下一篇:CentOS下搭建SVN环境