54、基於FCGI方式安裝LAMP

192.168.130.64 mysql

192.168.130.65 php

192.168.130.66 http


1、安裝準備(三臺機器都需要執行)

firewall-cmd --state

systemctl stop firewalld

firewall-cmd --state

systemctl disable firewalld

sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config

setenforce 0



2、安裝mysql-5.7.24

2.1、卸載系統自帶Mariadb

find / -name mariadb*

yum -y remove mariadb-libs-5.5.44

find / -name mariadb*


2.2、安裝mysql

tar xf mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz  -C /usr/local/

mv /usr/local/mysql-5.7.24-linux-glibc2.12-x86_64 /usr/local/mysql


2.3、創建相關用戶及目錄

groupadd -r mysql

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

mkdir -p /mydata/data

mkdir -p /mydata/binlog

chown mysql:mysql /mydata/data

chown mysql:mysql /mydata/binlog


2.4、服務器啓動腳本

chown :mysql /usr/local/mysql -R

cd /usr/local/mysql/

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


2.5、創建配置文件

vim /etc/my.cnf 

[mysqld]

bind-address = 0.0.0.0

socket=/tmp/mysql.sock

basedir=/usr/local/mysql

datadir=/mydata/data

log-bin=/mydata/binlog/log-bin

server-id=1

skip-name-resolve


2.6、設置環境變量

echo "export PATH=/usr/local/mysql/bin:$PATH" >> /etc/profile.d/mysql.sh

source /etc/profile.d/mysql.sh


2.7、配置爲開機啓動

/usr/local/mysql/bin/mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/mydata/data

/etc/init.d/mysqld start

chkconfig --add mysqld

chkconfig --list mysqld

netstat -tuanlp | grep 3306


2.8、設置root密碼,授權遠程連接

mysql

SET PASSWORD = PASSWORD('root123');

GRANT ALL ON mysql.* TO 'root'@'localhost' IDENTIFIED BY 'root123';

GRANT ALL ON mysql.* TO 'root'@'127.0.0.1' IDENTIFIED BY 'root123';

GRANT ALL ON mysql.* TO 'testuser'@'192.168.%.%' IDENTIFIED BY 'testpass';

FLUSH PRIVILEGES;



3、安裝php-5.6.38

3.1、安裝依賴包

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

yum -y install https://mirrors.aliyun.com/epel/7Server/x86_64/Packages/e/epel-release-7-11.noarch.rpm

yum -y install bzip2-devel libmcrypt-devel libxml2-devel openssl-devel


3.2、安裝php

tar xf php-5.6.38.tar.gz

cd php-5.6.38

#./configure --prefix=/usr/local/php --with-mysql=/usr/local/mysql --with-openssl --with-mysqli=/usr/local/mysql/bin/mysql_config --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和MYSQL是分離的,所以替換爲如下

./configure --prefix=/usr/local/php --with-mysql=mysqlnd --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd --with-openssl --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

make && make install


3.3、複製配置文件

cp php.ini-production /etc/php.ini


3.4、爲php-fpm提供SysV init腳本

cp /root/php-5.6.38/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm

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

chkconfig --add php-fpm

chkconfig --list php-fpm

chkconfig php-fpm on


3.5、爲php-fpm提供配置文件:

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

vim /usr/local/php/etc/php-fpm.conf

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

listen = 0.0.0.0:9000 

pm.max_children = 50

pm.start_servers = 5

pm.min_spare_servers = 2

pm.max_spare_servers = 8


3.6、啓動php-fpm

service php-fpm start

netstat -tuanlp | grep 9000


3.7、創建目錄及編寫測試頁

mkdir /var/www/html/{one,two} -p

cat /var/www/html/one/index.php

<?php

  $link = mysql_connect('192.168.130.64','testuser','testpass');

  if ($link)

    echo "www.a.com連接數據庫成功";

  else

    echo "www.a.com連接數據庫失敗";

  mysql_close();

  phpinfo();

?>


cat /var/www/html/two/index.php

<?php

  $link = mysql_connect('192.168.130.64','testuser','testpass');

  if ($link)

    echo "www.b.com連接數據庫成功";

  else

    echo "www.b.com連接數據庫失敗";

  mysql_close();

  phpinfo();

?>


4、安裝httpd-4.4.37

4.1安裝依賴包

yum -y install pcre-devel expat-devel openssl-devel

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


4.2、安裝arp、arp-util

tar xf apr-1.6.5.tar.gz

cd apr-1.6.5

./configure --prefix=/usr/local/apr

make && make install

tar xf apr-util-1.6.1.tar.gz

cd apr-util-1.6.1

./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr/

make && make install


4.3、安裝httpd

tar xf httpd-2.4.37.tar.gz

cd httpd-2.4.37

./configure --prefix=/usr/local/apache --sysconfdir=/etc/httpd24 --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util/ --enable-modules=most --enable-mpms-shared=all --with-mpm=event

make && make install


4.4、測試

/usr/local/apache/bin/apachectl start

netstat -tuanlp | grep 80

/usr/local/apache/bin/apachectl stop

netstat -tuanlp | grep 80


4.5、服務器啓動腳本

cat /etc/init.d/httpd 

#!/bin/bash

