LNMP編譯安裝-week16

1、源碼編譯安裝LNMP架構環境;

系統: CentOS 7.2
IP: 172.16.0.11
版本: nginx-1.10.3 php-5.6.30 mysql-5.6.30

一.安裝開發包組

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

二.編譯安裝nginx-1.10.3

(1) 安裝依賴包

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

(2) 創建nginx用戶和組

~]# useradd -r nginx

(3) 編譯安裝

~]# tar xf nginx-1.10.3.tar.gz
~]# cd nginx-1.10.3/
]# ./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.pid --lock-path=/var/lock/nginx.lock --with-http_ssl_module --with-http_gzip_static_module --with-debug --with-http_stub_status_module
]# make && make install

(4) 啟動nginx

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

(5) 測試頁:
mark

問題1: 重啟后執行/usr/local/nginx/sbin/nginx報錯,使用啟動腳本或者unit也無法啟動

]# /usr/local/nginx/sbin/nginx
[root@localhost ~]# nginx: [emerg] open() "/var/run/nginx/nginx.pid" failed (2: No such file or directory)

原因: 系統中沒有/var/run/nginx目錄,手動創建,即可執行成功,但/var/run/nginx下面的目錄重啟后就沒有了,所以我覺得pid目錄設置為/var/run/nginx.pid會好一些,因此上面的configure語句已經修改過并重新執行,證實該問題已經解決

(5) nginx開機啟動(啟動腳本)

]# vim /etc/rc.d/init.d/nginx
#! /bin/bash
#
# 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
# pidfile:     /var/run/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)

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

[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx

lockfile=/var/lock/nginx.lock


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 $prog -QUIT
    retval=$?
    echo
    [ $retval -eq 0 ] && rm -f $lockfile
    return $retval
}

restart() {
    configtest || return $?
    stop
    sleep 1
    start
}

reload() {
    configtest || return $?
    echo -n $"Reloading $prog: "
    killproc $nginx -HUP
    RETVAL=$?
    echo
}

force_reload() {
    restart
}

configtest() {
  $nginx -t -c $NGINX_CONF_FILE
}

rh_status() {
    status $prog
}

rh_status_q() {
    rh_status >/dev/null 2>&1
}

case "$1" in
    start)
        rh_status_q && exit 0
        $1
        ;;
    stop)
        rh_status_q || exit 0
        $1
        ;;
    restart|configtest)
        $1
        ;;
    reload)
        rh_status_q || exit 7
        $1
        ;;
    force-reload)
        force_reload
        ;;
    status)
        rh_status
        ;;
    condrestart|try-restart)
        rh_status_q || exit 0
            ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
        exit 2
esac

備注:腳本中pid文件改成如下路徑(和configure中一致),否則無法啟動服務

# pidfile:     /var/run/nginx.pid

授權

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

設置開機自啟動

]# chkconfig --add nginx
]# chkconfig nginx on
]# chkconfig --list nginx

Note: This output shows SysV services only and does not include native
      systemd services. SysV configuration data might be overridden by native
      systemd configuration.

      If you want to list systemd services use 'systemctl list-unit-files'.
      To see services enabled on particular target use
      'systemctl list-dependencies [target]'.

nginx           0:off   1:off   2:on    3:on    4:on    5:on    6:off

測試腳本:

[root@localhost init.d]# service nginx stop
Stopping nginx (via systemctl):                            [  OK  ]
[root@localhost init.d]# service nginx start
Starting nginx (via systemctl):                            [  OK  ]
[root@localhost init.d]# service nginx status
● nginx.service - SYSV: Nginx is an HTTP(S) server, HTTP(S) reverse proxy and IMAP/POP3 proxy server
   Loaded: loaded (/etc/rc.d/init.d/nginx; bad; vendor preset: disabled)
   Active: active (running) since Sun 2017-05-07 15:40:44 CST; 5s ago
     Docs: man:systemd-sysv-generator(8)
  Process: 1382 ExecStop=/etc/rc.d/init.d/nginx stop (code=exited, status=0/SUCCESS)
  Process: 1413 ExecStart=/etc/rc.d/init.d/nginx start (code=exited, status=0/SUCCESS)
 Main PID: 1420 (nginx)
   CGroup: /system.slice/nginx.service
           ├─1420 nginx: master process /usr/local/nginx/sbin/nginx -c /etc/n...
           └─1422 nginx: worker process

