码迷,mamicode.com
首页 > 其他好文 > 详细

源码搭建lamp和lnmp环境

时间:2016-05-15 19:55:15      阅读:253      评论:0      收藏:0      [点我收藏+]

标签:lamp   lnmp   lamp源码   lnmp源码   lamp和lnmp源码   

1: lamp和lnmp解释

    lampn分别代表:linux、apache、mysql、php、nginx。安装大概思路如下:

        linux环境安装就省略了。

        1:mysql安装在两个环境都一样的。

        2: lnmp环境下nginx的安装。

        3: lamp环境下apache的安装。

        4: php在安装的时候应该算最复杂的了吧。其中,针对不同环境的编译选项不同。apache的编译选项--with-apxs2=/usr/local/apache2/bin/apxs,这里apache的安装目录在/usr/local/apache2/。nginx的编译选项--enable-fpm。


2: mysql的安装,直接上安装脚本

#!/bin/sh
LANG=C

if [ -d "/usr/local/mysql/" ];then
    echo "mysql is install"
    exit 1
else
    echo "mysql in not install"
fi

my_gcc=`rpm -qa gcc`
if [[ -n "$my_gcc" ]];then
    echo "$my_gcc" 
else
    yum -y install gcc
fi

my_gcc_c=`rpm -qa gcc-c++`
if [[ -n "$my_gcc_c" ]];then
    echo "$my_gcc_c"
else
    yum -y install gcc-c++
fi

my_make=`rpm -qa make`
if [[ -n "$my_make" ]];then
    echo "$my_make"
else
    yum -y install make 
fi

my_tar=`rpm -qa tar`
if [[ -n "$my_tar" ]];then
    echo "$my_tar"
else
    yum -y install tar
fi

my_openssl=`rpm -qa openssl`
if [[ -n "$my_openssl" ]];then
    echo "$my_openssl"
else
    yum -y install openssl
fi

my_openssl_devel=`rpm -qa openssl-devel`
if [[ -n "$my_openssl_devel" ]];then
    echo "$my_openssl_devel"
else
    yum -y install openssl-devel
fi

my_cmake=`rpm -qa cmake`
if [[ -n "$my_cmake" ]];then
    echo "$my_cmake"
else
    yum -y install cmake || (echo "install cmake failed, exit 2.";exit 2)
fi

my_ncurses=`rpm -qa ncurses`
if [[ -n "$my_ncurses" ]];then
    echo "$my_ncurses"
else
    yum -y install ncurses
fi

my_ncurses_devel=`rpm -qa ncurses-devel`
if [[ -n "$my_ncurses_devel" ]];then
    echo "$my_ncurses_devel"
else
    yum -y install ncurses-devel
fi

#make clean
#rm  -f CMakeCache.txt
mkdir -p /data/mysql
groupadd mysql
useradd -g mysql -M -s /sbin/nologin mysql

cd /usr/local/src
tar -zxvf mysql-5.6.29.tar.gz
cd mysql-5.6.29
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/data/mysql -DDEFAULT_CHARSET=utf8  -DDEFAULT_COLLATION=utf8_general_ci -DWITH_EXTRA_CHARSETS:STRING=all -DWITH_DEBUG=0 -DWITH_SSL=yes -DWITH_READLINE=1 -DENABLED_LOCAL_INFILE=1
if [ $? -ne 0 ];then
    echo "configure error. exit 2"
	exit 2
fi
echo "*******************************************"
echo "configure success"
echo "*******************************************"
echo "sleep 5, and begin make and make install"
sleep 5
make
make install
echo "install mysql success. mysql is in /usr/local/mysql"
#cd /usr/local/mysql
#chown -R mysql:mysql  /usr/local/mysql
#./scripts/mysql_install_db --user=mysql  -datadir=/data/mysql
#cp support-files/my-default.cnf /etc/my.cnf
#cp support-files/mysql.server /etc/init.d/mysqld
#bin/mysqld_safe --user=mysql &
#/etc/init.d/mysqld start

先简单将mysql设置如下:

cd /usr/local/mysql

chown -R mysql:mysql  /usr/local/mysql

./scripts/mysql_install_db --user=mysql  -datadir=/data/mysql

cp support-files/my-default.cnf /etc/my.cnf

cp support-files/mysql.server /etc/init.d/mysqld

/usr/local/mysql/bin/mysqld_safe --user=mysql &


配置文件my.cnf配置如下:

socket = /data/mysql/mysql.sock

basedir = /usr/local/mysql

datadir = /data/mysql


进入mysql设置:

mysql -uroot -S /data/mysql/mysql.sock

select user,host,password from mysql.user; 默认的root是空密码,有两个没用户的。

