实验01LNMP平台的搭建

实验目标:nginx可以解析php

实验步骤:

一:环境准备

1:准备搭建环境,创建用户

[root@svr110 ~]# useradd www

[root@svr110 ~]# useradd mysql

2:将原有的http网站服务停止并删除原有mysql的主配置文件,停掉mysql服务

3:解压软件包

[root@svr110 ~]# unzip lnmp+memcached.zip

lnmp+memcached               lnmp+memcached.zip

二:安装源码nginx软件包

1:解压软件包

[root@svr110 ~]# cd lnmp+memcached

[root@svr110 lnmp+memcached]# tar -zxf nginx-1.2.0.tar.gz

[root@svr110 lnmp+memcached]# cd nginx-1.2.0

2:编写运行的脚本

[root@svr110 nginx-1.2.0]# vim nginx.sh

./configure    --prefix=/usr/local/nginx     --pid-path=/usr/local/nginx/nginx.pid   --user=www   --group=www  --with-http_ssl_module   --with-http_flv_module   --with-http_stub_status_module  --with-http_gzip_static_module  --http-client-body-temp-path=/usr/local/nginx/client  --http-proxy-temp-path=/usr/local/nginx/proxy  --http-fastcgi-temp-path=/usr/local/nginx/fcgi  --http-uwsgi-temp-path=/usr/local/nginx/uwsgi  --http-scgi-temp-path=/usr/local/nginx/scgi  --with-pcre

3:运行脚本进行编译及安装

[root@svr110 nginx-1.2.0]# sh nginx.sh

[root@svr110 nginx-1.2.0]# make && make install

4:启动服务,测试

[root@svr110 nginx-1.2.0]# /usr/local/nginx/sbin/nginx

[root@svr110 nginx-1.2.0]# elinks --dump http://127.0.0.1

                               Welcome to nginx!

三:安装源码的mysql软件包

1:解压软件包

[root@svr110 lnmp+memcached]# tar -zxf cmake-2.8.10.2.tar.gz

2:编译安装

[root@svr110 lnmp+memcached]# cd cmake-2.8.10.2

[root@svr110 cmake-2.8.10.2]# ./bootstrap --prefix=/usr/local/cmake

[root@svr110 cmake-2.8.10.2]# make && make install

3:解压mysql软件包

[root@svr110 lnmp+memcached]# tar -zxf mysql-5.5.13.tar.gz

4:编写mysql运行脚本

[root@svr110 lnmp+memcached]# cd mysql-5.5.13

[root@svr110 mysql-5.5.13]# vim mysql.sh

/usr/local/cmake/bin/cmake    -DCMAKE_INSTALL_PREFIX=/usr/local/mysql    -DSYSCONFDIR=/etc   -DMYSQL_DATADIR=/usr/local/mysql/data  -DMYSQL_TCP_PORT=3306   -DMYSQL_UNIX_ADDR=/tmp/mysqld.sock   -DMYSQL_USER=mysql   -DEXTRA_CHARSETS=all   -DWITH_READLINE=1   -DWITH_SSL=system   -DWITH_EMBEDDED_SERVER=1   -DENABLED_LOCAL_INFILE=1   -DWITH_INNOBASE_STORAGE_ENGINE=1

5:运行脚本,进行编译及安装

[root@svr110 mysql-5.5.13]# sh mysql.sh

[root@svr110 mysql-5.5.13]# make && make install

6:初始化授权库并建立主配置文件

[root@svr110 mysql-5.5.13]# cd /usr/local/mysql/

[root@svr110 mysql]# ./scripts/mysql_install_db --user=mysql

[root@svr110 mysql]# cd /root/lnmp+memcached/mysql-5.5.13

[root@svr110 mysql-5.5.13]# cd support-files/

[root@svr110 support-files]# cp -p my-medium.cnf /etc/my.cnf

7:启动数据库服务,修改密码并测试

[root@svr110 support-files]# /usr/local/mysql/bin/mysqld_safe --user=mysql &

如果mysql如果起不来,执行

chown -R mysql:mysql /usr/local/mysql

/usr/local/mysql/scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data &

进入/usr/local/mysql/data/目录删除ib_logfile0和ib_logfile1文件

[root@svr110 support-files]# /usr/local/mysql/bin/mysqladmin -hlocalhost -uroot password "123"

[root@svr110 support-files]# netstat -auntp | grep 3306

tcp        0      0 0.0.0.0:3306                0.0.0.0:*                   LISTEN      17806/mysqld        

