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.www58058.com/68032

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

相關推薦

  • 馬哥教育網絡班22期+第一周課程練習

        操作系統:centos 7.2  64位 計算機組成及功能。 計算機組成: 由存儲器、運算器、控制器、輸入設備、輸出設備組成。 功能: 存儲器:用來存放計算程序及參與運算的各種數據;例如:硬盤、內存 運算器:負責數據的算術運算和邏輯運算即數據的加工處理 控制器:負責對程序規定的控制信息進行分析,控制并…

    Linux干貨 2016-08-15
  • Linux三劍客之sed命令

    一.sed命令概述 Stream EDitor ,行編輯器 sed是一種流編輯器,它一次處理一行內容.處理時,把當前處理的行存儲在臨時緩沖區中,稱為”模式空間”(pattern space),接著用sed命令處理緩沖區中的內容,處理完成后,把緩沖區的內容送往屏幕.接著處理下一行,這樣不斷重復,直到文件末尾.文件內容并沒有改變除非你使用重定向存儲輸出.sed主…

    Linux干貨 2016-08-15
  • 系統管理之磁盤管理(二)磁盤配額,RAID,LVM

    上篇博文給大家介紹了磁盤和文件系統的基礎知識,也是最基本的使用.在實際生產環境中,對于磁盤的使用,要求穩定,靈活,那么下面給大家分享下磁盤的高級用法.磁盤配額,RAID,LVM等相關知識. 1.磁盤配額2.磁盤RAID3.LVM 一.磁盤配額 1.概述: ? 在內核中執行 ? 以文件系統為單位啟用 ? 對不同組或者用戶的策略不同…

    Linux干貨 2016-09-05
  • 文本編輯器:vim 基礎篇

       VI:Visual Interface,是一種文本編輯器,還是全屏編輯器。   VIM:Vi IMproved,vi的增強版,vim是模式化的編輯。 VIM的三種模式:        編輯模式(命令模式,默認模式)       插入?!?/p>

    Linux干貨 2016-08-18
  • Linux程序包管理rpm、yum、源碼編譯

    概述:     眾所周知,Linux操作系統本身,必須要借助額外的一些軟件,才能完成某些應用的,操作系統如果沒有應用程序的填充,就無法創造出生產力,這樣即使再完美的操作系統,也毫無用處。那么本章就簡要介紹一下Linux系統上對程序包的管理,分為以下三個部分:     1、程序包的…

    Linux干貨 2016-08-24

評論列表(1條)

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

    搭配上一張圖會更好點哈~~另外,腳本里面有些許錯誤,例如盡量保持換行~~加油~

欧美性久久久久