UPDATE `user` SET `password`=PASSWORD(‘xxxx‘) WHERE `User`=‘root‘;

delete from mysql where user=‘‘;

flush privileges;


mysql配置完成,两个环境都是由php操作mysql,这个并无差别。


2: nginx的安装,直接上安装脚本:

#!/bin/sh
LANG=C

if [ -d "/usr/local/nginx/" ];then
    echo "nginx is install"
    exit 1
else
    echo "nginx in not install"
fi

my_gcc=`rpm -qa gcc`
if [[ -n "$my_gcc" ]];then
    echo "$my_gcc" 
else
    yum -y install gcc
fi

my_gcc_c=`rpm -qa gcc-c++`
if [[ -n "$my_gcc_c" ]];then
    echo "$my_gcc_c"
else
    yum -y install gcc-c++
fi

my_make=`rpm -qa make`
if [[ -n "$my_make" ]];then
    echo "$my_make"
else
    yum -y install make 
fi

my_tar=`rpm -qa tar`
if [[ -n "$my_tar" ]];then
    echo "$my_tar"
else
    yum -y install tar
fi

my_pcre=`rpm -qa pcre`
if [[ -n "$my_pcre" ]];then
    echo "$my_pcre"
else
    yum -y install pcre
fi

my_pcre_devel=`rpm -qa pcre-devel`
if [[ -n "$my_pcre_devel" ]];then
    echo "$my_pcre_devel"
else
    yum -y install pcre-devel
fi

my_zlib=`rpm -qa zlib`
if [[ -n "$my_zlib" ]];then
    echo "$my_zlib"
else
    yum -y install zlib
fi

my_zlib_devel=`rpm -qa zlib-devel`
if [[ -n "$my_zlib_devel" ]];then
    echo "$my_zlib_devel"
else
    yum -y install zlib-devel
fi

my_openssl=`rpm -qa openssl`
if [[ -n "$my_openssl" ]];then
    echo "$my_openssl"
else
    yum -y install openssl
fi

my_openssl_devel=`rpm -qa openssl-devel`
if [[ -n "$my_openssl_devel" ]];then
    echo "$my_openssl_devel"
else
    yum -y install openssl-devel
fi

cd /usr/local/src
tar -zxvf nginx-1.8.1.tar.gz
cd nginx-1.8.1
./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module 
if [ $? -ne 0 ];then
    echo "configure error. exit 2"
	exit 2
fi
echo "*******************************************"
echo "configure success"
echo "*******************************************"
echo "sleep 5, and begin make and make install"
sleep 5
make
make install
echo "install nginx success. nginx is in /usr/local/nginx"
exit 0

nginx操作如下:

    /usr/local/nginx/sbin/nginx  -t 检查配置

    /usr/local/nginx/sbin/nginx 

    /usr/local/nginx/sbin/nginx  -s stop


3: apache的安装,直接上安装脚本

#!/bin/sh
LANG=C

if [ -d "/usr/local/apr/" ];then
    echo "apr is install"
    exit 1
else
    echo "apr in not install"
fi

if [ -d "/usr/local/apr-util/" ];then
    echo "apr_util is install"
    exit 1
else
    echo "apr-util in not install"
fi

if [ -d "/usr/local/apache2/" ];then
    echo "apache2 is install"
    exit 1
else
    echo "apache2 in not install"
fi

my_gcc=`rpm -qa gcc`
if [[ -n "$my_gcc" ]];then
    echo "$my_gcc" 
else
    yum -y install gcc
fi

my_make=`rpm -qa make`
if [[ -n "$my_make" ]];then
    echo "$my_make"
else
    yum -y install make 
fi

my_tar=`rpm -qa tar`
if [[ -n "$my_tar" ]];then
    echo "$my_tar"
else
    yum -y install tar
fi

my_libtool=`rpm -qa libtool`
if [[ -n "$my_libtool" ]];then
    echo "$my_libtool"
else
    yum -y install libtool
fi

my_pcre=`rpm -qa pcre`
if [[ -n "$my_pcre" ]];then
    echo "$my_pcre"
else
    yum -y install pcre
fi

my_pcre_devel=`rpm -qa pcre-devel`
if [[ -n "$my_pcre_devel" ]];then
    echo "$my_pcre_devel"
else
    yum -y install pcre-devel
fi

my_zlib=`rpm -qa zlib`
if [[ -n "$my_zlib" ]];then
    echo "$my_zlib"
else
    yum -y install zlib
fi

my_zlib_devel=`rpm -qa zlib-devel`
if [[ -n "$my_zlib_devel" ]];then
    echo "$my_zlib_devel"
else
    yum -y install zlib-devel
fi

my_openssl=`rpm -qa openssl`
if [[ -n "$my_openssl" ]];then
    echo "$my_openssl"
