LNMP环境的搭建配置(CentOS6.2+nginx1.1.15+mysql5.6+php5.3.10)

LNMP环境的搭建配置(CentOS6.2+nginx1.1.15+mysql5.6+php5.3.10)

一、安装前的准备

关闭防火墙
   vi /etc/selinux/config
   #SELINUX=enforcing       #注释掉
   #SELINUXTYPE=targeted    #注释掉
   SELINUX=disabled         #增加
   :wq  保存,关闭
   shutdown -r now   #重启系统
   如果不想重启系统,使用命令setenforce 0

1、下载所需要的安装包
Nginx 下载: http://nginx.org/download/nginx-1.1.15.tar.gz (当前最新稳定版本)
Mysql下载:
http://mysql.mirrors.pair.com/Downloads/MySQL-5.6/mysql-5.6.4-m7.tar.gz[/url] (测试版)
http://mysql.cs.pu.edu.tw/Downloads/MySQL-5.5/mysql-5.5.21.tar.gz (稳定版,建议下载此彼)
php下载:http://cn.php.net/distributions/php-5.3.10.tar.gz (目前最新的版本是5.4的,但此版本已经是漏洞修复版)
2、首先对环境需要的库和编译工具进行安装
yum -y install gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel libidn libidn-devel openssl openssl-devel openldap openldap-devel nss_ldap openldap-clients openldap-servers


二、安装过程

1、编译安装mysql-5.5.21.tar.gz
1) 创建mysql安装目录及数据存放目录 
# mkdir -p /usr/local/mysql/
# mkdir -p /usr/local/mysql/data

2)创建用户和用户组与赋予数据存放目录权限
# groupadd mysql
# useradd -g mysql mysql
# useradd mysql -M -s /sbin/nologin
# chown mysql.mysql -R /usr/local/mysql/data


3)安装必要的组件
# yum -y install cmake

4)开始编译安装Mysql
#cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_UNIX_ADDR=/usr/local/mysql/data/mysql.sock \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_EXTRA_CHARSETS:STRING=utf8,gbk \
-DWITH_MYISAM_STORAGE_ENGINE=1 \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_MEMORY_STORAGE_ENGINE=1 \
-DWITH_READLINE=1 \
-DENABLED_LOCAL_INFILE=1 \
-DMYSQL_DATADIR=/usr/local/mysql/data \
-DMYSQL_USER=mysql \
-DMYSQL_TCP_PORT=3306

make && make install && make clean



5)初始化数据库
cd /usr/local/mysql
scripts/mysql_install_db --user=mysql
chown -R root  /usr/local/mysql
chown -R mysql /usr/local/mysql/var
chgrp -R mysql /usr/local/mysql
cp support-files/my-medium.cnf /etc/my.cnf
/usr/local/mysql/bin/mysqld_safe --user=mysql &

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


6)配置环境
加入开机启动
# cp support-files/mysql.server /etc/init.d/mysql    
# chmod 755 /etc/init.d/mysql
# chkconfig mysql on  #添加mysql服务,设定mysqld在各等级为on,“各等级”包括2、3、4、5等级
# export PATH=/usr/local/mysql/bin/PATH


7)启动并设置初始密码
# /etc/init.d/mysql start
# mysqladmin -uroot password '123456'


进行再修改密码的语句
> UPDATE mysql.user SET Password = PASSWORD(‘newpwd’) WHERE User = ‘root’;(生新设置密码)
> flush privileges;(刷新权限)


//赋予任何主机访问数据的权限
mysql>GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION;
//使修改生效
mysql>FLUSH PRIVILEGES;



2、 编译安装nginx

1) 添加一个不能登录并且没有主目录的用户
# useradd www -M -s /sbin/nologin

2) 下载必要的组件并安装
cd /usr/local/soft
# wget http://nchc.dl.sourceforge.net/project/pcre/pcre/8.21/pcre-8.21.zip
# unzip pcre-8.21.zip
# cd pcre-8.21
# ./configure
# make && make install && make clean


3)下载编译安装nginx
cd /usr/local/soft
# wget http://nginx.org/download/nginx-1.1.15.tar.gz
# tar zxvf nginx-1.1.15.tar.gz
# cd nginx-1.1.15
#
./configure --user=www --group=www --with-http_stub_status_module --with-http_ssl_module --without-mail_pop3_module --without-mail_imap_module --without-mail_smtp_module --with-pcre=../pcre-8.21 --with-pcre-jit

# make && make install && make clean


