LNMP

1、源码编译安装LNMP架构环境

OS版本:2.6.32-431.el6.x86_64

Nginx版本:nginx-1.6.1

mariadb版本:mariadb-10.0.13

php版本:php-5.4.26


1、安装编译安装所需系统环境

~]# yum groupinstall "Development Tools" "Server Platform Development" -y


2、编译安装nginx-1.6.1

# yum -y install pcre-devel openssl-devel zlib-devel

# tar -xf nginx-1.6.1.tar.gz

# cd nginx-1.6.1

# ./configure –prefix=/usr/local/nginx –conf-path=/etc/nginx/nginx.conf –user=nginx –group=nginx  –error-log-path=/var/log/nginx/error.log –http-log-path=/var/log/nginx/access.log –pid-path=/var/run/nginx/nginx.pid –lock-path=/var/lock/nginx.lock –with-http_ssl_module –with-http_stub_status_module –with-http_gzip_static_module –with-debug

# make && make install

账号添加,否则开启会报错(nginx: [emerg] getpwnam("nginx") failed)

~]# useradd -r -M nginx

~]# id nginx

uid=498(nginx) gid=498(nginx) 组=498(nginx)

修改配置文件中servername从localhost修改为ip地址启动nginx测试

~]# /usr/local/nginx/sbin/nginx

~]# ss -tnlp | grep nginx

LISTEN     0      128                       *:80                       *:*      users:(("n ginx",3939,6),("nginx",3940,6))

LNMPImage.png

编写一个nginx启动脚本

~]# vim /etc/rc.d/init.d/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:      /etc/sysconfig/nginx

# pidfile:     /var/run/nginx/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)


sysconfig="/etc/sysconfig/$prog"

lockfile="/var/lock/nginx.lock"

pidfile="/var/run/nginx/${prog}.pid"


NGINX_CONF_FILE="/etc/nginx/nginx.conf"


[ -f $sysconfig ] && . $sysconfig



start() {

    [ -x $nginx ] || exit 5

    [ -f $NGINX_CONF_FILE ] || exit 6

    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 -p $pidfile $prog

    retval=$?

    echo

    [ $retval -eq 0 ] && rm -f $lockfile

    return $retval

}


restart() {

    configtest_q || return 6

    stop

    start

}


reload() {

    configtest_q || return 6

    echo -n $"Reloading $prog: "

    killproc -p $pidfile $prog -HUP

    echo

}


configtest() {

    $nginx -t -c $NGINX_CONF_FILE

}


configtest_q() {

    $nginx -t -q -c $NGINX_CONF_FILE

}


rh_status() {

    status $prog

}


rh_status_q() {

    rh_status >/dev/null 2>&1

}


# Upgrade the binary with no downtime.

upgrade() {

    local oldbin_pidfile="${pidfile}.oldbin"


    configtest_q || return 6

    echo -n $"Upgrading $prog: "

    killproc -p $pidfile $prog -USR2

    retval=$?

    sleep 1

    if [[ -f ${oldbin_pidfile} && -f ${pidfile} ]];  then

        killproc -p $oldbin_pidfile $prog -QUIT

        success $"$prog online upgrade"

        echo

        return 0

    else

        failure $"$prog online upgrade"

        echo

        return 1

    fi

}


# Tell nginx to reopen logs

reopen_logs() {

    configtest_q || return 6

    echo -n $"Reopening $prog logs: "

    killproc -p $pidfile $prog -USR1

    retval=$?

    echo

    return $retval

}


case "$1" in

    start)

        rh_status_q && exit 0

        $1

        ;;

    stop)

        rh_status_q || exit 0

        $1

        ;;

    restart|configtest|reopen_logs)

        $1

        ;;

    force-reload|upgrade)

        rh_status_q || exit 7

        upgrade

        ;;

    reload)

        rh_status_q || exit 7

        $1

        ;;

    status|status_q)

        rh_$1

        ;;

    condrestart|try-restart)

        rh_status_q || exit 7

        restart

        ;;

    *)

        echo $"Usage: $0 {start|stop|reload|configtest|status|force-reload|upgrade|restart|reopen_logs}"        exit 2

esac


~]# service nginx start

正在启动 nginx:                                           [确定]

~]# service nginx status

nginx (pid 1171 1169) 正在运行…

设置开机启动

 ~]# chkconfig –add nginx

~]# chkconfig –list nginx

nginx              0:关闭    1:关闭    2:关闭    3:关闭    4:关闭    5:关闭    6:关闭