else
    yum -y install openssl
fi

my_openssl_devel=`rpm -qa openssl-devel`
if [[ -n "$my_openssl_devel" ]];then
    echo "$my_openssl_devel"
else
    yum -y install openssl-devel
fi

cd /usr/local/src
tar -zxvf apr-1.5.2.tar.gz
cd apr-1.5.2
./configure --prefix=/usr/local/apr
if [ $? -ne 0 ];then
    echo "configure error. exit 2"
	exit 2
fi
echo "*******************************************"
echo "configure success"
echo "*******************************************"
echo "sleep 5, and begin make and make install"
sleep 5
make
make install
echo "install apr success. apr is in /usr/local/apr"


cd /usr/local/src
tar -zxvf apr-util-1.5.4.tar.gz
cd apr-util-1.5.4
./configure --with-apr=/usr/local/apr --prefix=/usr/local/apr-util
if [ $? -ne 0 ];then
    echo "configure error. exit 2"
	exit 2
fi
echo "*******************************************"
echo "configure success"
echo "*******************************************"
echo "sleep 5, and begin make and make install"
sleep 5
make
make install
echo "install apr-util success. apr-util is in /usr/local/apr-util"

cd /usr/local/src
tar -zxvf httpd-2.4.18.tar.gz
cd httpd-2.4.18
./configure --prefix=/usr/local/apache2 --enable-rewrite --enable-so --enable-headers --enable-expires --with-mpm=worker --enable-modules=most --enable-deflate --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util
if [ $? -ne 0 ];then
    echo "configure error. exit 2"
	exit 2
fi
echo "*******************************************"
echo "configure success"
echo "*******************************************"
echo "sleep 5, and begin make and make install"
sleep 5
make
make install
echo "install apache2 success. apache2 is in /usr/local/apache2"

apache的启动与关闭:

    /usr/local/apache2/bin/apachectl -k start

    /usr/local/apache2/bin/apachectl -k stop



4.1: lnmp下php的安装,安装脚本

#!/bin/sh
LANG=C

if [ -d "/usr/local/php" ];then
    echo "php is install"
    exit 1
else
    echo "php in not install"
fi

my_epel=`rpm -qa epel-release`
if [[ -n "$my_epel" ]];then
    echo "$my_epel"
else
    yum -y install epel-release
    sed -i ‘s/^#baseurl/baseurl/;s/^mirrorlist/#mirrorlist/‘ /etc/yum.repos.d/epel.repo
fi

my_gcc=`rpm -qa gcc`
if [[ -n "$my_gcc" ]];then
    echo "$my_gcc"
else
    yum -y install gcc
fi

my_gcc_c=`rpm -qa gcc-c++`
if [[ -n "$my_gcc_c" ]];then
    echo "$my_gcc_c"
else
    yum -y install gcc-c++
fi

my_make=`rpm -qa make`
if [[ -n "$my_make" ]];then
    echo "$my_make"
else
    yum -y install make 
fi

my_tar=`rpm -qa tar`
if [[ -n "$my_tar" ]];then
    echo "$my_tar"
else
    yum -y install tar
fi

my_pcre=`rpm -qa pcre`
if [[ -n "$my_pcre" ]];then
    echo "$my_pcre"
else
    yum -y install pcre
fi

my_pcre_devel=`rpm -qa pcre-devel`
if [[ -n "$my_pcre_devel" ]];then
    echo "$my_pcre_devel"
else
    yum -y install pcre-devel
fi

my_zlib=`rpm -qa zlib`
if [[ -n "$my_zlib" ]];then
    echo "$my_zlib"
else
    yum -y install zlib
fi

my_zlib_devel=`rpm -qa zlib-devel`
if [[ -n "$my_zlib_devel" ]];then
    echo "$my_zlib_devel"
else
    yum -y install zlib-devel
fi

my_openssl=`rpm -qa openssl`
if [[ -n "$my_openssl" ]];then
    echo "$my_openssl"
else
    yum -y install openssl
fi

my_openssl_devel=`rpm -qa openssl-devel`
if [[ -n "$my_openssl_devel" ]];then
    echo "$my_openssl_devel"
else
    yum -y install openssl-devel
fi

my_libxml2=`rpm -qa libxml2`
if [[ -n "$my_libxml2" ]];then
    echo "$my_libxml2"
else
    yum -y install libxml2
fi

my_libxml2_devel=`rpm -qa libxml2-devel`
if [[ -n "$my_libxml2_devel" ]];then
    echo "$my_libxml2_devel"
else
    yum -y install libxml2-devel
fi

my_libcurl=`rpm -qa libcurl`
if [[ -n "$my_libcurl" ]];then
    echo "$my_libcurl"