注:差错,提示一个 pcre 相关的错误 undefined reference to `pcre_free_study’,经查发现需要增加 pcre 的路径参数

4)启动nginx
# /usr/local/nginx/sbin/nginx
启动后,在浏览器里输入IP,即可查看到:Welcome to nginx!的欢迎界面
# echo  "/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf" >> /etc/rc.local



4 编译安装php
1) 下载安装必要的组件
wget -c http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.14.tar.gz
wget http://blog.s135.com/soft/linux/nginx_php/mcrypt/libmcrypt-2.5.8.tar.gz 或者wget ftp://mcrypt.hellug.gr/pub/crypto/mcrypt/libmcrypt/libmcrypt-2.5.7.tar.gz
wget http://blog.s135.com/soft/linux/nginx_php/mcrypt/mcrypt-2.6.8.tar.gz
wget http://blog.s135.com/soft/linux/nginx_php/memcache/memcache-2.2.5.tgz
wget http://blog.s135.com/soft/linux/nginx_php/mhash/mhash-0.9.9.9.tar.gz
wget http://cn.php.net/distributions/php-5.3.10.tar.gz
wget http://pear.php.net/go-pear.phar

# yum -y install libjpeg-devel libpng-devel
# tar zxvf libiconv-1.14.tar.gz
# cd libiconv-1.14
# ./configure
# make && make install && make clean

# tar -jxvf libmcrypt-2.5.8.tar.bz2
# cd libmcrypt-2.5.8
# ./configure
# make && make install && make clean
# /sbin/ldconfig
# cd libltdl/
# ./configure --enable-ltdl-install
# make && make install && make clean

# cd ../../
# tar zxvf mhash-0.9.9.9.tar.gz
# cd mhash-0.9.9.9
# ./configure
# make && make install && make clean

# cd ../
ln -s /usr/local/lib/libmcrypt.la /usr/lib/libmcrypt.la
ln -s /usr/local/lib/libmcrypt.so /usr/lib/libmcrypt.so
ln -s /usr/local/lib/libmcrypt.so.4 /usr/lib/libmcrypt.so.4
ln -s /usr/local/lib/libmcrypt.so.4.4.8 /usr/lib/libmcrypt.so.4.4.8
ln -s /usr/local/lib/libmhash.a /usr/lib/libmhash.a
ln -s /usr/local/lib/libmhash.la /usr/lib/libmhash.la
ln -s /usr/local/lib/libmhash.so /usr/lib/libmhash.so
ln -s /usr/local/lib/libmhash.so.2 /usr/lib/libmhash.so.2
ln -s /usr/local/lib/libmhash.so.2.0.1 /usr/lib/libmhash.so.2.0.1
ln -s /usr/local/bin/libmcrypt-config /usr/bin/libmcrypt-config
32位系统 ln -s /usr/local/mysql/lib/libmysqlclient.so.18 /usr/lib/
64位系统 ln -s /usr/local/mysql/lib/libmysqlclient.so.18 /usr/lib64/

ps:查看linux是几位的操作系统
file /sbin/init
32位的 32-bit, 如是64位的, 显示的是 64-bit

# tar zxvf mcrypt-2.6.8.tar.gz
# cd mcrypt-2.6.8
# /sbin/ldconfig
#./configure
# make && make install && make clean


# cd ../
2)编译安装php
# tar zxvf php-5.3.10.tar.gz
# cd php-5.3.10
# ./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/lib \
--with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib \
--with-libxml-dir=/usr \
--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 --with-xmlrpc --enable-zip \
--enable-soap  \
--without-pear \
--with-fpm-user=www --with-fpm-group=www \

查看ext/phar/phar.phar 是否存在,不存在则
cp ext/phar/phar.php ext/phar/phar.phar

# make ZEND_EXTRA_LIBS='-liconv'
# make install


备注:在make ZEND_EXTRA_LIBS='-liconv' 这一步时,出错了,先是下面第一个错
①/home/Lnmp/php-5.3.10/sapi/cli/php: error while loading shared libraries: libiconv.so.2: cannot open shared object file: No such file or directory
make: *** [ext/phar/phar.php] 错误 127
②/home/Lnmp/php-5.3.10/sapi/cli/php: error while loading shared libraries: libmysqlclient.so.18: cannot open shared object file: No such file or directory
make: *** [ext/phar/phar.php] 错误 127
find / -name libmysqlclient.so.18
结果是在: /usr/local/mysql/lib/libmysqlclient.so.18
做个链接:
32位系统 ln -s /usr/local/mysql/lib/libmysqlclient.so.18 /usr/lib/
64位系统 ln -s /usr/local/mysql/lib/libmysqlclient.so.18 /usr/lib64/

还报错cannot access `ext/phar/phar.phar': No such file or directory
cp ext/phar/phar.php ext/phar/phar.phar

3) 复制和修改php配置文件
# cp php.ini-production /usr/local/php/lib/php.ini 或是 /usr/local/lib/php.ini
# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
# /usr/local/php/bin/php -ini //测试ini文件是否加载
注:Nginx+PHP整合,在安装时必须启用–enable-fastcgi 和 –enable-fpm

vi /usr/local/nginx/conf/nginx.conf

把下面的注释去掉
        #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;
        #}

并修改为
        location ~ \.php$ {
            root           /usr/local/www;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /usr/local/www$fastcgi_script_name;
            include        fastcgi_params;
        }

ps:/usr/local/www为php文件目录


php-fpm启动
/usr/local/php/sbin/php-fpm
加入开机启动
 vi /etc/rc.d/rc.local


5 环境测试,写一个phpinfo文件测试一下


外网如不能访问,则是因为防火墙没关
1) 重启后生效
开启: chkconfig iptables on
关闭: chkconfig iptables off
2) 即时生效,重启后失效
开启: service iptables start
关闭: service iptables stop


浏览器如显示
File not found.
是因为这一行,php找不到文件
fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;

你可能感兴趣的:(LNMP环境的搭建配置(CentOS6.2+nginx1.1.15+mysql5.6+php5.3.10))