~]# chkconfig nginx on

~]# chkconfig –list nginx

nginx              0:关闭    1:关闭    2:启用    3:启用    4:启用    5:启用    6:关闭


3、mariadb编译安装

首先安装mariadb编译安装所需的cmake、ncurses-devel

~]# yum -y install cmake ncurses-devel

(1)创建数据存放目录

~]# mkdir -pv /data/mydata

mkdir: 已创建目录 "/data"

mkdir: 已创建目录 "/data/mydata"

(2)创建mysql用户,并授予数据文件mysql账户权限

~]# groupadd mysql

~]# useradd -s /sbin/nologin -g mysql -M mysql

~]# id mysql

uid=500(mysql) gid=500(mysql) 组=500(mysql)

~]# chown -R mysql.mysql /data/mydata

(3)安装mariadb

解压mariadb

~]# tar -xf mariadb-10.0.13.tar.gz

编译安装mariadb

mariadb-10.0.13]# cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/data/mydata  -DSYSCONFDIR=/etc -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DWITH_READLINE=1 -DWITH_SSL=system -DWITH_ZLIB=system -DWITH_LIBWRAP=0 -DMYSQL_UNIX_ADDR=/tmp/mysql.sock -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci

mariadb-10.0.13]# make -j 4 && make install

(4)配置mariadb

初始化数据库

~]# cd /usr/local/mysql/

mysql]# scripts/mysql_install_db –user=mysql –datadir=/data/mydata

mysql]# ls /data/mydata/

aria_log.00000001  ibdata1      ib_logfile1  performance_schema

aria_log_control   ib_logfile0  mysql        test


设置配置文件,修改datadir指定目录项和socket所指定文件

[mysqld]

datadir=/data/mydata

socket=/tmp/mysql.sock

user=mysql

# Disabling symbolic-links is recommended to prevent assorted security risks

symbolic-links=0


[mysqld_safe]

log-error=/var/log/mysqld.log

pid-file=/var/run/mysqld/mysqld.pid


设置启动脚本

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

mysql]# chkconfig –add mysqld

mysql]# chkconfig –list mysqld

启动并查看服务

mysql]# service mysqld start

Starting MySQL. SUCCESS!

~]# ss -tnlp | grep 3306

LISTEN     0      128                      :::3306                    :::*      users:(("mysqld",65174,

16))

设置环境变量

~]# vim /etc/profile.d/mysql.sh

export PATH=/usr/local/mysql/bin:$PATH

~]# source /etc/profile.d/mysql.sh

导出头文件

~]# ln -sv /usr/local/mysql/include/ /usr/local/include/mysql

"/usr/local/include/mysql" -> "/usr/local/mysql/include/"

导出库文件

mysql]# vim /etc/ld.so.conf.d/mysql.conf

/usr/local/mysql/lib

mysql]# ldconfig -v

mysql]# ldconfig -p |grep mysql

    libmysqlclient_r.so.16 (libc6,x86-64) => /usr/lib64/mysql/libmysqlclient_r.so.16

    libmysqlclient.so.18 (libc6,x86-64) => /usr/local/mysql/lib/libmysqlclient.so.18

    libmysqlclient.so.16 (libc6,x86-64) => /usr/lib64/mysql/libmysqlclient.so.16

    libmysqlclient.so (libc6,x86-64) => /usr/local/mysql/lib/libmysqlclient.so

使用mysql_secure_installation脚本来进行安全配置

[root@localhost mysql]# mysql_secure_installation

/usr/local/mysql/bin/mysql_secure_installation: line 379: find_mysql_client: command not fou

nd

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB

      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!


In order to log into MariaDB to secure it, we'll need the current

password for the root user.  If you've just installed MariaDB, and

you haven't set the root password yet, the password will be blank,

so you should just press enter here.


Enter current password for root (enter for none):

OK, successfully used password, moving on…


Setting the root password ensures that nobody can log into the MariaDB

root user without the proper authorisation.


Set root password? [Y/n]

New password:

Re-enter new password:

Password updated successfully!

Reloading privilege tables..

 … Success!


By default, a MariaDB installation has an anonymous user, allowing anyone

to log into MariaDB without having to have a user account created for

them.  This is intended only for testing, and to make the installation

go a bit smoother.  You should remove them before moving into a

production environment.


Remove anonymous users? [Y/n]

 … Success!


Normally, root should only be allowed to connect from 'localhost'.  This

ensures that someone cannot guess at the root password from the network.