[root@svr110 support-files]# pkill -9 mysqld

[1]+  已杀死               /usr/local/mysql/bin/mysqld_safe --user=mysql

[root@svr110 support-files]# /usr/local/mysql/bin/mysqld_safe --user=mysql &

[root@svr110 support-files]# /usr/local/mysql/bin/mysql -uroot -p

Enter password:

8:给源码mysql设置启动脚本

[root@svr110 support-files]# cp -p mysql.server /etc/init.d/mysqldd

[root@svr110 support-files]# chmod +x /etc/init.d/mysqldd

[root@svr110 support-files]# chkconfig --add mysqldd

[root@svr110 support-files]# chkconfig --list mysqldd

mysqldd        0:关闭1:关闭2:启用3:启用4:启用5:启用6:关闭

[root@svr110 support-files]# /etc/init.d/mysqldd stop

Shutting down MySQL.141129 13:54:25 mysqld_safe mysqld from pid file /usr/local/mysql/data/svr110.tarena.com.pid ended

                                                           [确定]

[1]+  Done                    /usr/local/mysql/bin/mysqld_safe --user=mysql

[root@svr110 support-files]# /etc/init.d/mysqldd restart

MySQL server PID file could not be found!                  [失败]

Starting MySQL..                                           [确定]

四:安装源码php

1:安装扩展

[root@svr110 lnmp+memcached]# tar -zxf mhash-0.9.9.9.tar.gz

[root@svr110 lnmp+memcached]# tar -zxf libiconv-1.13.tar.gz

[root@svr110 lnmp+memcached]# tar -zxf libmcrypt-2.5.8.tar.gz

libiconv-1.13.tar.gz    libiconv-1.13     mhash-0.9.9.9  mhash-0.9.9.9.tar.gz

  libmcrypt-2.5.8    libmcrypt-2.5.8.tar.gz

2:编译安装

[root@svr110 lnmp+memcached]# cd mhash-0.9.9.9

[root@svr110 mhash-0.9.9.9]# ./configure

[root@svr110 mhash-0.9.9.9]# make && make install

[root@svr110 lnmp+memcached]# cd libiconv-1.13

[root@svr110 libiconv-1.13]# ./configure

[root@svr110 libiconv-1.13]# make && make install

[root@svr110 lnmp+memcached]# cd libmcrypt-2.5.8

[root@svr110 libmcrypt-2.5.8]# ./configure

[root@svr110 libmcrypt-2.5.8]# make && make install

[root@svr110 libmcrypt-2.5.8]# cd libltdl/

[root@svr110 libltdl]# ./configure   --with-gmetad --enable-gexec --enable-ltdl-install

[root@svr110 libltdl]# make && make install

3:解压php软件包

[root@svr110 lnmp+memcached]# tar -zxf php-5.4.9.tar.gz

php-5.4.9            php-5.4.9.tar.gz

[root@svr110 lnmp+memcached]# vim /etc/ld.so.conf

