Centos6.6 LNMP环境搭建(带一键安装shell脚本)
本文最后更新于 2267 天前,其中的信息可能已经有所发展或是发生改变。

上次搭建了LAMP环境,这次来搭建LNMP环境,也就是 Linux+Nginx+Mysql+Php。

有了上次的经验,这一次并不是很难,建议第一次安装的朋友用我给的安装包安装。

具体看操作:

1、安装Mysql数据库(Php要依赖它,Nginx最后安装)

Command:

cd /usr/local/src
wget http://dev.mysql.com/get/Downloads/MySQL-5.1/mysql-5.1.40-linux-i686-icc-glibc23.tar.gz
tar -zxf mysql-5.1.40-linux-i686-icc-glibc23.tar.gz
mkdir -p /usr/local/mysql
mv mysql-5.1.40-linux-i686-icc-glibc23/* /usr/local/mysql/
useradd -s /sbin/nologin mysql
mkdir -p /data/mysql
chown -R mysql:mysql /data/mysql
/usr/local/mysql/scripts/mysql_install_db --user=mysql --datadir=/data/mysql   //如果这里提示错误,就执行这一句:/usr/local/mysql/scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql
cp /usr/local/mysql/support-files/my-large.cnf /etc/my.cnf
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
chmod 755 /etc/init.d/mysqld
sed -i ':a $!N;$!ba;{s/\(datadir=[^\n]*\)\n/\1\/data\/mysql\n/}' /etc/init.d/mysqld
chkconfig --add mysqld
chkconfig mysqld on
service mysqld start

成功会提示:Starting MySQL. SUCCESS

上面的mysql只能32位系统用,x86_64要用另一个包

http://dev.mysql.com/get/Downloads/MySQL-5.1/mysql-5.1.40-linux-x86_64-icc-glibc23.tar.gz

如果安装系统的时候是最小化安装,可能还需要安装Development tools工具包。

yum -y groupinstall "Development tools"

另外可能会报以下错误:

/usr/local/mysql/bin/mysqld: error while loading shared libraries: libstdc++.so.5: cannot open shared object file: No such file or directory

解决方法:

yum -y install compat-libstdc++-33

再用ldd查看

ldd /usr/local/mysql/bin/mysqld/

所有的依赖库都有就可以运行了

2、安装Php

Command:

wget http://cn2.php.net/distributions/php-5.4.36.tar.bz2
tar jxf php-5.4.36.tar.bz2
useradd -s /sbin/nologin php-fpm
cd php-5.4.36
./configure --prefix=/usr/local/php   --with-config-file-path=/usr/local/php/etc  --enable-fpm   --with-fpm-user=php-fpm  --with-fpm-group=php-fpm   --with-mysql=/usr/local/mysql  --with-mysql-sock=/tmp/mysql.sock  --with-libxml-dir  --with-gd   --with-jpeg-dir   --with-png-dir   --with-freetype-dir  --with-iconv-dir   --with-zlib-dir   --with-mcrypt   --enable-soap   --enable-gd-native-ttf   --enable-ftp  --enable-mbstring  --enable-exif    --disable-ipv6     --with-curl

当终端上显示:Thank you for using PHP.

说明配置已经成功了,也可以输入:echo $? 如果返回0说明成功了

然后编译安装:

make && make install

make install 出现这个错误:

Resolving pear.php.net... failed: Temporary failure in name resolution.
wget: unable to resolve host address “pear.php.net”
make: *** [install-pear] Error 4

重新运行make install命令这是因为网络不好不能下载:

http://pear.php.net/install-pear-nozlib.phar

最后再次输入:echo  $?返回0说明安装成功

cp php.ini-production /usr/local/php/etc/php.ini
cp /usr/local/src/php-5.4.36/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
mv /usr/local/php/etc/php-fpm.conf.default  /usr/local/php/etc/php-fpm.conf
chmod 755 /etc/init.d/php-fpm
chkconfig --add php-fpm
service php-fpm start         //成功会返回:Starting php-fpm  done
chkconfig php-fpm on

3. 安装nginx

Command:

cd ..
wget http://nginx.org/download/nginx-1.6.2.tar.gz
tar zxvf nginx-1.6.2.tar.gz
cd nginx-1.6.2
./configure   --prefix=/usr/local/nginx   --with-pcre

出现错误:

./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre=

安装pcre

yum -y install pcre-devel
make
make install

启动nginx:

/usr/local/nginx/sbin/nginx 

4. 配置解析php

vim  /usr/local/nginx/conf/nginx.conf

//把下面的配置,前面的#删除,并更改fastcgi_param SCRIPT_FILENAME 那一行

location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
include fastcgi_params;
}

重新加载

/usr/local/nginx/sbin/nginx -s  reload

vim  /usr/local/nginx/html/1.php

增加


测试:

curl localhost/1.php

以下是错误解决方案:(只针对本文章Php版本和安装方法):

出现错误:

checking for cc... no
checking for gcc... no
configure: error: in `/usr/local/src/php-5.4.36':
configure: error: no acceptable C compiler found in $PATH
See `config.log' for more details

没有安装gcc,安装gcc和gcc-c++

解决:

yum -y install gcc gcc-c++

出现错误:

checking for xml2-config path...
configure: error: xml2-config not found. Please check your libxml2 installation.

解决:

yum -y install libxml2-devel

出现错误:

checking if we should use cURL for url streams... no
checking for cURL in default path... not found
configure: error: Please reinstall the libcurl distribution -
easy.h should be in <curl-dir>/include/curl/

解决:

yum -y install libcurl-devel

出现错误:

configure: error: jpeglib.h not found.

解决:

yum -y install libjpeg-turbo libjpeg-turbo-static libjpeg-turbo-devel freetype-demos freetype-devel freetype

出现错误:

If configure fails try --with-vpx-dir=<DIR>
configure: error: png.h not found.

解决:

yum -y install libpng libpng-devel

出现错误:

configure: error: mcrypt.h not found. Please reinstall libmcrypt.

解决:

yum -y install epel-release.noarch

yum -y install libmcrypt-devel

Shell Scripts:

#!/bin/bash

######  统计时间    ######
begin_time()
{
begin_year_month_day=`date +%-Y年%-m月%-d日`
begin_hours=`date +%-H`
begin_minute=`date +%-M`
begin_second=`date +%-S`
}
#####   mysql php nginx download address

mysql='https://downloads.mariadb.com/archives/mysql-5.1/mysql-5.1.40-linux-i686-icc-glibc23.tar.gz'
php='http://cn2.php.net/distributions/php-5.4.36.tar.bz2'
nginx='http://nginx.org/download/nginx-1.6.2.tar.gz'


#####   local address mysql php Nginx

mysql_local='mysql-5.1.40-linux-i686-icc-glibc23.tar.gz'
php_local='php-5.4.36.tar.bz2'
nginx_local='nginx-1.6.2.tar.gz'


####    rely

rely=(epel-release.noarch gcc.$(uname -m) gcc-c++.$(uname -m) pcre.$(uname -m) pcre-devel.$(uname -m)   zlib-devel.$(uname -m) libxml2-devel.$(uname -m) openssl openssl-devel.$(uname -m) bzip2 bzip2-devel.$(uname -m) libjpeg-turbo.$(uname -m) libjpeg-turbo-static.$(uname -m) libjpeg-turbo-devel.$(uname -m) freetype-demos.$(uname -m) freetype-devel.$(uname -m) freetype.$(uname -m) libpng.$(uname -m) libpng-devel.$(uname -m) libmcrypt-devel.$(uname -m) libcurl-devel.$(uname -m))

#####   Check the local directory exists Mysql Php Nginx

checkfile()
{
if [ -e $mysql_local ]; then
        echo -e "\033[32m Mysql Installation package already exists, do not download directly installed!\033[0m"
else
        echo -e "\033[31m Mysql Installation package does not exist, you need to re-download, please wait ......\033[0m"
        wget $mysql
fi


if [ -e $php_local ]; then
        echo -e "\033[32m Php Installation package already exists, do not download directly installed!\033[0m"
else
        echo -e "\033[31m Php Installation package does not exist, you need to re-download, please wait ......\033[0m"
        wget $php

fi

if [ -e $nginx_local ]; then
        echo -e "\033[32m Nginx Installation package already exists, do not download directly installed!\033[0m"
else
        echo -e "\033[31m Nginx Installation package does not exist, you need to re-download, please wait ......\033[0m"
        wget $nginx
fi

for i in ${rely[*]}; do
    if ! rpm -q "$i">/dev/null ; then
        yum -y install $i
    fi
done
} 

#-----------------------------------------------------------------------------------------------------------------------------------------------
#-----------------------------------------------------------------------------------------------------------------------------------------------

#########       Install Mysql PHp Nginx Functions

#####   Install Mysql Functions

install_mysql()

{
        tar -zxf $mysql_local && mv /usr/local/mysql /usr/local/mysql_old 2>/dev/null || mkdir -p /usr/local/mysql
        mkdir -p /usr/local/mysql && mv mysql-5.1.40-linux-i686-icc-glibc23/* /usr/local/mysql/
        useradd -s /sbin/nologin mysql 2>/dev/null || userdel -r mysql && useradd -s /sbin/nologin mysql 2>/dev/null
        mv /data/mysql /data/mysql_back 2>/dev/null;mkdir -p /data/mysql
        chown -R mysql:mysql /data/mysql
        /usr/local/mysql/scripts/mysql_install_db --user=mysql --datadir=/data/mysql 2>/dev/null || /usr/local/mysql/scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql
        \cp /usr/local/mysql/support-files/my-large.cnf /etc/my.cnf
        \cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
        chmod 755 /etc/init.d/mysqld
        sed -i ':a $!N;$!ba;{s/\(datadir=[^\n]*\)\n/\1\/data\/mysql\n/}' /etc/init.d/mysqld
        chkconfig --add mysqld
        chkconfig mysqld on
        service mysqld start &&  echo -e "\033[32m Mysql has been successfully installed \033[0m"
}


#####   Install Php Functions

install_php()

{
        tar jxf $php_local
        cd php-5.4.36
        useradd -s /sbin/nologin php-fpm
        ./configure \
        --prefix=/usr/local/php \
        --with-config-file-path=/usr/local/php/etc \
        --enable-fpm \
        --with-fpm-user=php-fpm \
        --with-fpm-group=php-fpm \
        --with-mysql=/usr/local/mysql \
        --with-mysql-sock=/tmp/mysql.sock \
        --with-libxml-dir \
        --with-gd \
        --with-jpeg-dir \
        --with-png-dir \
        --with-freetype-dir \
        --with-iconv-dir \
        --with-zlib-dir \
        --with-mcrypt \
        --enable-soap \
        --enable-gd-native-ttf \
        --enable-ftp \
        --enable-mbstring \
        --enable-exif \
        --disable-ipv6 \
        --with-curl
        make
        make install &&  echo -e "\033[32m Php has been successfully installed \033[0m" || make install &&  echo -e "\033[32m Php has been successfully installed \033[0m"
        \cp php.ini-production /usr/local/php/etc/php.ini
        \cp /usr/local/src/php-5.4.36/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
        \mv /usr/local/php/etc/php-fpm.conf.default  /usr/local/php/etc/php-fpm.conf
        chmod 755 /etc/init.d/php-fpm
        chkconfig --add php-fpm
        service php-fpm start
        chkconfig php-fpm on
        
}

#####   Install Nginx Functions

install_nginx()

{       cd ..
        tar -zxf $nginx_local
        cd nginx-1.6.2
        ./configure \
        --prefix=/usr/local/nginx \
        --with-pcre
        make
        make install &&  echo -e "\033[32m Nginx has been successfully installed \033[0m"
}

#####   Nginx Combine Php

ncp()

{
        sed -i 's/index  index\.html index\.htm\;/index  index\.html index\.htm index\.php\;/' /usr/local/nginx/conf/nginx.conf
        sed -i 's/\#    fastcgi\_param  SCRIPT_FILENAME  \/scripts$fastcgi_script_name;/\#    fastcgi\_param  SCRIPT_FILENAME  \/usr\/local\/nginx\/html$fastcgi_script_name;/g' /usr/local/nginx/conf/nginx.conf
        sed -i '65,71s/^        \#/        /g' /usr/local/nginx/conf/nginx.conf
        /usr/local/nginx/sbin/nginx -s  reload
        echo -e "<?php\n  echo 'php script successfully tested';\n?>" >/usr/local/nginx/html/test.php
        /usr/local/nginx/sbin/nginx -t >/dev/null && echo -e "\033[32m Nginx Syntax OK \033[0m"
        /usr/local/nginx/sbin/nginx >/dev/null && echo -e "\033[32m Nginx Successful start \033[0m"
        result=$(curl -s localhost/test.php)
        echo -e "\033[31m $result \033[0m"

}


#-----------------------------------------------------------------------------------------------------------------------------------------------
#-----------------------------------------------------------------------------------------------------------------------------------------------

##########  Remove Mysql Php Nginx

#####   Remove Mysql

remove_mysql()

{
        service mysqld stop && echo -e "\033[31m service mysqld stop OK \033[0m"
        chkconfig --del mysqld && echo -e "\033[31m service mysqld delete OK \033[0m"
        killall -9 mysqld && echo -e "\033[31m Process mysqld kill OK\033[0m"
        rm -rf /etc/init.d/mysqld && echo -e "\033[31m /etc/init.d/mysqld Deleted OK\033[0m"
        rm -rf /etc/my.cnf && echo -e "\033[31m /etc/my.cnf Deleted OK\033[0m"
        rm -rf /data/mysql && echo -e "\033[31m /data/mysql Deleted OK\033[0m"
        rm -rf /usr/local/mysql && echo -e "\033[31m /usr/local/mysql Deleted OK\033[0m"
        rm -rf /var/lock/subsys/mysql && echo -e "\033[31m /var/lock/subsys/mysql Deleted OK\033[0m"
        userdel -r mysql && echo -e "\033[31m Mysqluser Deleted OK\033[0m"
        
}

#####   Remove Nginx

remove_nginx()

{       
        killall -9 nginx && echo -e "\033[31m Process nginx kill OK\033[0m"
        rm -rf /usr/local/nginx && echo -e "\033[31m /usr/local/nginx Deleted OK\033[0m"
}


#####   Remove Php

remove_php()

{
        service php-fpm stop && echo -e "\033[31m Service php-fpm stop OK\033[0m"
        chkconfig --del php-fpm && echo -e "\033[31m Service php-fpm delete OK\033[0m"
        killall -9 php-fpm && echo -e "\033[31m Process php-fpm kill OK\033[0m"
        userdel -r php-fpm && echo -e "\033[31m Php-fpm user Deleted OK\033[0m"
        rm -rf /usr/local/php && echo -e "\033[31m /usr/local/php Deleted OK\033[0m"
        rm -rf /usr/local/php/etc/php.ini && echo -e "\033[31m /usr/local/php/etc/php.ini Deleted OK\033[0m"
        rm -rf /etc/init.d/php-fpm && echo -e "\033[31m /etc/init.d/php-fpm Deleted OK\033[0m"
        rm -rf /usr/local/php/etc/php-fpm.conf && echo -e "\033[31m /usr/local/php/etc/php-fpm.conf Deleted OK\033[0m"

}

#-----------------------------------------------------------------------------------------------------------------------------------------------
#-----------------------------------------------------------------------------------------------------------------------------------------------

end_time()
{
echo ""
end_year_month_day=`date +%-Y年%-m月%-d日`
end_hours=`date +%-H`
end_minute=`date +%-M`
end_second=`date +%-S`

echo "从 $begin_year_month_day$begin_hours:$begin_minute:$begin_second 开始安装,到 $end_year_month_day$end_hours:$end_minute:$end_second 安装完成."
echo ""
echo 一共耗费了 $[$end_hours-begin_hours] 小时 $[$end_minute-begin_minute] 分钟 $[$end_second-$begin_second] 秒|sed 's/\-//'
echo ""
}
#####   Select Menu

selection=""
while [ "$selection" != "0" ]; do
        echo "
        Scripts Name:auto_install_LAMP.sh
        Version:1.0 
        By:iansiu
        My Blog:http://laowang.me
        support & Feedback:[email protected]
    "
        echo "
        
        PROGRAM MENU


        1 - Install Mysql、Php、Nginx
        2 - Remove  Mysql、Php、Nginx
        
        0 - exit program "
        echo ""
        read -n1 -p " Enter selection: " selection
        echo ""
        case $selection in
                1 ) clear; begin_time;checkfile&&install_mysql&&install_php&&install_nginx&&ncp;end_time;exit ;;
                2 ) clear; if [ -e /etc/init.d/mysqld ]; then remove_mysql&&remove_nginx&&remove_php; else echo -e "\033[31m No installation can not be uninstalled \033[0m"; fi ;;
                0 ) exit ;;
                * ) clear; echo "Please enter 1, 2, or 0"
        esac
done

开启对Mysqli的支持

./configure --with-mysql=/usr/local/mysql/bin/mysql_config \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd
暂无评论

发送评论 编辑评论


|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