Disallow root login remotely? [Y/n] n

 … skipping.


By default, MariaDB comes with a database named 'test' that anyone can

access.  This is also intended only for testing, and should be removed

before moving into a production environment.


Remove test database and access to it? [Y/n] n

 … skipping.


Reloading the privilege tables will ensure that all changes made so far

will take effect immediately.


Reload privilege tables now? [Y/n]

 … Success!


Cleaning up…


All done!  If you've completed all of the above steps, your MariaDB

installation should now be secure.


Thanks for using MariaDB!


4、编译安装php

编译安装前安装所需要包

~]# yum -y install libxml2-devel

安装bzip2-devel

php-5.4.26]# yum -y install bzip2-devel

~]# wget ftp://mcrypt.hellug.gr/pub/crypto/mcrypt/libmcrypt/libmcrypt-2.5.7.tar.gz

~]# tar -xf libmcrypt-2.5.7.tar.gz

~]# cd libmcrypt-2.5.7

libmcrypt-2.5.7]# ./configure

libmcrypt-2.5.7]# make && make install


解压并进行编译安装,nginx是通过php-fpm连接php的,编译时需添加–enable-fpm

./configure –prefix=/usr/local/php5 –with-mysql=mysqlnd –with-openssl –with-mysqli=mysqlnd –enable-mbstring –with-freetype-dir –with-jpeg-dir –with-png-dir –with-zlib –with-libxml-dir=/usr –enable-xml  –enable-sockets –enable-fpm –with-mcrypt  –with-config-file-path=/etc –with-config-file-scan-dir=/etc/php.d –with-bz2


再次编译没有问题,进行安装

php-5.4.26]# make -j 4 && make install


拷贝配置文件至/etc目录

php-5.4.26]# cp php.ini-production /etc/php.ini

拷贝php-fpm配置文件,并同时取消pid选项的注释

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

php-5.4.26]# vim /usr/local/php5/etc/php-fpm.conf

pid = /usr/local/php5/var/run/php-fpm.pid

添加服务脚本

fpm]# pwd

/root/php-5.4.26/sapi/fpm

fpm]# cp init.d.php-fpm /etc/rc.d/init.d/php-fpm

~]# chmod +x /etc/rc.d/init.d/php-fpm

~]# chkconfig –add php-fpm

启动php-fpm

~]# service php-fpm start

~]# ps aux | grep fpm

root      14881  0.0  0.3  68920  3928 ?        Ss   09:51   0:00 php-fpm: master process (/usr/local/php5/etc/php-fpm.conf)                                                                      nobody    14882  0.0  0.3  68920  3460 ?        S    09:51   0:00 php-fpm: pool www

                                                                                           nobody    14883  0.0  0.3  68920  3460 ?        S    09:51   0:00 php-fpm: pool www

                                                                                           root      14885  0.0  0.0 103260   872 pts/1    S+   09:51   0:00 grep f



5、修改nginx.conf文件,支持php

         location / {

            root   html;

            index  index.html index.htm index.php;     添加支持index.php

        }


        location ~ \.php$ {

            root    html;

            fastcgi_pass   127.0.0.1:9000;

            fastcgi_index  index.php;

            include        fastcgi.conf;     这边要注意的是直接include fastcgi.conf即可,老版本的使用方法是fastcgi_param  SCRIPT_FILENAME   $document_root$fastcgi_script_name;再include。参考http://gxl-ct001.iteye.com/blog/2270522

        }


修改完成后重启nginx

service nginx restart

停止 nginx:                                               [确定]

正在启动 nginx:                                           [确定]

6、编写测试页进行测试

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

<h1>LNMP TEST</h1>

<?php

    $conn = mysql_connect('localhost','root','oracleadmin');     测试连接mariadb是否ok

     if ($conn)

          echo "OK";

     else

          echo "Failure";

     phpinfo();     测试nginx使用php是否ok

?>

Image2.png


此时LNMP的编译安装全部完成

LNMP



2、编写一个脚本完成以下功能:

     (1)、一键搭建LNMP源码编译环境;

     (2)、可通过在脚本后面跟上一些参数来自定义安装目录等其他选项。

script]# cat LNMP.sh

#!/bin/bash

echo "Compile environment installation"

source $1