else
    yum -y install libcurl
fi

my_libcurl_devel=`rpm -qa libcurl-devel`
if [[ -n "$my_libcurl_devel" ]];then
    echo "$my_libcurl_devel"
else
    yum -y install libcurl-devel
fi

my_libjpeg_devel=`rpm -qa libjpeg*|grep devel`
if [[ -n "$my_libjpeg_devel" ]];then
    echo "$my_libjpeg_devel"
else
    yum install libjpeg libjpeg-devel -y
fi

my_libpng=`rpm -qa libpng`
if [[ -n "$my_libpng" ]];then
    echo "$my_libpng"
else
    yum -y install libpng
fi

my_libpng_devel=`rpm -qa libpng-devel`
if [[ -n "$my_libpng_devel" ]];then
    echo "$my_libpng_devel"
else
    yum -y install libpng-devel
fi

my_freetype=`rpm -qa freetype`
if [[ -n "$my_freetype" ]];then
    echo "$my_freetype"
else
    yum -y install freetype
fi

my_freetype_devel=`rpm -qa freetype-devel`
if [[ -n "$my_freetype_devel" ]];then
    echo "$my_freetype_devel"
else
    yum -y install freetype-devel
fi

my_openldap=`rpm -qa openldap`
if [[ -n "$my_openldap" ]];then
    echo "$my_openldap"
else
    yum -y install openldap
fi

my_openldap_devel=`rpm -qa openldap-devel`
if [[ -n "$my_openldap_devel" ]];then
    echo "$my_openldap_devel"
else
    yum -y install openldap-devel
fi

my_libmcrypt=`rpm -qa libmcrypt`
if [[ -n "$my_libmcrypt" ]];then
    echo "$my_libmcrypt"
else
    yum -y install libmcrypt
fi

my_libmcrypt_devel=`rpm -qa libmcrypt-devel`
if [[ -n "$my_libmcrypt_devel" ]];then
    echo "$my_libmcrypt_devel"
else
    yum -y install libmcrypt-devel
fi

testldap=`ls -al /usr/lib/libldap*`
if [[ -n "$testldap" ]];then
    echo "$testldap"
else
    cp /usr/lib64/libldap* /usr/lib/
fi


cd /usr/local/src/
tar -zxvf php-5.6.17.tar.gz
cd php-5.6.17
./configure  --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --enable-mbregex --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 --enable-fpm
#--with-apxs2=/usr/local/apache2/bin/apxs --with-mysql=/usr/local/mysql/ --with-mysqli=/usr/local/mysql/bin/mysql_config
if [ $? -ne 0 ];then
    echo "configure error. exit 2"
	exit 2
fi
echo "*******************************************"
echo "configure success"
echo "*******************************************"
echo "sleep 5, and begin make and make install"
sleep 5
make 
make install
echo "install php success. php in /usr/local/php."

lnmp环境下php是使用php-fpm,lnmp环境最后配置的操作步骤如下:

    cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf

    注意下php-fpm.conf的listen配置,后续要跟nginx的对得上:

    listen = 127.0.0.1:9000


    nginx的配置如下:

        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;

        }

    

    测试php:

        vi /usr/local/nginx/html/mytest.php

            <?php

                echo "mytest";

            ?>

        访问http://localhost:8809/mytest.php,测试使用的是虚拟机,url需要自行测试。


    测试php的mysql模块:

        $link=mysql_connect(‘127.0.0.1‘,‘root‘,‘zwh@654‘);

        if(!$link){

                echo "unlink to mysql";

        }else{

                echo "mysql is link success";

        }


    技术分享


    lnmp配置完成。


4.2 lamp下php的安装,安装脚本只需要把--enable-fpm改为--with-apxs2=/usr/local/apache2/bin/apxs,然后安装即可。

    注意,如果机器有安装fpm,把安装的路径和解压缩的文件删除掉或者备份到其它地方。不然可能会安装不成功。

    

    lamp的最后配置如下:

        1: apache上配置支持php,vi /usr/local/apache2/conf/httpd.conf

            AddType application/x-httpd-php .php

            LoadModule php5_module        modules/libphp5.so

            

            配置完重启apache。/usr/local/apache2/bin/apachectl -k restart

    

        测试如上。


        lamp搭建完成。


注:脚本中有一些软件需要下载,请到官网下载源码包,或者直接谷歌download 对应的文件名下载即可。

本文出自 “鸿弟” 博客,请务必保留此出处http://zhuangweihong.blog.51cto.com/8808431/1773663

源码搭建lamp和lnmp环境

标签:lamp   lnmp   lamp源码   lnmp源码   lamp和lnmp源码   

原文地址:http://zhuangweihong.blog.51cto.com/8808431/1773663

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!