说明:
操作系统:CenOS 6.3 32位
所需安装包及软件可以到这个网址下载 http://soft.7dot.com/
(phpmyadmin 各类库文件等 )
关闭SElinux 防火墙添加允许80 3306端口通过规则
vi /etc/sysconfig/iptables -----给防火墙添加规则
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT #允许80端口通过防火墙
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT #允许3306端口通过防火墙
特别提示:如果这两条规则添加到防火墙配置的最后一行,导致防火墙启动失败,正确的应该是添加到默认的22端口这条规则的下面
service iptables restart ------重启防火墙使配置生效
2. vi /etc/selinux/config -------关闭SELinux
#SELINUX=enforcing -------#注释掉
#SELINUXTYPE=targeted --------#注释掉
SELINUX=disabled ----------#增加
:wq! -------#保存退出
setenforce 0 -------#使配置立即生效
3. 下载软件包
nginx
wget http://nginx.org/download/nginx-1.4.4.tar.gz
pcre(支持nginx伪静态)
wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.34.tar.gz
mysql
wget http://cdn.mysql.com/Downloads/MySQL-5.5/mysql-5.5.35.tar.gz
php
wget http://cn2.php.net/distributions/php-5.3.28.tar.gz
wget http://mirrors.sohu.com/php/php-5.3.28.tar.gz
cmake(mysql编译工具)
wget http://www.cmake.org/files/v2.8/cmake-2.8.12.1.tar.gz
另外在安装php过程中会出现需要GD libmcrypt等库和模块文件,如果在系统查询没有安装,请下载源码包编译安装
GD库(php页面图片验证码支持)
wget http://ring.u-toyama.ac.jp/archives/graphics/gd/gd-2.0.36RC1.tar.gz
libmcrypt(PHPlibmcrypt模块)
wget http://nchc.dl.sourceforge.net/project/mcrypt/Libmcrypt/2.5.8/libmcrypt-2.5.8.tar.gz
附加两个加速php执行效率的组件
Zend Guard(加速php执行效率)
wget http://downloads.zend.com/guard/5.5.0/ZendGuardLoader-php-5.3-linux-glibc23-i386.tar.gz 32位
wget http://downloads.zend.com/guard/5.5.0/ZendGuardLoader-php-5.3-linux-glibc23-x86_64.tar.gz 64位
eaccelerator(加速php执行效率)
wget http://soft.7dot.com/soft/eaccelerator-0.9.6.1.tar.bz2
4. 安装编译工具及库文件
yum install -y make apr* autoconf automake curl curl-devel gcc gcc-c++ gtk+-devel zlib-devel openssl openssl-devel pcre-devel gd kernel keyutils patch perl kernel-headers compat* cpp glibc libgomp libstdc++-devel keyutils-libs-devel libsepol-devel libselinux-devel krb5-devel libXpm* freetype freetype-devel freetype* fontconfig fontconfig-devel libjpeg* libpng* php-common php-gd gettext gettext-devel ncurses* libtool* libxml2 libxml2-devel patch policycoreutils bison
-----------------------------------------------------------------------------------------
安装cmake
tar zxvf cmake-2.8.12.1.tar.gz
cd cmake-2.8.12.1
./configure && make && make install
-----------------------------------------------------------------------------------------
安装pcre
mkdir /usr/local/pcre --------创建安装目录
tar zxvf pcre-8.34.tar.gz
cd pcre-8.34
./configure --prefix=/usr/local/pcre && make && make install
-----------------------------------------------------------------------------------------
安装libmcrypt
tar zxvf libmcrypt-2.5.8.tar.gz
cd libmcrypt-2.5.8
./configure && make && make install
安装GD库
tar zxvf gd-2.0.36RC1.tar.gz
cd gd-2.0.36RC1
mkdir -p /usr/local/gd
./configure --prefix=/usr/local/gd --with-jpeg=/usr/local/jpeg/ --with-png=/usr/local/libpng/ --with-zlib=/usr/local/zlib/ --with-freetype=/usr/local/freetype/
make && make install
-----------------------------------------------------------------------------------------------------------------------
MYSQL安装
tar -zxvf mysql-5.5.25.tar.gz
mkdir -pv /usr/local/mysql/data
cd mysql-5.5.25
cmake \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_DATADIR=/user/local/mysql/data \
-DSYSCONFDIR=/etc \
-DEXTRA_CHARSETS=all \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_FEDERATED_STORAGE_ENGINE=1 \
-DWITH_PARTITION_STORAGE_ENGINE=1 \
-DWITH_PERFSCHEMA_STORAGE_ENGINE=1 \
-DMYSQL_UNIX_ADDR=/tmp/mysqld.sock \
-DMYSQL_TCP_PORT=3306 \
-DWITH_DEBUG=0 \
-DENABLED_LOCAL_INFILE=1
make && make install
//设置Mysql
//在support-files目录中有五个配置信息文件(这里很重要,一定要根据自己的内存复制对应的cnf文件,否则mysql始终起不来):
//my-small.cnf (内存<=64M)
//my-medium.cnf (内存 128M)
//my-large.cnf (内存 512M)
//my-huge.cnf (内存 1G-2G)
//my-innodb-heavy-4G.cnf (内存 4GB)
cd /usr/local/mysql
cp ./support-files/my-huge.cnf /etc/my.cnf
vi /etc/my.cnf
#在 [mysqld] 段修改
max_allowed_packet = 16M
添加mysql用户和用户组,生成新的mysql授权表
//添加mysql运行的用户和用户组
#groupadd mysql
# useradd -g mysql -s /sbin/nologin mysql
#chmod +x /usr/local/mysql
#chown -R mysql:mysql /usr/local/mysql
#chown -R mysql:mysql /usr/local/mysql/data
//生成新的mysql授权表
//进入mysql安装目录下的脚本目录
#cd /usr/local/mysql/scripts
//利用mysql_install_db脚本生成新的mysql授权表
#./mysql_install_db --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --user=mysql
//mysql server在系统中的服务项设置
//复制服务文件并修改
#cd /usr/local/mysql/support-files
#cp mysql.server mysqld
//修改mysqld
#vim mysqld
basedir=/usr/local/mysql
datadir=/usr/local/ysql/data
#mv mysqld /etc/init.d/mysqld
#chmod 755 /etc/init.d/mysqld
//设置软连接使mysql, mysqldump, mysqladmin这三个bin命令能在shell中直接运行
#ln -s /usr/local/mysql/bin/mysql /usr/bin
#ln -s /usr/local/mysql/bin/mysqldump /usr/bin
#ln -s /usr/local/mysql/bin/mysqladmin /usr/bin
启动mysql
#/etc/init.d/mysqld start
Starting MySQL.. SUCCESS! 出现这个就是启动成功的意思
NGINX安装
grupadd www -----添加www组
useradd -g www www -s /sbin/nologin ---创建nginx账户并加入www组,不允许此用户登录系统
mkdir -p /usr/local/nginx -----创建安装目录
tar zxvf nginx-1.4.4.tar.gz -----解压
cd nginx-1.4.4
./configure --prefix=/usr/local/nginx --without-http_memcached_module --user=www --group=www --with-http_stub_status_module --with-openssl=/usr/ --with-pcre=/root/pcre-8.34
注意:--with-pcre=/root/pcre-8.34指向的是源码包解压的路径,而不是安装的路径,否则会报错
最后一行出现 creating objs/Makefile 证明检测成功生成了makefile文件
make ------只要没出现error错误就继续make install
/usr/local/nginx/sbin/nginx -----启动nginx
vim /etc/rc.d/init.d/nginx -------编辑文件设置nginx开机启动
#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon
#
# chkconfig: - 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /etc/nginx/nginx.conf
# config: /usr/local/nginx/conf/nginx.conf
# pidfile: /usr/local/nginx/logs/nginx.pid
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
nginx="/usr/local/nginx/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
lockfile=/var/lock/subsys/nginx
make_dirs() {
# make required directories
user=`$nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
if [ -z "`grep $user /etc/passwd`" ]; then
useradd -M -s /bin/nologin $user
fi
options=`$nginx -V 2>&1 | grep 'configure arguments:'`
for opt in $options; do
if [ `echo $opt | grep '.*-temp-path'` ]; then
value=`echo $opt | cut -d "=" -f 2`
if [ ! -d "$value" ]; then
# echo "creating" $value
mkdir -p $value && chown -R $user $value
fi
fi
done
}
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
make_dirs
echo -n $"Starting $prog: "
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}
stop() {
echo -n $"Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}
restart() {
#configtest || return $?
stop
sleep 1
start
}
reload() {
#configtest || return $?
echo -n $"Reloading $prog: "
killproc $nginx -HUP
RETVAL=$?
echo
}
force_reload() {
restart
}
configtest() {
$nginx -t -c $NGINX_CONF_FILE
}
rh_status() {
status $prog
}
rh_status_q() {
rh_status >/dev/null 2>&1
}
case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit 2
esac
=======================================================
:wq! #保存退出
chmod 775 /etc/rc.d/init.d/nginx -------- 赋予文件执行权限
chkconfig nginx on --------设置开机启动
/etc/rc.d/init.d/nginx restart --------重新启动Nginx
service nginx restart
=======================================================
PHP安装
tar zxvf php-5.3.28.tar.gz -----解压
cd php-5.3.28
mkdir -p /usr/local/php ----新建安装目录
./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-mysql-sock=/tmp/mysql.sock --with-pdo-mysql=/usr/local/mysql --with-gd=/usr/local/gd --with-png-dir=/usr/lib --with-jpeg-dir=/usr/lib --with-freetype-dir=/usr/lib --with-iconv --with-zlib --enable-xml --enable-magic-quotes --enable-safe-mode --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curlwrappers --enable-mbregex --enable-fpm --enable-mbstring --enable-ftp --enable-gd-native-ttf --with-openssl --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --without-pear --with-gettext --enable-session --with-mcrypt --with-curl
下面列举的是安装PHP的常用错误,如果我们一开始就安装好了下面两项就不会出现
---------------------------------------------------------------------------------
出现错误:
configure: error: Unable to find gd.h anywhere under /usr/local/gd
系统中已经有GD了,把--with-gd后面留空,就可以顺利configure了,上面的命令,我已经去掉了―with-gd的值,所以应该不会出这个error了。仔细想想好像我装系统时确实把图形那个组件给点上了!
去掉--with-gd 选项后再次执行
又出现错误:
configure: error: mcrypt.h not found. Please reinstall libmcrypt.
缺少libmcrypt 这个模块,用yum安装发现源里面没有,下面我们下载源码编译安装
---------------------------------------------------------------------------------------
再次./configure
Configure成功后提示”Thank you for using PHP.”.
make && make install
cp php.ini-production /usr/local/php/etc/php.ini ------复制php配置文件到安装目录
rm -rf /etc/php.ini ---删除系统自带的配置文件
ln -s /usr/local/php/etc/php.ini /etc/php.ini -----添加软链接
cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf ----拷贝模板为php-fpm配置文件
vim /usr/local/php/etc/php-fpm.conf
修改
user = www -----设置php-fpm运行账户为www
group = www ----设置php-fpm运行组为www
pid = run/php-fpm.pid ----取消前面的分号注释
cp php-5.3.28/sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm ----拷贝php-fpm到启动目录
chmod +x /etc/rc.d/init.d/php-fpm ----添加执行权限
chkconfig php-fpm on -----设置开机启动
service php-fpm start ------启动
vi /usr/local/php/etc/php.ini
找到:disable_functions =
修改为:disable_functions = passthru,exec,system,chroot,scandir,chgrp,chown,shell_exec,proc_open,proc_get_status,ini_alter,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,stream_socket_server,escapeshellcmd,dll,popen,disk_free_space,checkdnsrr,checkdnsrr,getservbyname,getservbyport,disk_total_space,posix_ctermid,posix_get_last_error,posix_getcwd, posix_getegid,posix_geteuid,posix_getgid, posix_getgrgid,posix_getgrnam,posix_getgroups,posix_getlogin,posix_getpgid,posix_getpgrp,posix_getpid, posix_getppid,posix_getpwnam,posix_getpwuid, posix_getrlimit, posix_getsid,posix_getuid,posix_isatty, posix_kill,posix_mkfifo,posix_setegid,posix_seteuid,posix_setgid, posix_setpgid,posix_setsid,posix_setuid,posix_strerror,posix_times,posix_ttyname,posix_uname
#列出PHP可以禁用的函数,如果某些程序需要用到这个函数,可以删除,取消禁用
找到:;date.timezone =
修改为:date.timezone = PRC #设置时区
找到:expose_php = On
修改为:expose_php = OFF #禁止显示php版本的信息
找到:short_open_tag = Off
修改为:short_open_tag = ON #支持php短标签
service php-fpm restart -----重启php
配置NGINX支持PHP
vim /usr/local/nginx/conf/nginx.conf
user www www; #首行user去掉注释,修改Nginx运行组为www www;必须与/usr/local/php/etc/php-fpm.conf中的user,group配置相同,否则php运行出错
index index.php index.html index.htm; #添加index.php
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
#取消FastCGI server部分location的注释,注意fastcgi_param行的参数,改为$document_root$fastcgi_script_name,或者使用绝对路径
service nginx restart ----重启nginx
配置PHP支持Zend Guard
安装Zend Guard加速php执行效率
mkdir /usr/local/zend
tar zxvf ZendGuardLoader-php-5.3-linux-glibc23-i386.tar.gz
cd ZendGuardLoader-php-5.3-linux-glibc23-i386
cp php-5.3.x/ZendGuardLoader.so /usr/local/zend/ -----拷贝文件到安装目录
vim /usr/local/php/etc/php.ini
在最后位置添加以下内容
[Zend Guard]
zend_extension=/usr/local/zend/ZendGuardLoader.so
zend_loader.enable=1
zend_loader.disable_licensing=0
zend_loader.obfuscation_level_support=3
zend_loader.license_path=
安装eaccelerator加速php执行效率
tar jxf eaccelerator-0.9.6.1.tar.bz2 -----解压
cd eaccelerator-0.9.6.1
/usr/local/php/bin/phpize ----用phpize生成configure配置文件
./configure -enable-eaccelerator=shared --with-php-config=/usr/local/php/bin/php-config
make && make install
make install 后记住后面的路径 /usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/ -----eaccelerator模块路径
mkdir /tmp/eaccelerator #创建目录
chmod 777 /tmp/eaccelerator #设置目录权限为完全控制
vi /usr/local/php/etc/php.ini #编辑配置文件,在ZendGuardLoader配置部分前添加以下内容
[eaccelerator]
zend_extension="/usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/eaccelerator.so"
eaccelerator.cache_dir="/tmp/eaccelerator"
eaccelerator.shm_size="8"
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.compress_level="9"
eaccelerator.keys ="disk_only"
eaccelerator.sessions ="disk_only"
eaccelerator.content ="disk_only"
备注:如果配置文件中已经有了ZendGuardLoader等配置信息,为了避免冲突,需要把上面的配置信息写在ZendGuardLoader配置之前
现在LNMP已经全部搭建完成了,下面进行测试
cd /usr/local/nginx/html/ --------进入nginx默认网站根目录
rm -rf * --------删除默认测试页
vi index.php ------新建index.php文件
<?php
phpinfo();
?>
:wq! #保存退出
chown www.www /usr/local/nginx/html/ -R #设置目录所有者
chmod 700 /usr/local/nginx/html/ -R #设置目录权限
在本地浏览器中输入localhost 我们会看到php版本的信息
本文出自 “不变的时光---胡” 博客,转载请与作者联系!