envinstall() {

    cd /opt

        echo "——–Compile environment install Starting——–"

    yum -y groupinstall "Development Tools" "Server Platform Development"

    yum -y install pcre-devel openssl-devel zlib-devel cmake ncurses-devel libxml2-devel bzip2-devel wget

    wget ftp://mcrypt.hellug.gr/pub/crypto/mcrypt/libmcrypt/libmcrypt-2.5.7.tar.gz

    tar -xf libmcrypt-2.5.7.tar.gz

    cd libmcrypt-2.5.7

        ./configure

     make && make install

}

nginxinstall() {

        id nginx &>/dev/null

        if [ $? -gt 0 ];then

        useradd -r -M nginx

        fi

        cd /opt

        echo "——–Nginx Start install——–"

        wget http://nginx.org/download/nginx-1.6.1.tar.gz

        tar -xf nginx-1.6.1.tar.gz

        cd nginx-1.6.1

    ./configure –prefix=$nginx_dir –conf-path=/etc/nginx/nginx.conf –user=nginx –group=nginx  –error-log

-path=/var/log/nginx/error.log –http-log-path=/var/log/nginx/access.log –pid-path=/var/run/nginx/nginx.pid –lock-path=/var/lock/nginx.lock –with-http_ssl_module –with-http_stub_status_module –with-http_gzip_static_module –with-debug    make && make install

        cp $package/nginx.start /etc/rc.d/init.d/nginx

        chmod +x /etc/rc.d/init.d/nginx

        chkconfig –add nginx

        chkconfig nginx on

        echo "export PATH=$nginx_dir/sbin:$PATH">/etc/profile.d/nginx

        source /etc/profile.d/nginx

    mv /etc/nginx/nginx.conf{,.bak}

    cp -f $package/nginx.conf /etc/nginx/nginx.conf

}

mysqlinstall() {

    id mysql &>/dev/null

    if [ $? -gt 0 ];then

    groupadd mysql

    useradd -s /sbin/nologin -g mysql -M mysql

    fi

    if [ ! -d $mydata_dir ];then

    mkdir -p $mydata_dir

    fi

    cd /opt

    echo "——–Mysql Start install——–"

    cp $package/mariadb-10.0.13.tar.gz /opt

    tar -xf mariadb-10.0.13.tar.gz

    cd mariadb-10.0.13

    cmake . -DCMAKE_INSTALL_PREFIX=$mysql_dir -DMYSQL_DATADIR=$mydat

a_dir  -DSYSCONFDIR=/etc -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAG

E_ENGINE=1 -DWITH_READLINE=1 -DWITH_SSL=system -DWITH_ZLIB=system -DWITH_LIBWRAP=0 -DMYSQL_UNIX_ADDR=/tmp/mysql.sock -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci    make && make install    cd $mysql_dir

    scripts/mysql_install_db –user=mysql –datadir=$mydata_dir

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

    chkconfig –add mysqld

    chkconfig mysqld on

    #cp $package/nginx.start /etc/rc.d/init.d/nginx

    chmod +x /etc/rc.d/init.d/nginx

    echo "export PATH=$mysql_dir/bin:$PATH">/etc/profile.d/mysql

    source /etc/profile.d/mysql

    ln -sv /usr/local/mysql/include/ /usr/local/include/mysql

    echo "/usr/local/mysql/lib">/etc/ld.so.conf.d/mysql.conf

    ldconfig -v

    mv /etc/my.cnf{,.bak}

    cp -f $package/my.cnf /etc/my.cnf

    service mysqld start

    mysqladmin -u root -p password "oracleadmin"

}

phpinstall() {

    cd /opt

    echo "——–php Start install——–"

    cp $package/php-5.4.26.tar.bz2 /opt

    tar -xf php-5.4.26.tar.bz2

    cd php-5.4.26

    ./configure –prefix=$php_dir –with-mysql=mysqlnd –with-openss

l –with-mysqli=mysqlnd –enable-mbstring –with-freetype-dir –with-jpeg-dir –with-png-dir –with-zlib –with-l

ibxml-dir=/usr –enable-xml  –enable-sockets –enable-fpm –with-mcrypt  –with-config-file-path=/etc –with-config-file-scan-dir=/etc/php.d –with-bz2    make && make install    cp php.ini-production /etc/php.ini

    cp $php_dir/etc/php-fpm.conf.default  $php_dir/etc/php-fpm.conf

    cp /root/php-5.4.26/sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php

-fpm    chmod +x /etc/rc.d/init.d/php-fpm

    chkconfig –add php-fpm

    chkconfig php-fpm on

    #cp $package/nginx.start /etc/rc.d/init.d/nginx

    echo "export PATH=$php_dir/bin:$PATH">/etc/profile.d/php

    source /etc/profile.d/php

    cat>/usr/local/php5/etc/php-fpm.conf <<EOF

         pid = /usr/local/php5/var/run/php-fpm.pid

    EOF

}

