Lnmp
 Linux + nginx + mysql + php
 Nginx = engine x


 lamp工作流程:
  index.php --> apache ---> libphp.so ---> apache ---> client


 lnmp工作流程
   index.php --> nginx --tcp/ip或socket-> php-cgi --> nginx --> client
  

1、安装MySQL

# useradd -s /sbin/nologin mysql

# tar xvf mysql-5.1.54.tar.gz -C /usr/src


# ./configure --prefix=/usr/local/mysql --enable-assembler --enable-profiling --enable-local-infile --with-charset=utf8 --with-extra-charsets=gbk,gb2312 --with-pthread --with-low-memory --with-big-tables --with-plugins=innobase,heap,innodb_plugin --with-mysqld-ldflags='-all-static'

 

# make && make install


# vim /etc/my.cnf
[mysqld]
datadir=/data
user=mysql
socket=/data/mysqld.sock
port=3306
server-id=1
log-bin=/data/mysqld-bin
log-bin-index=/data/mysqld-bin-index


[mysqld_safe]
log-error=/data/mysqld.err
pid-file=/data/mysqld.pid


# mkdir /data
# chown mysql:mysql /data


# cd /usr/local/mysql
# ./bin/mysql_install_db --datadir=/data
Installing MySQL system tables...
OK
Filling help tables...
OK

...


# /usr/local/mysql/bin/mysqld_safe &

建议:如果一开始是用mysqld_safe启动,那么以后坚持使用该方式启动。


# mysqladmin -u root -S /data/mysqld.sock password '123'
# mysqladmin -u root -h dev.upl.com password '123'

 

2、php的一些功能包

# yum install freetype libjpeg libpng libxml2 fontconfig -y

libmcrypt-2.5.8.tar.gz
# tar xvf libmcrypt-2.5.8.tar.gz -C /usr/src
# ./configure  && make && make install
# cd libltdl/
# ./configure --enable-ltdl-install && make && make install


mhash
# ./configure  && make && make install

# vim /etc/ld.so.conf
/usr/local/lib/  《--新添加一行

# ldconfig


mcrypt-2.6.8
# ./configure  && make && make install


libiconv-1.13.tar.gz
# ./configure --prefix=/usr/local/  && make && make install
# ldconfig

gd-2.0.35.tar.gz
# ./configure --prefix=/usr/local/ --with-png --with-jpeg --with-freetype --with-fontconfig


安装对应版本mysql的开发库包
# rpm -ivh --force MySQL-shared-compat-5.1.54-1.rhel5.i386.rpm


3、编译php

# tar xvf php-5.2.13.tar.gz  -C /usr/src

给php源码包打补丁,让其支持直接使用脚本平滑的启动php和重载php
# gzip -cd php-5.2.13-fpm-0.5.13.diff.gz | patch -d /usr/src/php-5.2.13 -p1

# ./configure --enable-fastcgi --enable-force-cgi-redirect --enable-fpm --with-config-file-scan-dir=/usr/local/etc --with-libxml-dir --with-openssl --with-zlib --enable-bcmath --with-curl --with-curlwrappers --enable-ftp --with-gd --with-jpeg-dir --with-png-dir --with-freetype-dir --enable-gd-native-ttf --enable-mbstring  --with-mcrypt --with-mhash --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-ncurses --enable-pcntl --enable-soap --enable-sysvsem --with-iconv-dir=/usr/local --enable-zip  --disable-rpath


# make ZEND_EXTRA_LIBS='-liconv'

# make install


4、安装php的扩展模块
1)
memcache客户端

# cd /usr/src/memcache-2.2.5
# /usr/local/bin/phpize
# ./configure --with-php-config=/usr/local/bin/php-config  && make && make install


模块安装之后保存的路径
Installing shared extensions:     /usr/local/lib/php/extensions/no-debug-non-zts-20060613/

2) eaccelerator-0.9.6.tar.bz2

# /usr/local/bin/phpize
# ./configure --with-php-config=/usr/local/bin/php-config  && make && make install

 

拷贝php模板配置文件
# cp /usr/src/php-5.2.13/php.ini-recommended  /usr/local/etc/php.ini
# vim /usr/local/etc/php.ini
找到
extension_dir = "./"
修改:
extension_dir = "/usr/local/lib/php/extensions/no-debug-non-zts-20060613/"
extension = "memcache.so"


zend_extension="/usr/local/lib/php/extensions/no-debug-non-zts-20060613/eaccelerator.so"
eaccelerator.shm_size="16"
eaccelerator.cache_dir="/tmp/eaccelerator"
eaccelerator.enable="1"
eaccelerator.optimizer="1"
eaccelerator.check_mtime="1"
eaccelerator.debug="0"
eaccelerator.filter=""
eaccelerator.shm_max="0"
eaccelerator.shm_ttl="0"
eaccelerator.shm_prune_period="0"
eaccelerator.shm_only="0"
eaccelerator.compress="1"
eaccelerator.compress_level="9"


# chmod 777 /tmp/eaccelerator

验证模块是否正确调用
# php -m


5、编译nginx

1) pcre-8.01

# ./configure --prefix=/usr && make && make install

2)
# tar xvf  nginx-0.8.54.tar.gz -C /usr/local/

# ./configure --prefix=/usr/local/nginx --with-pcre --with-http_stub_status_module --user=daemon --group=daemon

# make && make install

 

整个架构的配置:

 lnmp工作流程
   index.php --> nginx --tcp/ip或socket-> php-cgi --> nginx --> client


1)配置并且启动php-cgi
php-cgi配置文件:php.ini
使用fpm脚本启动,而fpm脚本是有配置文件

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

127.0.0.1:9000
daemon
daemon
static
32  启动多少个php-cgi进程
204800
65535 《---最大的并发请求


启动php-cgi进程
# php-fpm start


2)配置nginx
# vim /usr/local/nginx/conf/nginx.conf

user  daemon;
worker_processes  4;
error_log  logs/error.log  info;
pid        logs/nginx.pid;
events {
    worker_connections  65535;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  www.upl.com;
        root   /www/www.upl.com/;
        index  index.html index.htm index.php;
        location ~ .*\.php$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            include        fastcgi.conf;
        }
    }
}

3)启动
# ulimit -SHn 65535
# /usr/local/nginx/sbin/nginx

# lsof -i:80

关闭服务
# /usr/local/nginx/sbin/nginx -s stop