LNMP环境搭建
Centios6.2 64位
(可选步骤:建议centos5.6上不做此步骤
在64位的 CentOS 上yum 只安装64位的软件包
#vim /etc/yum.conf
[main]
cachedir=/var/cache/yum
keepcache=0
……##加一行那可##
exclude=*.i?86
……
exclude选项支持正则表达式,*.i?86可以用来过滤掉i386,i686等32bit包了
如果你已经安装了一些32位的软件包你也可以以下命令删除哦 #yum remove \*.i\?86)
selinux
A 不需要重启Linux:
[root@localhost ~]# setenforce 0
B 需要重启Linux:
vi /etc/selinux/config 将SELINUX=enforcing 改成SELINUX=disabled
查看iptables状态:
service iptables status
iptables开机自动启动:
开启: chkconfig iptables on
关闭: chkconfig iptables off
iptables关闭服务:
开启: service iptables start
关闭: service iptables stop
系统文件最大值修改
在/etc/security/limits.conf中增加
* soft nproc 102400
* hard nproc 102400
* soft nofile 102400
* hard nofile 102400
做这之前最好是重启下ssh服务,servicesshd restart 最后
说明:* 代表针对所有用户,nproc是代表最大进程数,nofile 是代表最大文件打开数.修改了limits.conf文件之后,要让它立即生效,只需关闭当前session,重新连接即可
一.下载所需工具
#依赖库和开发工具
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 krb5 krb5-devel libidnlibidn-devel openssl openssl-devel openldap openldap-devel nss_ldapopenldap-clients openldap-servers libevent libevent-devel gd 共82个
#Nginx
yum -y install pcre-devel zlib-devel
#Php
yum -y install gd-devel libjpeg-devel libpng-devel freetype-devellibxml2-devel curl-devel freetype-devel
#Mysql
yum -y install bison gccgcc-c++ autoconf automake zlib* libxml* ncurses-devel libtool-ltdl-devel*
二.安装Mysql
1.安装相关库文件(没有的话就安装,有就不用安装了)
2.安装cmake包
# wget http://wwwNaNake.org/files/v2.8/cmake-2.8.6.tar.gz
# tar zxvf cmake-2.8.6.tar.gz
# cd cmake-2.8.6/
# ./configure
# gmake
# gmake install
3.添加用户和创建数据库目录和权限设置
groupadd mysql
useradd -g mysql mysql
# mkdir -p /data/mysql
# chown -R mysql:mysql /data/mysql
4.下载
wgethttp://downloads.mysql.com/archives/mysql-5.5/mysql-5.5.16.tar.gz
# tar -zxvf mysql-5.5.16.tar.gz
# cd mysql-5.5.16
# cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql-DMYSQL_DATADIR=/data/mysql -DSYSCONFDIR=/etc/
# gmake
# gmake install
文件关联
cd/usr/local/mysql/lib
ln-s /usr/local/mysql/lib/libmysqlclient.so.18/usr/lib/libmysqlclient.so.18 # (32Bit)
ln -s /usr/local/mysql/lib/libmysqlclient.so.18/usr/lib64/libmysqlclient.so.18 #(64Bit)
5.设置mysql配置文件
# cd /usr/local/mysql
# cp ./support-files/my-huge.cnf /etc/my.cnf
覆盖 yes
6.编辑/etc/my.cnf
# vi /etc/my.cnf
在 [mysqld] 段增加
datadir = /data/mysql
wait-timeout = 30
max_connections = 512
max_connect_errors = 10000000
default-storage-engine = MyISAM
在 [mysqld] 段修改
max_allowed_packet = 16M
7.初始化数据库
# ./scripts/mysql_install_db --datadir=/data/mysql--basedir=/usr/local/mysql --defaults-file=/etc/my.cnf --user=mysql
# cp ./support-files/mysql.server /etc/rc.d/init.d/mysqld
# chmod 755 /etc/rc.d/init.d/mysqld
# chkconfig --add mysqld
chkconfig --level 345 mysqld on
8.设置启动脚本变量
编辑 /etc/rc.d/init.d/mysqld
# vi /etc/rc.d/init.d/mysqld
basedir=/usr/local/mysql
datadir=/data/mysql
9.设置变量环境
# echo "PATH=$PATH:/usr/local/mysql/bin">> /etc/profile (永久生效,重起服务器)
# export PATH=$PATH:/usr/local/mysql/bin (当前生效,不用重起服务器)
10.启动服务
# service mysqld start
MySQL server PID file could not befound! [失败]
Starting MySQL.The server quit withoutupdating PID file (/var/mysql/data/vm-1.pid). [失败]
1,查看mysql日志(在/var/lib/mysql目录下有个err文件的
2,打开/etc/selinux/config,把SELINUX=enforcing改为SELINUX=disabled后存盘退出重启机器
********************************************************************:
11.设置数据库管理员root密码
# mysqladmin -u root password '123456'
问题: [root@vm-1 local]#mysqladmin -u root password '123456'
执行# mysql -u root -p 报下列错误
bash: mysqladmin: command not found
处理: # exportPATH=$PATH:/usr/local/mysql/bin
******************************************************************************
# service mysqld restart
12.进入Mysql
# mysql -u root -p
三.安装PHP
1.下载所需软件包
wget http://sourceforge.net/projects/mcrypt/files/MCrypt/2.6.8/mcrypt-2.6.8.tar.gz/download
wget http://sourceforge.net/projects/mhash/files/mhash/0.9.9.9/mhash-0.9.9.9.tar.gz/download
wget http://sourceforge.net/projects/mcrypt/files/Libmcrypt/2.5.8/libmcrypt-2.5.8.tar.gz/download
wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.14.tar.gz
wget http://php.net/get/php-5.3.8.tar.bz2/from/ca1.php.net/mirror
2.编译安装PHP 5.3.8所需的支持库:
##2.1步,按顺序安装
(加强系统对支持字符编码转换的功能)
tar zxvflibiconv-1.14.tar.gz && cd libiconv-1.14/
./configure--prefix=/usr/local
make&& make install && cd ../
## 2.2首先安装libmcrypt
(加密算法库,PHP扩展mcrypt功能对此库有依耐关系,要使用mcrypt必须先安装此库)
tar zxvflibmcrypt-2.5.8.tar.gz && cd libmcrypt-2.5.8/
./configure&& make && make install
/sbin/ldconfig&& cd libltdl/ && ./configure --enable-ltdl-install
make&& make install && cd ../../
## 2.3接着安装mhash
tar zxvfmhash-0.9.9.9.tar.gz && cd mhash-0.9.9.9/ && ./configure
make&& make install && cd ../
## 2.3最后安装mcrypt
tar zxvfmcrypt-2.6.8.tar.gz &&cd mcrypt-2.6.8/
/sbin/ldconfig
exportLD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH
./configure
make&& make install && cd ../
tar jxvf php-5.3.8.tar.bz2&& cd php-5.3.8
cp -frp /usr/lib64/libldap* /usr/lib/
定义模块 (蓝色一次性输入)
./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-iconv-dir=/usr/local/--with-freetype-dir \
--with-jpeg-dir--with-png-dir --with-zlib \
--with-mhash--enable-sockets --enable-ftp \
--with-libxml-dir--enable-xml --disable-rpath \
--enable-safe-mode--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 \
--without-pear --enable-fpm \
--with-xmlrpc--enable-zip --enable-soap
makeZEND_EXTRA_LIBS='-liconv'
makeinstall
cpphp.ini-development /usr/local/php/etc/php.ini
wget http://fastlnmp.googlecode.com/files/PDO_MYSQL-1.0.2.tgz
tar zxvf PDO_MYSQL-1.0.2.tgz
cd PDO_MYSQL-1.0.2/
/usr/local/php/bin/phpize
./configure--with-php-config=/usr/local/php/bin/php-config--with-pdo-mysql=/usr/local/mysql
make
make install
/usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/
cd ../
修改php.ini文件
手工修改:查找/usr/loca/php/etc/php.ini中的extension_dir = "./"
修改为extension_dir= "/usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/"
红色部分为上面make install下面出现的内容
并在此行后增加以下几行,然后保存:
extension ="pdo_mysql.so"
再查找output_buffering= Off
修改为output_buffering= On 134行
output_buffering
Default Value: On
Development Value: 4096
Production Value: 4096
3.添加WWW用户
#/usr/sbin/groupadd www && /usr/sbin/useradd-g www www
#mkdir -p /var/log/nginx && chmod+w /var/log/nginx &&chown -R www:www /var/log/nginx
#mkdir -p /data/www && chmod +w/data/www && chown -R www:www /data/www
#cp /usr/local/php/etc/php-fpm.conf.default/usr/local/php/etc/php-fpm.conf
4.编辑php-fpm.conf
vi/usr/local/php/etc/php-fpm.conf
#去掉前面注释号”;”/更改后面参数配置文件中的;
[global]
pid = run/php-fpm.pid
error_log = log/php-fpm.log
emergency_restart_interval = 1m
#表示在emergency_restart_interval所设值内出现SIGSEGV或者SIGBUS错误的php-cgi进程数如果超过 emergency_restart_threshold个,php-fpm就会优雅重启。这两个选项一般保持默认值。
process_control_timeout = 5s
#设置子进程接受主进程复用信号的超时时间. 可用单位: s(秒), m(分), h(小时), 或者 d(天) 默认单位: s(秒). 默认值: 0
pm.max_children= 64
pm.start_servers = 20
pm.min_spare_servers = 5
保证空闲进程数最小值,如果空闲进程小于此值,则创建新的子进程
pm.max_spare_servers = 35
保证空闲进程数最大值,如果空闲进程大于此值,此进行清理
对于专用服务器,pm可以设置为static。
pm.max_requests = 1024
设置每个子进程重生之前服务的请求数. 对于可能存在内存泄漏的第三方模块来说是非常有用的. 如果设置为 ’0′ 则一直接受请求. 等同于 PHP_FCGI_MAX_REQUESTS 环境变量. 默认值: 0.
user = www
group = www
/usr/local/php/sbin/php-fpm -t
#出现NOTICE:configuration file /usr/local/php/etc/php-fpm.conf test is successful 测试成功
php-fpm日志文件及pid在/usr/local/php/var/下
测试启动
/usr/local/php/sbin/php-fpm&
测试成功[1] 29528
测试后killall php-fpm (否则正式启动报错)
#设置开机启动
vi /etc/rc.d/rc.local
#在行末加入
/usr/local/php/sbin/php-fpm
正式启动
#/usr/local/php/sbin/php-fpm ========启动php-fpm
出错:
ALERT: [pool www] pm.min_spare_servers(0) must be a positive value
编辑php-fpm.conf找到pm.min_spare_servers 去除;号,注意非注释内容pm.min_spare_servers
ALERT: [pool www] pm.max_spare_servers(0) must be a positive value
编辑php-fpm.conf找到pm.max_spare_servers 去除;号,同样非注释内容pm.max_spare_servers
WARNING: [pool www] pm.start_servers is not set. It's been set to 20.
编辑php-fpm.conf找到pm.start_servers 去除;号,同样非注释内容pm.start_servers
ERROR: bind() for address '127.0.0.1:9000' failed: Address already in use (98)
# ps -ef |grep php 查看进程,有N多进程
nobody 26152 26151 0 15:45? 00:00:00 php-fpm: pool www
nobody 26153 26151 0 15:45? 00:00:00 php-fpm: pool www
nobody 26154 26151 0 15:45? 00:00:00 php-fpm: pool www
nobody 26155 26151 0 15:45? 00:00:00 php-fpm: pool www
#killall php-fpm
结束所有php-fpm进程,
再启动php-fpm #/usr/local/php/sbin/php-fpm
四.安装Nginx
1,添加一个不能登录且没有主目录的用户:
2,必要的组件
wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.30.tar.gz
# tar zxvf pcre-8.30.tar.gz
# cd pcre-8.30
# ./configure
# make && make install
3,编译nginx并安装
###yum install openssl openssl-devel(前面装过就不用装了)
wget http://nginx.org/download/nginx-1.0.10.tar.gz
# tar -zxvf nginx-1.0.10.tar.gz
# cd nginx-1.0.10
# ./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_stub_status_module--with-http_ssl_module
# make && make install
4.编辑nginx.conf
vi /usr/local/nginx/conf/nginx.conf
#修改一些参数,别直接替换文件,这只是一部分
user www;
events {
use epoll;
worker_connections 1024;
}
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;
}
#注意这里
#$document_root$fastcgi_script_name;
#检测配置文件
/usr/local/nginx/sbin/nginx -t
#提示表示成功
#nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
#nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
#开启Nginx
/usr/local/nginx/sbin/nginx
#平滑重启Nginx
/usr/local/nginx/sbin/nginx -s reload
#添加开机启动
vi /etc/rc.d/rc.local
#最后移行加入
/usr/local/nginx/sbin/nginx
#测试
cd /usr/local/nginx/html/
touch index.php
vi /usr/local/nginx/html/index.php
service iptables status
Service iptables stop
开启php、nginx,测试即可
/usr/local/nginx/sbin/nginx
/usr/local/php/sbin/php-fpm
http://ip地址
“welcome to nginx!”
http://ip地址/index.php
以下部分暂不用安装
CentOS安装php加速软件Zend Guardhttp://www.mouyao.com/archives/1928.html
说明:PHP5.3以上的版本不再支持Zend Optimizer,已经被全新的 Zend Guard Loader 取代,下面是安装Zend Guard具体步骤,以下操作均在终端命令行执行
关闭SELINUX
vi /etc/selinux/config
#SELINUX=enforcing #注释掉
#SELINUXTYPE=targeted #注释掉
SELINUX=disabled #增加
:wq 保存,关闭
shutdown -r now #重启系统
1、下载Zend Guardcd /home
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位
2、安装Zend Guard
mkdir /usr/local/Zend #建立Zend Guard安装目录
tar xvfz ZendGuardLoader-php-5.3-linux-glibc23-i386.tar.gz #解压安装文件
cpZendGuardLoader-php-5.3-linux-glibc23-i386/php-5.3.x/ZendGuardLoader.so /usr/local/Zend/ #拷贝文件到安装目录
rm -rf /home/ZendGuardLoader-php-5.3-linux-glibc23-i386* #删除安装包
3、配置Zend Guard
cp /etc/php.ini /etc/php.inibak #修改之前先备份
vi /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=
4、重启web服务器
/etc/init.d/httpd restart
至此,Zend Guard安装完成。
mysql权限解决方法
chown -R root /usr/local/mysql
chgrp -R mysql /usr/local/mysql
chown -R root /usr/local/mysql/bin
chgrp -R mysql /usr/local/mysql/bin
chgrp -R mysql /var/lib/mysql
chmod 777 /var/lib/mysql
chown -R root /var/lib/mysql/mysql
chgrp -R mysql /var/lib/mysql/mysql
chmod 777 /var/lib/mysql/mysql
chown -R root /var/lib/mysql/mysql/*
chgrp -R mysql /var/lib/mysql/mysql/*
chmod 777 /var/lib/mysql/mysql/*
chmod 777 /usr/local/mysql/lib/mysql/libmysqlclient.a
下面这步比较关键,
ln -s /usr/local/mysql/lib/mysql /usr/lib/mysql
ln -s /usr/local/mysql/include/mysql /usr/include/mysql
大家可以不做这步,大可以在编译其他软件的时候自定义myslq的库文件路径,但我还是喜欢把库文件链接到默认的位置,这样你在编译类似PHP,Vpopmail等软件时可以不用指定mysql的库文件地址。
5.6.4 安装memcache扩展(已经安装PHP)
cd/usr/local/src/
tar zvxfmemcache-2.2.6.tar.gz
cdmemcache-2.2.6
/opt/php/bin/phpize
ldconfig
./configure--with-php-config=/opt/php/bin/php-config
make&& make install
修改php配置文件,支持memcache
vim/opt/php/etc/php.ini
在文件中搜索extension_dir、extension ,在相应位置添加下面两行
extension_dir ="/opt/php/lib/php/extensions/no-debug-non-zts-20090626/"
extension = "memcache.so"
5.5.4 打开防火墙80端口
写入规则,保存并重启服务
iptables -I INPUT -p tcp --dport 80 -jACCEPT
/etc/rc.d/init.d/iptables save
/etc/init.d/iptables restart
查看防火墙信息
/etc/init.d/iptables status
如果显示以下类似信息,即表示已经打开了80端口
1 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:8
Generating files
updating cache./config.cache
creating ./config.status
creating php5.spec
creatingmain/build-defs.h
creatingscripts/phpize
creatingscripts/man1/phpize.1
creatingscripts/php-config
creatingscripts/man1/php-config.1
creatingsapi/cli/php.1
creatingsapi/fpm/php-fpm.conf
creating sapi/fpm/init.d.php-fpm
creatingsapi/fpm/php-fpm.8
creatingmain/php_config.h
creatingmain/internal_functions.c
creatingmain/internal_functions_cli.c