實驗01:LNMP平台的搭建
實驗目標: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 --dumphttp://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 http://localhost/test.php
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 --dumphttp://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 --dumphttp://localhost/likdb.php