envinstall

if [ $? -eq 0 ];then

    echo "Compile environment install is finished Sucess!"

else

    echo "Compile enviroment install Failed!"

    exit 1

fi

nginxinstall

service nginx start

if [$? -eq 0 ];then

        echo "nginx install is finished Sucess!"

else

        echo "nginx install Failed!"

        exit 1

fi

mysqlinstall

service mysqld start

if [ $? -eq 0 ];then

    echo "mysql install is finished Sucess!"

else

    echo "mysql install Failed!"

    exit 1

fi

phpinstall

service php-fpm start

if [ $? -eq 0 ];then

    echo "php install is finished Sucess!"

else

    echo "php install Failed!"

    exit 1

fi

[root@localhost script]# ls

dir.conf  LNMP.sh  package     dir.conf为参数文件     package为资源文件和配置文件

[root@localhost script]# cat dir.conf

package=/root/script/package

nginx_dir=/usr/local/nginx

mysql_dir=/usr/local/mysql

mydata_dir=/data/mydata

php_dir=/usr/local/php5

[root@localhost script]# ls -l package/

总用量 62920

-rw-r–r– 1 root root 51333762 2月   9 12:30 mariadb-10.0.13.tar.gz

-rw-r–r– 1 root root      239 2月   9 14:06 my.cnf

-rw-r–r– 1 root root   803301 2月   9 12:30 nginx-1.6.1.tar.gz

-rw-r–r– 1 root root     2584 2月   9 13:53 nginx.conf

-rw-r–r– 1 root root     2818 2月   9 10:41 nginx.start

-rw-r–r– 1 root root     2818 2月   9 10:41 nginx.start.bak

-rw-r–r– 1 root root 12270535 2月   9 12:30 php-5.4.26.tar.bz2

原创文章,作者:N23-苏州-void,如若转载,请注明出处:http://www.178linux.com/68032

(0)
N23-苏州-voidN23-苏州-void
上一篇 2017-02-09
下一篇 2017-02-10

相关推荐

  • 学习标准I/O和管道的心得体会

    重定向、标准输出、标准输入、标准错误、管道的命令整理

    2017-11-20
  • 2017中华数据库与运维大会强势回归!

         时隔一年,2017年中华数据库与运维大会将于06月03日在上海宝丰联大酒店强势回归,届时来自全国各行各业的IT从业人员将欢聚上海,探讨与交流数据库和运维的经验。       自2009年以来,超过4000位技术总监、技术负责人、架构师、项目经理、运维总监、运维经理、DBA经理、DBA、运维…

    2017-02-28
  • 马哥教育网络班20期+第3周课程练习

     1、列出当前系统上所有已经登录的用户的用户名,注意:同一个用户登录多次,则只显示一次即可。 法一: who | cut -d ‘ ‘ -f 1 | tail -u 法二: who | cut -d ’’-f&…

    Linux干货 2016-06-26
  • quota AND raid

    1.磁盘配额 将home目录独立出来单独的分区,限制用户的使用。 (1)先创建分区,如下图的分区sda6 (2)命令同步系统的新增加分区:partx –a /dev/sda (3)命令格式化新分区:     (4)把创建的设备挂载到/mnt/home下    (5)迁移数据,移动之前要确保home分区没有使用。权限是保…

    Linux干货 2016-09-19
  • N25_第二周博客作业:

    N25_第二周博客作业: 1、linux上的文件管理命令都有哪些,其常用的使用方法及其相关示例演示。 Linux上常见的文件管理类命令有mkdir、touch、cp、mv、rm、stat。 mkdir:创建新目录 SYNOPSIS   mkdir [OPTION]… DIRECTORY… 常用: 1、直接建目录 [root@…

    Linux干货 2016-12-09
  • 始于此

         “路漫漫其修远兮,吾将上下而求索”,一段路程的结束,意味着一段路程的开始,同岁月般,永远也不能回到出发的那一刻,你所能做的,就是一路走下去。回顾珍惜,却不能逗留。         我的学习宣言是“功崇惟志,业广惟勤”,时刻告诉自己,“功”出…

    Linux干货 2016-11-28

评论列表(1条)

  • 马哥教育
    马哥教育 2017-03-13 23:38

    搭配上一张图会更好点哈~~另外,脚本里面有些许错误,例如尽量保持换行~~加油~