May 07 15:40:44 localhost.localdomain systemd[1]: Starting SYSV: Nginx is an ...
May 07 15:40:44 localhost.localdomain nginx[1413]: Starting nginx: [  OK  ]
May 07 15:40:44 localhost.localdomain systemd[1]: PID file /var/run/nginx/ngi...
May 07 15:40:44 localhost.localdomain systemd[1]: Started SYSV: Nginx is an H...
Hint: Some lines were ellipsized, use -l to show in full.

重啟服務器查看效果

]# ss -tnl
State      Recv-Q Send-Q Local Address:Port               Peer Address:Port     
LISTEN     0      128          *:80                       *:*

(6) unit自啟動
配置unit

]# cd /usr/lib/systemd/system
]# vim nginx.service
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/var/run/nginx/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /etc/nginx/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

開機自啟動

]# systemctl enable nginx.service

測試

[root@localhost system]# systemctl start nginx.service
[root@localhost system]# ss -tnl
State      Recv-Q Send-Q Local Address:Port               Peer Address:Port     
LISTEN     0      128          *:80                       *:*
[root@localhost system]# systemctl stop nginx.service
[root@localhost system]# ss -tnl
State      Recv-Q Send-Q Local Address:Port               Peer Address:Port     
LISTEN     0      128          *:22                       *:*
LISTEN     0      100    127.0.0.1:25                       *:*                 
LISTEN     0      128         :::22                      :::*
LISTEN     0      100        ::1:25                      :::*
重啟:
[root@localhost ~]# ss -tnl
State      Recv-Q Send-Q Local Address:Port               Peer Address:Port     
LISTEN     0      128          *:80                       *:*

三.安裝mysql-5.6.30

(1) 下載mysql

]# cd /usr/local/src
]# wget http://dev.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6.30.tar.gz

(2) 安裝編譯源碼所需的工具和庫

]# yum -y install cmake ncurses-devel

(3) 設置MySQL用戶和組

]# useradd -r mysql

(4) 編譯安裝

]# cd /usr/local/src
]# tar xf mysql-5.6.30.tar.gz
]# cd mysql-5.6.30/
]# cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_MEMORY_STORAGE_ENGINE=1 -DWITH_PARTITION_STORAGE_ENGINE=1 -DMYSQL_DATADIR=/data/mysqldb -DMYSQL_TCP_PORT=3306 -DENABLE_DOWNLOADS=1 -DEXTRA_CHARSETS=all -DENABLED_LOCAL_INFILE=1 -DWITH_READLINE=1

(注:重新運行配置,需要刪除CMakeCache.txt文件 rm CMakeCache.txt )   
]# make -j 4 && make install

(5) 初始化數據庫

]# mkdir -p /data/mysqldb      #創建數據庫數據目錄  
]# chown -R mysql.mysql /usr/local/mysql   # 給程序目錄授權,否則socket文件無法寫入到該目錄
]# chown -R mysql.mysql /data/mysqldb/   #給數據目錄授權
]# cd /usr/local/mysql/
]# ./scripts/mysql_install_db --user=mysql --datadir=/data/mysqldb   


參考: /usr/local/mysql/scripts/mysql_install_db --defaults-file=/etc/my.cnf --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --user=mysql

(6) 準備mysql配置文件

]# cp /usr/local/mysql/support-files/my-default.cnf /etc/my.cnf    (注:如果/etc/my.cnf文件存在,則覆蓋。)  
]# vim /etc/my.cnf
[mysqld]
...
datadir = /data/mysqldb
innodb_file_per_table = ON
skip_name_resolve = ON
...

(7) 復制mysql啟動腳本

]# cp support-files/mysql.server /etc/rc.d/init.d/mysqld
]# sed  -i 's@^basedir=@basedir=/usr/local/mysql@' /etc/init.d/mysqld
]# sed  -i 's@^datadir=@datadir=/data/mysqldb@' /etc/init.d/mysqld

(8) 啟動mysql服務并設置開機自啟動

]# service mysqld start
Starting MySQL... SUCCESS!
]# chkconfig --add mysqld
]# chkconfig --level 35 mysqld on

查看是否啟動

[root@localhost mysqldb]# ss -tnlp | grep 3306
LISTEN     0      80          :::3306                    :::*                   users:(("mysqld",pid=2812,fd=10))