#

# httpd        Startup script for the Apache HTTP Server

#

# chkconfig: - 85 15

# description: Apache is a World Wide Web server.  It is used to serve \

#        HTML files and CGI.

# processname: httpd

# config: /etc/httpd/conf/httpd.conf

# config: /etc/sysconfig/httpd

# pidfile: /var/run/httpd.pid


# Source function library.

. /etc/rc.d/init.d/functions


if [ -f /etc/sysconfig/httpd ]; then

        . /etc/sysconfig/httpd

fi


# Start httpd in the C locale by default.

HTTPD_LANG=${HTTPD_LANG-"C"}


# This will prevent initlog from swallowing up a pass-phrase prompt if

# mod_ssl needs a pass-phrase from the user.

INITLOG_ARGS=""


# Set HTTPD=/usr/sbin/httpd.worker in /etc/sysconfig/httpd to use a server

# with the thread-based "worker" MPM; BE WARNED that some modules may not

# work correctly with a thread-based MPM; notably PHP will refuse to start.


# Path to the apachectl script, server binary, and short-form for messages.

apachectl=/usr/local/apache/bin/apachectl

httpd=${HTTPD-/usr/local/apache/bin/httpd}

prog=httpd

pidfile=${PIDFILE-/var/run/httpd.pid}

lockfile=${LOCKFILE-/var/lock/subsys/httpd}

RETVAL=0


start() {

        echo -n $"Starting $prog: "

        LANG=$HTTPD_LANG daemon --pidfile=${pidfile} $httpd $OPTIONS

        RETVAL=$?

        echo

        [ $RETVAL = 0 ] && touch ${lockfile}

        return $RETVAL

}


stop() {

  echo -n $"Stopping $prog: "

  killproc -p ${pidfile} -d 10 $httpd

  RETVAL=$?

  echo

  [ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile}

}

reload() {

    echo -n $"Reloading $prog: "

    if ! LANG=$HTTPD_LANG $httpd $OPTIONS -t >&/dev/null; then

        RETVAL=$?

        echo $"not reloading due to configuration syntax error"

        failure $"not reloading $httpd due to configuration syntax error"

    else

        killproc -p ${pidfile} $httpd -HUP

        RETVAL=$?

    fi

    echo

}


# See how we were called.

case "$1" in

  start)

  start

  ;;

  stop)

  stop

  ;;

  status)

        status -p ${pidfile} $httpd

  RETVAL=$?

  ;;

  restart)

  stop

  start

  ;;

  condrestart)

  if [ -f ${pidfile} ] ; then

    stop

    start

  fi

  ;;

  reload)

        reload

  ;;

  graceful|help|configtest|fullstatus)

  $apachectl $@

  RETVAL=$?

  ;;

  *)

  echo $"Usage: $prog {start|stop|restart|condrestart|reload|status|fullstatus|graceful|help|configtest}"

  exit 1

esac


exit $RETVAL



chmod +x /etc/init.d/httpd


4.6、修改配置文件

cat /etc/httpd24/httpd.conf

PidFile "/var/run/httpd.pid"

Listen 0.0.0.0:80


4.7、設置環境變量

echo "export PATH=/usr/local/apache/bin:$PATH" >> /etc/profile.d/httpd.sh

source /etc/profile.d/httpd.sh


4.8、設置開機啓動

chkconfig --add httpd

chkconfig httpd on

chkconfig --list httpd


4.9、啓動服務

/etc/init.d/httpd start

netstat -tuanlp | grep 80


4.10、修改配置文件以支持fcgi和php

vim /etc/httpd24/httpd.conf

LoadModule proxy_module modules/mod_proxy.so

LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so

AddType application/x-httpd-php  .php

AddType application/x-httpd-php-source  .phps

DirectoryIndex  index.php  index.html


#DocumentRoot "/usr/local/apache/htdocs"

Include /etc/httpd24/extra/httpd-vhosts.conf


4.11、虛擬機主機配置

grep -v ^# /etc/httpd24/extra/httpd-vhosts.conf 

<VirtualHost *:80>

    ServerAdmin [email protected]

    DocumentRoot "/var/www/html/one"

    ServerName www.one.com

    ServerAlias one.com

    ErrorLog "logs/one.com-error_log"

    CustomLog "logs/one.com-access_log" combined

    ProxyRequests Off

    ProxyPassMatch ^/(.*\.php)$ fcgi://192.168.130.66:9000/var/www/html/one/$1

    <Directory "/var/www/html/one">

        Options none

        Require all granted

    </Directory>

</VirtualHost>



<VirtualHost *:80>

    ServerAdmin [email protected]

    DocumentRoot "/var/www/html/two"

    ServerName www.two.com

    ServerAlias two.com

    ErrorLog "logs/two.com-error_log"

    CustomLog "logs/two.com-access_log" combined

    ProxyRequests Off

    ProxyPassMatch ^/(.*\.php)$ fcgi://192.168.130.66:9000/var/www/html/two/$1

    <Directory "/var/www/html/two">

        Options none

        Require all granted

    </Directory>

</VirtualHost>


4.12、創建對應目錄並重啓服務器 

mkdir /var/www/html/{one,two} -p

service httpd restart


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章