include ld.so.conf.d/*.conf

/usr/local/mysql/lib/

/usr/local/mysql/include/

[root@svr110 lnmp+memcached]# ln -sv /usr/local/lib/libmcrypt*     /usr/lib/

[root@svr110 lnmp+memcached]# ln -sv /usr/local/lib/libmhash.*    /usr/lib/

[root@svr110 lnmp+memcached]# ln -sv /usr/local/lib/libiconv.*    /usr/lib/

[root@svr110 lnmp+memcached]# ldconfig

4:编辑php运行脚本

[root@svr110 lnmp+memcached]# cd php-5.4.9

[root@svr110 php-5.4.9]# vim php.sh

./configure --prefix=/usr/local/php5nginx --with-config-file-path=/usr/local/php5nginx/etc  --with-mysql=/usr/local/mysql  --with-mysqli=/usr/local/mysql/bin/mysql_config --with-iconv-dir=/usr/local --with-freetype-dir   --with-jpeg-dir --with-png-dir     --with-zlib    --with-libxml-dir=/usr --enable-xml    --disable-rpath   --enable-bcmath --enable-shmop  --enable-sysvsem --enable-inline-optimization  --with-curl   --with-curlwrappers --enable-mbregex   --enable-fpm  --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

5:运行脚本及编译安装

[root@svr110 php-5.4.9]# sh php.sh

如果提示configure: error: Cannot find ldap libraries in /usr/lib.错误请执行

cp -frp /usr/lib64/libldap* /usr/lib/

[root@svr110 php-5.4.9]# make ZEND_EXTRA_LIBS='-liconv'

[root@svr110 php-5.4.9]# make

[root@svr110 php-5.4.9]# make install

6:创建主配置文件,并创建测试文件

[root@svr110 php-5.4.9]# cp php.ini-production /usr/local/php5nginx/etc/php.ini

[root@svr110 php-5.4.9]# vim /usr/local/nginx/html/test.php

[root@svr110 etc]# ./configure --enable-fpm

7:客户机测试

http//192.168.6.110/test.php    无法显示其内容

五:编译安装nginx + Fast-cgi

1:配置fpm

[root@svr110 php-5.4.9]# cd /usr/local/php5nginx/etc/

[root@svr110 etc]# cp php-fpm.conf.default php-fpm.conf

2:启动fpm服务

cd   php-5.4.9/sapi/fpm

cp init.d.php-fpm /etc/init.d/php-fpm

chmod +x /etc/init.d/php-fpm

/etc/init.d/php-fpm start

netstat  -utnalp | grep :9000

3:修改nginx主配置文件

vim   nginx.conf

http  {

   server    {

       location    /   {

             root   html;

             index   index.php  index.html;

       }

 

       location ~ \.php$ {

            root           html;

           fastcgi_pass   127.0.0.1:9000;

           fastcgi_index  index.php;

           fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;

           include        fastcgi_params;

         }

   }

}

vim    fastcgi_params

fastcgi_param  SCRIPT_FILENAME      $document_root$fastcgi_script_name;

4:重新加载配置文件

kill  -HUP  `cat  /usr/local/ngin/logs/nginx.pid`

5:进行本地回环测试

elinks   --dump

6:测试php能否连接到mysql数据库服务器上

service  mysqldd  status

mysql  -hlocalhost   -uroot -p123

 

[root@svr110 html]# cat linkdb.php

<?php

$linkdb=mysql_connect("localhost","root","123");

if($linkdb){

     echo "ok";

}else{

     echo "no";

};

 

?>

[root@svr110 html]# elinks  --dump http://localhost/linkdb.php   

                             ok

六:lnmp+memcached搭建

1、在nginx服务器本机运行memcached服务使用500m物理内存做网站数据缓存

2:解压软件包

tar  -zxvf libevent-2.0.21-stable.tar.gz

cd libevent-2.0.21-stable

3:进行编译安装

./configure

make

make install

echo "/usr/local/lib/" > /etc/ld.so.conf.d/libevent.conf

ldconfig -v

tar -zxvf memcached-1.4.5.tar.gz

cd memcached-1.4.5

./configure

make

make install

4:配置memcached服务

/usr/local/bin/memcached -u root -m 500m -n 10 -f  2  -d

netstat -utnalp | grep :11211

5:安装php连接memcached服务连接工具

[root@svr110 memcache-2.2.5]# /usr/local/php5nginx/bin/phpize Configuring for:

PHP Api Version:         20100412

Zend Module Api No:      20100525

Zend Extension Api No:   220100525

6:进行编译及安装

./configure    --with-php-config=/usr/local/php5nginx/bin/php-config

--enable-memcache

make

[root@svr110 memcache-2.2.5]# make install

Installing shared extensions:     /usr/local/php5nginx/lib/php/extensions/no-debug-non-zts-20100525/

7:修改php主配置文件

vim  php.ini

extension_dir = "/usr/local/php5nginx/lib/php/extensions/no-debug-no     n-zts-20100525/"

extension  = "memcache.so"

8:重新启动服务

/etc/init.d/php-fpm stop

/etc/init.d/php-fpm start

9:修改nginx主配置文件

vim  nginx.conf

server   {

location / {

            root   html;

            index  index.php index.html index.htm;

            set  $memcached_key  $uri;

            memcached_pass 127.0.0.1:11211;

            default_type text/html;

            error_page 404 @fallback;

        }

 

        location @fallback {

           root   html;

           index  index.php index.html index.htm;

        }

     location ~ \.php$ {

            root           html;

            fastcgi_pass   127.0.0.1:9000;

            fastcgi_index  index.php;

            fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;

            include        fastcgi_params;

        }

 }

10:重新加载配置文件

kill  -HUP  `cat  /usr/local/ngin/logs/nginx.pid`

11:客户机进行测试

[root@svr100 php-5.4.9]# elinks --dump http://localhost/likdb.php