檢查開機啟動

]# ss -tnlp | grep 3306
LISTEN     0      80          :::3306                    :::*                   users:(("mysqld",pid=1028,fd=10))

(9) 設置環境變量

]# echo "export PATH=/usr/local/mysql/bin:$PATH" > /etc/profile.d/mysql.sh
[root@localhost ~]# source /etc/profile.d/mysql.sh

(10) 測試登錄,默認是沒有密碼,直接回車就可進入

]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.30 Source distribution

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

(11) 修改MySQL用戶root的密碼,此時使用安全加固方法

]# mysql_secure_installation



NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MySQL to secure it, we'll need the current
password for the root user.  If you've just installed MySQL, 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 MySQL
root user without the proper authorisation.

Set root password? [Y/n] y
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
 ... Success!


By default, a MySQL installation has an anonymous user, allowing anyone
to log into MySQL 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] n
 ... skipping.

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, MySQL 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] y
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] y
 ... Success!




All done!  If you've completed all of the above steps, your MySQL
installation should now be secure.

Thanks for using MySQL!


Cleaning up...

方法一:mysql -u root -p
use mysql
update user set password=password(‘123456’) where user=’root’
flush privileges(刷新權限)
方法二:/usr/local/mysql/bin/mysql_secure_installation
(修改MySQL用戶root的密碼,同時可禁止root遠程連接,移除test數據庫和匿名用戶。)
方法三: /usr/local/mysql/bin/mysqladmin -uroot -p password ‘你的密碼’

(12) 輸出mysql的頭文件至系統頭文件路徑/usr/include(非必須)

]# ln -s /usr/local/mysql/include/ /usr/include/mysql

(13) 輸出mysql的庫文件給系統庫查找路徑(非必須)

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

問題2:啟動服務報錯

]# service mysqld start
Starting MySQL.... ERROR! The server quit without updating PID file (/data/mysqldb/localhost.localdomain.pid).

]# vim localhost.localdomain.err
2017-05-07 23:29:40 2496 [ERROR] Can't start server : Bind on unix socket: Permission denied
2017-05-07 23:29:40 2496 [ERROR] Do you already have another mysqld server running on socket: /usr/local/mysql/mysql.sock ?
2017-05-07 23:29:40 2496 [ERROR] Aborting  

找到原因: 因為mysql帳號對/usr/local/mysql沒有寫權限,無法創建socket文件  

解決方法: 
]# chown -R mysql.mysql /usr/local/mysql
[root@localhost mysqldb]# service mysqld start
Starting MySQL... SUCCESS!

四. php安裝

(1) PHP添加libmcrypt拓展

[root@localhost ~]# tar xf libmcrypt-2.5.8.tar.bz2
[root@localhost ~]# cd libmcrypt-2.5.8/
[root@localhost libmcrypt-2.5.8]# ./configure --prefix=/usr/local/libmcrypt
[root@localhost ~]# make && make install && cd
[root@localhost ~]# sed -i '1a/usr/local/libmcrypt/lib' /etc/ld.so.conf

(2) 解決依賴

[root@localhost ~]# yum -y install php-pear
[root@localhost ~]# yum -y install libxml2-devel libcurl-devel libjpeg-devel libpng-devel freetype freetype-devel

(3) 編譯安裝php-5.6

[root@localhost ~]# tar xf php-5.6.30.tar.bz2 
[root@localhost ~]# cd php-5.6.30/
[root@localhost php-5.6.30]# ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php  --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-iconv-dir --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir --enable-xml --disable-rpath --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --enable-mbregex --enable-fpm --enable-mbstring --with-gd --enable-gd-native-ttf --with-mhash  --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --with-mcrypt=/usr/local/libmcrypt/ --with-gettext
[root@localhost php-5.6.30]# make && make install

(4) 復制php配置文件

[root@localhost php-5.6.30]# cp php.ini-production /usr/local/php/etc/php.ini

(5) 復制php-fpm配置文件

[root@localhost php-5.6.30]# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf

(6) 復制php-fpm啟動腳本到init.d,設置開機啟動

[root@localhost php-5.6.30]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@localhost php-5.6.30]# cd
[root@localhost ~]# chmod +x /etc/rc.d/init.d/php-fpm 
[root@localhost ~]# chkconfig --add php-fpm
[root@localhost ~]# chkconfig php-fpm on

(7) 立即啟動php-fpm

[root@localhost php]# service php-fpm start

(8) 修改nginx配置文件使之支持php

[root@localhost ~]# vim /etc/nginx/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           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;
}

備注: #取消FastCGI server部分location的注釋,并要注意fastcgi_param行的參數,改為$document_root$fastcgi_script_name,或者使用絕對路徑  

測試一下是否有錯:
/usr/local/nginx/sbin/nginx -t
平滑重啟nginx
[root@localhost ~]# systemctl reload nginx
或 
[root@localhost ~]# /usr/local/nginx/sbin/nginx -s reload

五. 測試php和nginx和mysql

(1) 測試nginx和php的聯動

[root@localhost ~]# cat > /usr/local/nginx/html/index.php << EOF
<?php
    phpinfo()
?>
EOF

mark
(2) 測試php與mysql的聯動

[root@localhost ~]# cat > /usr/local/nginx/html/index.php <<EOF
<?php
    \$conn = mysql_connect('127.0.0.1','root','123456');
    if (\$conn)
       echo "OK";
    else
       echo "Failure";
?>
EOF

mark

問題3: 測試php和mysql聯動是有警告mysql_connect(): Headers and client library minor version mismatch ,該文檔已經修改過,無此錯誤
之前的錯誤圖片:
mark

[root@localhost ~]# /usr/local/php/bin/php -i|grep Client
Client API version => 5.5.44-MariaDB
Client API library version => 5.5.44-MariaDB
Client API header version => 5.6.30
Soap Client => enabled

原因: 版本不兼容導致上述警告, 唉,新版本總會有各種各樣的問題!!!
new MySQL 5.6 family you need to install PHP with php5-mysqlnd, not php5-mysql
解決方法: 重新編譯

./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php  --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-iconv-dir --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir --enable-xml --disable-rpath --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --enable-mbregex --enable-fpm --enable-mbstring --with-gd --enable-gd-native-ttf --with-mhash  --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --with-mcrypt=/usr/local/libmcrypt/ --with-gettext

用
--with-mysql=mysqlnd 
--with-mysqli=mysqlnd 
--with-pdo-mysql=mysqlnd
替換掉原來的
--with-mysql=/usr/local/mysql
--with-mysqli=/usr/local/mysql/bin/mysql_config

2、編寫一個腳本完成以下功能:

(1)、一鍵搭建LNMP源碼編譯環境;
(2)、可通過在腳本后面跟上一些參數來自定義安裝目錄等其他選項。

系統: centos7.2
(1) 準備程序

[root@localhost src]# pwd
/usr/local/src
[root@localhost src]# ls
libmcrypt-2.5.8.tar.bz2  lnmp_source.sh  nginx  nginx-1.10.3.tar.gz  php-5.6.30.tar.bz2

(2) 執行腳本

[root@localhost src]# chmod +x lnmp_source.sh
[root@localhost src]# ./lnmp_source.sh 2>&1 | tee lnmp_install.log
#安裝記錄輸出到文件,供分析錯誤用

(3) 腳本內容

]# cat lnmp_source.sh
#!/bin/bash
# lnmp 源碼安裝
# han
system_ready() {
        echo "安裝前環境準備..."
        sleep 3
        iptables -F
        systemctl stop firewalld.service
        systemctl disable firewalld.service
        setenforce 0 &> /dev/null
        sed -i -r 's/(SELINUX=).*/\1disabled/' /etc/selinux/config
        yum -y groupinstall "Development Tools" "Server Platform Development"
#返回一個值,判斷是否安裝成功
        [ $? -eq 0 ]  && return 2
}
nginx_install() {
        echo "開始安裝nginx"
        sleep 3
#編譯安裝
        yum -y install openssl-devel pcre-devel zlib-devel
        useradd -r nginx
        cd $tools
        tar xf nginx-1.10.3.tar.gz
        cd nginx-1.10.3/
        ./configure --prefix=$install/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.pid --lock-path=/var/lock/nginx.lock --with-http_ssl_module --with-http_gzip_static_module --with-debug --with-http_stub_status_module
        make && make install
#設置開機自啟動
        cp $tools/nginx /etc/rc.d/init.d/nginx
        chmod +x /etc/rc.d/init.d/nginx
        chkconfig --add nginx
        chkconfig nginx on

#啟動ngnix
        /etc/init.d/nginx start
#測試nginx是否啟動成功
        if curl http://127.0.0.1 &> /dev/null;then
                return 3
        fi
}
mysql_install(){
        echo "開始安裝Mysql..."
        sleep 3
#編譯安裝
        cd $tools
        wget http://dev.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6.30.tar.gz
        yum -y install cmake ncurses-devel
        useradd -r mysql
        tar xf mysql-5.6.30.tar.gz
        cd mysql-5.6.30/
        cmake -DCMAKE_INSTALL_PREFIX=$install/mysql -DMYSQL_UNIX_ADDR=$install/mysql/mysql.sock -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_MEMORY_STORAGE_ENGINE=1 -DWITH_PARTITION_STORAGE_ENGINE=1 -DMYSQL_DATADIR=$data -DMYSQL_TCP_PORT=3306 -DENABLE_DOWNLOADS=1 -DEXTRA_CHARSETS=all -DENABLED_LOCAL_INFILE=1 -DWITH_READLINE=1
        make -j 4 && make install
#初始化數據庫
        mkdir -p $data
        chown -R mysql.mysql $install/mysql
        chown -R mysql.mysql $data
        cd $install/mysql/
        ./scripts/mysql_install_db --user=mysql --datadir=$data
 #準備配置文件
        cp $install/mysql/support-files/my-default.cnf /etc/my.cnf
        sed -i "/\[mysqld\]/a datadir= $data\ninnodb_file_per_table= ON\nskip_name_resolve= ON" /etc/my.cnf
#準備啟動腳本
        cp support-files/mysql.server /etc/rc.d/init.d/mysqld
        sed  -i "s#^basedir=#basedir=$install/mysql#" /etc/init.d/mysqld
        sed  -i "s#^datadir=#datadir=$data#" /etc/init.d/mysqld
        chkconfig --add mysqld
        chkconfig --level 35 mysqld on
        service mysqld start
#設置環境變量
        echo "export PATH=$install/mysql/bin:$PATH" > /etc/profile.d/mysql.sh
        source /etc/profile.d/mysql.sh
#設置root密碼
#        $install/mysql/bin/mysqladmin -uroot -p password '123456'
#輸出頭文件
        ln -s $install/mysql/include/ /usr/include/mysql
#輸出庫文件
        echo "$install/mysql/lib/" >/etc/ld.so.conf.d/mysql.conf
        ldconfig
#測試
        if  ss -tnlp | grep mysqld &> /dev/null;then
                return 4
        fi
}
php_install(){
        echo "開始安裝php..."
        sleep 3
#編譯安裝libmcrpyt
        cd $tools
        tar xf libmcrypt-2.5.8.tar.bz2
        cd libmcrypt-2.5.8/
        ./configure --prefix=$install/libmcrypt
        make && make install
        sed -i "1a$install/libmcrypt/lib" /etc/ld.so.conf
#安裝依賴
        yum -y install php-pear
        yum -y install libxml2-devel libcurl-devel libjpeg-devel libpng-devel freetype freetype-devel
#編譯安裝php
        cd $tools
        tar xf php-5.6.30.tar.bz2
        cd php-5.6.30/
        ./configure --prefix=$install/php --with-config-file-path=$install/php  --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-iconv-dir --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir --enable-xml --disable-rpath --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --enable-mbregex --enable-fpm --enable-mbstring --with-gd --enable-gd-native-ttf --with-mhash  --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --with-mcrypt=$install/libmcrypt/ --with-gettext
        if [ $? -eq 0 ];then
                make && make install
        else
                return 5
        fi
#拷貝配置文件
        cp php.ini-production $install/php/etc/php.ini
        cp $install/php/etc/php-fpm.conf.default $install/php/etc/php-fpm.conf
#復制php-fpm啟動腳本到init.d,設置開機啟動
        cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
        cd
        chmod +x /etc/rc.d/init.d/php-fpm
        chkconfig --add php-fpm
        chkconfig php-fpm on
        service php-fpm start
#修改nginx配置文件使之支持php
        sed -i '/# pass/a location ~ \\\.php$ { \nroot           html; \nfastcgi_pass   127.0.0.1:9000; \nfastcgi_index  index.php; \nfastcgi_param  SCRIPT_FILENAME   $document_root$fastcgi_script_name;\ninclude        fastcgi_params; \n}' /etc/nginx/nginx.conf
# 啟動服務
        $install/nginx/sbin/nginx -t
        [ $? -eq 0 ] && systemctl reload nginx
        if ss -tnlp | grep php-fpm  &>/dev/null;then
                return  5
        fi
}
#主程序
tools=/usr/local/src
if [ $# -eq 0 ];then
        install=/usr/local
        data=/data/mysqldb
elif [$# -eq 2];then
        install=$1
        data=$2
else
        echo "Usage lnmp.sh install_dir mysqldata"
        exit 1
fi
system_ready
[ $? -ne 2 ] && echo "system ready have something wrong!!!"  && exit
nginx_install
[ $? -ne 3 ] && echo "The install of Nginx have something wrong!!" && exit
mysql_install
[ $? -ne 4 ] && echo "The install of mysql have something wrong!!" && exit
php_install
[ $? -ne 5 ] && echo "The install of php-fpm have something wrong!!" && exit
echo "lamp源碼安裝完畢!!!!!!!!!!!"

問題4: 腳本中無法使用service nginx start啟動服務
修改為/etc/init.d/nginx start
如果修改為/usr/local/nginx/sbin/nginx,后面systemctl reload nginx同樣出錯

問題5: sed腳本出錯,導致mysql安裝失敗

sed  -i 's#^basedir=#basedir=$install/mysql#' /etc/init.d/mysqld

解決方法: 把腳本中的單引號改成雙引號
備注: sed中可以使用變量,但記得使用雙引號

原創文章,作者:hansj,如若轉載,請注明出處:http://www.www58058.com/75185

(0)
hansjhansj
上一篇 2017-05-11
下一篇 2017-05-11

相關推薦

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

    1.描述計算機的組成及其功能     計算機的組成部分分為硬件部分與軟件部分         硬件部分: I/O設備 + 運算器 + 存儲器 + 控制器       &n…

    Linux干貨 2016-08-15
  • 小白易患錯誤之絕對路徑和相對路徑的操作錯誤

    小白易患錯誤之絕對路徑和相對路徑的操作錯誤 作為一個不安穩的小白,一天都在那路亂折騰,恰巧,老師課程題目中有一題將/etc/skel 這個目錄的文件除了..和. 復制到/home/USRNAEM 的家目錄下。然后自以為是不按照老師的方法,自己折騰用了這樣一條命令 [root@local skel]# ls -A .bash_lo…

    Linux干貨 2016-08-05
  • shell編程進階

    2、編寫腳本/root/bin/yesorno.sh,提示用戶輸入yes或no,并判斷用戶輸入的是yes還是no,或是其它信息 read -p “Enter you choice yes|no:” Choice Choice1=`echo $Choice | tr ‘[a-z]’ ‘[A-Z]&#8…

    2017-09-16
  • 相識–Varnish

    Varnish與一般服務器軟件類似,分為master(management)進程和child(worker,主要做cache的工作)進程。master進程讀入命令,進行一些初始化,然后fork并監控child進程。child進程分配若干線程進行工作,主要包括一些管理線程和很多woker線程。 VCL:?”域“專有類型的配置語言 VCL有多個狀態引擎,狀態之間…

    Linux干貨 2017-11-13
  • 命令組合實戰

    1 列出/etc/下以。conf結尾的文件 [redsun@jiange root]$ ls  /etc/*.conf | tr 'a-z' 'A-Z'  | sed 's/ETC/etc/' > /tmp/etc.conf [redsun@jiange root]$ mo…

    Linux干貨 2016-11-13
  • 讓自定義腳本成為服務腳本

    1.腳本注釋格式:此格式能讓chkconfig命令識別 #!/bin/bash#chkconfig:runlevel [S]##  [K]##    定義默認runlevel) (S開頭,定義啟動優先級) (K開頭,定義關閉優先級) #description:腳本說明:太長的話需要\換行 2.case語句實現start,stop…

    Linux干貨 2017-05-15

評論列表(1條)

  • 馬哥教育
    馬哥教育 2017-06-20 11:24

    寫的很好,完全可以當范文了,希望可以再接再厲,繼續保持

欧美性久久久久