Centos6.4(32位)LNMP的安装与配置
一、安装前装备工作:
1. 安装编译工具及相关的软件包
yum -y install gcc gcc-c++ autoconf libjpeglibjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-develzlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncursesncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel libidn libidn-developenssl openssl-devel openldap openldap-devel openldap-clients openldap-serversmake gd gd-devel
2.安装下载工具及CMAKE编译器
yum -y install wget cmake
3.安装crond服务
yum -y install vixie-cron
service crond start
chkconfig –add crond
chkconfig crond on
crontab –e
00 00 * * * /usr/sbin/ntpdate ntp.api.bz>> /dev/null 2>&1
4.时间校正
yum -y install ntp
二、下载所需软件包
ImageMagick-6.8.6-9.tar.bz2
eaccelerator-eaccelerator-42067ac.tar.gz
imagick-3.1.0.tgz
libiconv-1.14.tar.gz
libmcrypt-2.5.8.tar.bz2
mcrypt-2.6.8.tar.gz
memcache-3.0.8.tgz
mhash-0.9.9.9.tar.bz2
mysql-5.6.13.tar.gz
nginx-1.5.4.tar.gz
pcre-8.33.tar.bz2
php-5.3.27.tar.bz2
php-5.2.17-fpm-0.5.14.diff.gz
三、解包安装,要注意安装的顺序
1.安装libiconv
tar zxvf libiconv-1.14.tar.gz
cd libiconv-1.14
./configure --prefix=/usr/local
make
make install
cd ../
rm -rf libiconv-1.14
2.安装libmcrypt
tar jxvf libmcrypt-2.5.8.tar.bz2
cd libmcrypt-2.5.8
./configure
make
make install
/sbin/ldconfig
cd libltdl
./configure --enable-ltdl-install
make
make install
cd ../../
rm -rf libmcrypt-2.5.8
3.安装mhash
tar jxvf mhash-0.9.9.9.tar.bz2
cd mhash-0.9.9.9
./configure
make
make install
cd ../
rm -rf mhash-0.9.9.9
4.安装mcrypt
ln -s /usr/local/lib/libmcrypt.* /usr/lib/
ln -s /usr/local/lib/libmhash.* /usr/lib/
tar zxvf mcrypt-2.6.8.tar.gz
cd mcrypt-2.6.8
/sbin/ldconfig
./configure
make
make install
cd ../
rm -rf mcrypt-2.6.8
5.安装Mysql
groupadd mysql
useradd -r -g mysql mysql
tar zxvf mysql-5.6.13.tar.gz
cd mysql-5.6.13
cmake .
make
make install
cd /usr/local/mysql
chown -R mysql .
chgrp -R mysql .
scripts/mysql_install_db --user=mysql
chown -R root .
chown -R mysql data
cp support-files/my-default.cnf /etc/my.cnf
bin/mysqld_safe --user=mysql&
cp support-files/mysql.server/etc/init.d/mysqld
service mysqld restart
6.安装php
tarjxvf php-5.3.27.tar.bz2
gzip -cd php-5.2.17-fpm-0.5.14.diff.gz| patch -d php-5.3.27 -p1
cd php-5.3.27
./configure --prefix=/usr/local/php--with-config-file-path=/usr/local/php/etc
--enable-fpm --enable-bcmath --enable-calendar--with-curl --with-curlwrapper
s --with-gd=/usr --with-jpeg-dir=/usr--with-png-dir=/usr --with-zlib-dir --wi
th-xpm-dir --with-freetype-dir=/usr--enable-gd-native-ttf --with-mhash --enab
le-mbstring --with-mcrypt--with-mysql=/usr/local/mysql --with-pdo-mysql=/usr/
local/mysql --enable-shmop --enable-soap--enable-sysvmsg --enable-sysvsem --e
nable-sysvshm --enable-zip --with-xsl--with-libxml-dir --enable-sockets --wit
h-openssl-dir --with-xmlrpc --enable-pcntl--with-mysqli=/usr/local/mysql/bin/
mysql_config --enable-embedded-mysqli--enable-ftp --with-ldap --with-ldap-sas
l --with-iconv=/usr/local
make ZEND_EXTRA_LIBS='-liconv'
make install
cp php.ini-production/usr/local/php/etc/php.ini
cd ../rm -rf php-5.3.27
注:从这一步开始是安装PHP的扩展模块
7.安装memcache
tar zxvf memcache-3.0.8.tgz
cd memcache-3.0.8
/usr/local/php/bin/phpize
./configure--with-php-config=/usr/local/php/bin/php-config
make
make install
cd ../
rm -rf memcache-3.0.8
8.安装eaccelerator,主要用于php加速
tar zxvfeaccelerator-eaccelerator-42067ac.tar.gz
cd eaccelerator-eaccelerator-42067ac
/usr/local/php/bin/phpize
./configure --enable-eaccelerator=shared--with-php-config=/usr/local/php/bin/
php-config
make
make install
cd ../
rm -rf eaccelerator-eaccelerator-42067ac
9.安装ImageMagick
tar jxvf ImageMagick-6.8.6-9.tar.bz2
cd ImageMagick-6.8.6-9
./configure --prefix=/usr/local/ImageMagick--enable-shared --with-modules
make
make install
cd ../
rm -rf ImageMagick-6.8.6-9
10.安装imagick
ln -s/usr/local/ImageMagick/include/ImageMagick-6 /usr/local/ImageMagick/incl
ude/ImageMagick
tar zxvf imagick-3.1.0RC2.tgz
cd imagick-3.1.0
exportPKG_CONFIG_PATH=/usr/local/ImageMagick/lib/pkgconfig
source /etc/profile
/usr/local/php/bin/phpize
./configure--with-php-config=/usr/local/php/bin/php-config --with-imagick=/us
r/local/ImageMagick
make
make install
cd ../
rm -rf imagick-3.1.0
11.安装pcre
tar jxvf pcre-8.33.tar.bz2
cd pcre-8.33
./configure
make
make install
cd ../
rm -rf pcre-8.33
12.安装nginx
groupadd www
useradd –g www www
tar zxvf nginx-1.5.4.tar.gz
cd nginx-1.5.4
./configure --prefix=/usr/local/nginx--user=www --group=www --with-http_stub_
status_module --with-http_ssl_module
make
make install
cd ../
rm -rf nginx-1.5.4
四、相关的配置文件:php.ini 、php-fpm.conf,、nginx.conf、vhost.conf 、fastcgi.conf
1.配置php.ini
#vi/usr/local/php/etc/php.ini
查找extension_dir=”./”,并其将修改,内容如下:
extension_dir = "extension_dir ="/usr/local/php/lib/php/extensions/no-debug-n
on-zts-20090626/"
extension = "memcache.so"
extension = "imagick.so""
查找output_buffering,并修改,其内容如下:
output_buffering = On
查找short_open_tag,并修改,其内容如下:
short_open_tag=On,
查找cgi.fix_pathinfo,并修改,其内容如下:
cgi.fix_pathinfo=0
作用是防止Nginx文件类型错误解析漏洞
配置eaccelerator加速PHP,在文件末尾添加如下配置信息,
[eaccelerator]
zend_extension="/usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/eaccelerator.so"
eaccelerator.shm_size="64"
eaccelerator.cache_dir="/usr/local/eaccelerator_cache"
eaccelerator.enable="1"
eaccelerator.optimizer="1"
eaccelerator.check_mtime="1"
eaccelerator.debug="0"
eaccelerator.filter=""
eaccelerator.shm_max="0"
eaccelerator.shm_ttl="3600"
eaccelerator.shm_prune_period="3600"
eaccelerator.shm_only="0"
eaccelerator.compress="1"
创建eaccelerator_cache目录
#mkdir –p /usr/local/eaccelerator_cache
#chown –R www:www /usr/local/eaccelerator_cache
2.配置php-fpm.conf
#more /usr/local/php/etc/php-fpm.conf
[global]
pid = /usr/local/php/var/run/php-fpm.pid
error_log = /usr/local/php/var/log/php-fpm.log
log_level = notice
[www]
user = www
group = www
listen = 127.0.0.1:9000
pm = dynamic
pm.max_children = 35
pm.start_servers = 20
pm.min_spare_servers = 5
pm.max_spare_servers = 35
pm.max_requests = 500
3.配置nginx.conf
#more /usr/local/nginx/conf/nginx.conf
userwww;
worker_processes2;
error_log/data/logs/error.log;
pid/usr/local/nginx/logs/nginx.pid;
events {
use epoll;
worker_connections65535;
}
worker_rlimit_nofile 65535;
http {
includemime.types;
default_typeapplication/octet-stream;
include vhosts/*.conf;
log_formatmain'$remote_addr - $remote_user [$time_local]"$request" '
'$status $body_bytes_sent"$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log/data/logs/access.logmain;
sendfileon;
tcp_nopushon;
tcp_nodelayon;
keepalive_timeout60;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 8m;
client_header_timeout 3m;
client_body_timeout 3m;
send_timeout 3m;
output_buffers 1 32k;
postpone_output 1460;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size128k;
gzipon;
gzip_min_length1k;
gzip_buffers4 16k;
gzip_http_version1.0;
gzip_comp_level2;
gzip_typestext/plain application/x-javascripttext/css application/xml;
gzip_vary on;
}
4.配置nginx虚拟主机host.conf
#mkdir /usr/local/nginx/conf/vhosts
#more/usr/local/nginx/conf/vhosts/host.conf
server {
listen80;
server_namelocalhost;
#charset koi8-r;
access_log/data/logs/host.access.logcombined;
location / {
root/data/web;
indexindex.html index.htmindex.php;
}
error_page404/404.html;
# redirect server error pages to the static page /50x.html
#
error_page500 502 503 504/50x.html;
location = /50x.html {
roothtml;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
root/data/web;
fastcgi_pass127.0.0.1:9000;
fastcgi_indexindex.php;
fastcgi_paramSCRIPT_FILENAME/data/web$fastcgi_script_name;
includefastcgi_params;
}
}
5.配置fastcgi.conf
#vi /usr/local/nginx/conf/fastcgi.conf
将此行
fastcgi_paramSCRIPT_FILENAME$document_root$fastcgi_script_name;
内容修改为如下:
fastcgi_paramSCRIPT_FILENAME/data/web$fastcgi_script_name;
原因是:/data/web为本实例的网站根目录
五、检查配置文件
1.可以通过如下名令来,检查配置文件语法是否正确
#/usr/local/php/sbin/php-fpm -t
[21-Sep-2013 17:08:46] NOTICE:configuration file /usr/local/php/etc/php-fpm.conf test is successful
#/usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.confsyntax is ok
nginx: configuration file/usr/local/nginx/conf/nginx.conf test is successful
六、启动Mysqld、php-fpm、nginx
1.启动Mysql
#service mysqld start
#lsof –i:3306
COMMANDPIDUSERFDTYPE DEVICE SIZE/OFF NODE NAME
mysqld1998 mysql15uIPv6144800t0TCP *:mysql (LISTEN)
2.启动php-fpm
#/usr/local/php/sbin/php-fpm --fpm-config=/usr/local/php/etc/php-fpm.conf
查看php-fpm进程
# lsof -i:9000
COMMANDPID USERFDTYPE DEVICE SIZE/OFF NODE NAME
php-fpm 2026 root7uIPv4 146090t0TCP localhost:cslistener (LISTEN)
php-fpm 2027www0uIPv4146090t0TCP localhost:cslistener(LISTEN)
#ps aux|grep php-fpm
root20260.00.5 1113922660 ?Ss14:340:00 php-fpm: master process (/usr/local/php/etc/php-fpm.conf)
www20270.00.5 1116522636 ?S14:340:00 php-fpm: pool
3.启动nginx
#/usr/local/nginx/sbin/nginx
查看nginx进程
ps aux|grep nginx
root20480.00.17276900 ?Ss14:350:00 nginx: master process/usr/local/nginx/sbin/nginx
www21330.02.720332 13976 ?S14:470:00 nginx: worker process
www21340.02.720332 13904 ?S14:470:00 nginx: worker process
Nginx的停止、重启命令
#/usr/local/nginx/sbin/nginx -s [stop | quit| reopen | reload]四个选项
至此LNMP安装与配置结束
七、测试
#vi /data/web/index.php
<?php
phpinfo();
?>
在windowns-xp客户机上的浏览器输入:如能看到如下图所示,则说明配置正确
在windowns-xp客户机上的浏览器输入: