1. 程式人生 > >三臺主機CentOS7 編譯安裝lamp

三臺主機CentOS7 編譯安裝lamp

servers 啟動 figure tor dex tab .cn freetype firewalld

作業題:三臺主機編譯安裝http、php,二進制源碼安裝mariadb
環境:
A主機:192.166.0.161,編譯安裝httpd
B主機:192.166.0.162,編譯安裝php-fpm
C主機:192.166.0.163,二進制格式安裝mariadb

三臺主機均關閉了firewalld、selinux,軟件存放目錄均為/root/tools/

一:安裝http
安裝擴展組件

yum -y groupinstall "開發工具" "服務器平臺開發"
yum install -y pcre-devel openssl-devel gcc 

安裝apr

 wget http://archive.apache.org/dist/apr/apr-1.5.2.tar.bz2
  tar -jxvf apr-1.5.2.tar.bz2
  cd apr-1.5.2/
  ./configure --prefix=/usr/local/apr
  make -j 2 && make install

安裝apr-util

null
null
null
null
wget http://archive.apache.org/dist/apr/apr-util-1.5.4.tar.gz
   tar -zxvf apr-util-1.5.4.tar.gz 
   cd apr-util-1.5.4/
   ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
   make -j 2 && make install

安裝 httpd-2.4

 wget http://archive.apache.org/dist/httpd/httpd-2.4.23.tar.bz2
  tar -jxvf httpd-2.4.23.tar.bz2 
  cd httpd-2.4.23/
  ./configure --help  // 根據需要選擇對應的模塊
  ./configure   --prefix=/usr/local/apache   --sysconfdir=/etc/httpd   --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  //指定httpd的MPM方式,分別是prefork、event和worker

  
  make -j 2 && make install

設置 開機啟動

 cp /usr/local/apache/bin/apachectl /etc/init.d/httpd
    vim /etc/init.d/httpd
      添加:
                #chkconfig: 2345 70 30   //2345是運行級別,70是啟動順序,30是關閉順序
    chkconfig --add httpd   //添加httpd服務
    chkconfig --list httpd  //查看httpd是否開機啟動

添加環境變量:
vim /etc/profile.d/httpd.sh,添加以下內容
export PATH=/usr/local/apache/bin:$PATH

導出頭文件
ln -sv /usr/local/apache/include /usr/include/apache

啟動httpd服務

  systemctl start httpd
  systemctl status httpd

修改配置文件

vim /etc/httpd/httpd.conf 
    註釋 #DocumentRoot "/usr/local/apache/htdocs" 
    並打開啟用 include /etc/httpd/conf.d/*.conf
     添加 ServerName www.200.com
    同時定位 AddType;添加下面兩行
    AddType application/x-httpd-php .php
    AddType application/x-httpd-php-source .phps
    並且定位至DirectoryIndex
    <IfModule dir_module>
    DirectoryIndex index.php index.html #添加index.php(最好添加在最前面)
    </IfModule>
    還要啟用proxy_module proxy-fcgi_module等
    LoadModule proxy_module modules/mod_proxy.so #開啟
    LoadModule proxy_connect_module modules/mod_proxy_connect.so
    LoadModule proxy_ftp_module modules/mod_proxy_ftp.so
    LoadModule proxy_http_module modules/mod_proxy_http.so
    LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so #開啟  

    
   mkdir /etc/httpd/conf.d
   vim /etc/httpd/conf.d/vhost.conf
    添加兩臺虛擬主機
    <VirtualHost *:80>
        ServerAdmin [email protected]
        ServerName www1.200.com
        DocumentRoot ‘/var/www1‘
        ErrorLog  ‘/var/log/httpd/www1_error_log‘
        CustomLog  ‘/var/log/httpd/www1_access_log‘ combined
        ProxyRequests off //關閉正向代理
        ProxyPassMatch ^/(.*\.php)$ fcgi://192.166.0.162:9000/var/www1/$1
        <Directory ‘/var/www1‘>
                Options FollowSymLinks
                AllowOverride none
                Require all granted
        </Directory>
    </VirtualHost>

    
    <VirtualHost *:80>
        ServerAdmin [email protected]
        ServerName www2.200.com
        DocumentRoot ‘/var/www2‘
        ErrorLog  ‘/var/log/httpd/www2_error_log‘
        CustomLog  ‘/var/log/httpd/www1_access_log‘ combined
        ProxyRequests off //關閉正向代理
        ProxyPassMatch ^/(.*\.php)$ fcgi://192.166.0.162:9000/var/www2/$1
        <Directory ‘/var/www2‘>
                Options FollowSymLinks
                AllowOverride none
                Require all granted
        </Directory>
    </VirtualHost>

    
    mkdir -pv /var/www{1,2}
    mkdir -pv /var/log/httpd
添加測試文件:
 vim /var/www1/index.html
      <h1> this is www1.200.com </h1>
    vim /var/www2/index.html
      <h1> this is www2.200.com </h1>
檢查語法錯誤
/usr/local/apache/bin/apachectl -t`




二、安裝php

yum -y groupinstall "開發工具" "服務器平臺開發"
    yum -y install libxml2-devel libmcrypt-devel gcc
    tar -jxvf php-5.4.16.tar.bz2
    cd php-5.4.16/

    
    ./configure 
    --prefix=/usr/local/php     --enable-fpm     --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     --with-mcrypt=/usr/local/mcrypt      --with-config-file-path=/etc     --with-config-file-scan-dir=/etc/php.d     --with-bz2      --enable-maintainer-zts
    `make -j 2 && make install`


註意:
一、安裝gcc 由於是獨立的主機 需要配置環境 yum -y install gcc
二、 –with-mysql幾個選項都要為mysqlnd;因為mysql服務器單獨為另一臺主機
三、為了支持apache的worker或event這兩個MPM,編譯時使用了–enable-maintainer-zts選項

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


配置fpm 
為php-fpm提供Sysv init腳本,並將其添加至服務列表:
cp 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
chkconfig –list php-fpm




為php-fpm提供配置文件:
cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf  

編輯php-fpm的配置文件:
vim /usr/local/php/etc/php-fpm.conf


修改配置php-fpm的文件vim /usr/local/php/etc/php-fpm 
定位到listen =192.166.0.162:9000
配置fpm的相關選項為你所需要的值:


pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 2
pm.max_spare_servers = 8 

接下來就可以啟動php-fpm了:
service php-fpm start
可以使用netstat -tunlp 查看 9000端口


安裝Xchache
安裝xcache
wget http://xcache.lighttpd.net/pub/Releases/3.1.0/xcache-3.1.0.tar.gz
tar zxvf xcache-3.1.0.tar.gz 
cd xcache-3.1.0/
/usr/local/php/bin/phpize
./configure --enable-xcache --with-php-config=/usr/local/php/bin/php-config
make && make install
/usr/local/php/bin/php -m       //查看是否啟用xcache,此時並未啟用
cp ./xcache.ini /etc/php.d/
mkdir /etc/php.d
cp ./xcache.ini /etc/php.d/     
systemctl restart php-fpm
/usr/local/php/bin/php -m       
    [Zend Modules]
    XCache
    XCache Cacher           //表示已經啟用xcache




三、二進制格式安裝mariadb

groupadd -r mysql
useradd -r -g mysql mysql
tar -axf mariadb-10.1.13-linux-x86_64.tar.gz -C /usr/local/
cd /usr/local/
ln -s mariadb-10.1.13-linux-x86_64/ mysql
cd /usr/local/mysql
chown -R root:mysql ./*
mkdir /mydata   //備註:數據庫存放目錄。
chown -R mysql.mysql /mysqldata
cp  support-files/mysql.server   /etc/init.d/mysqld
ll /etc/init.d/mysqld 
chkconfig --add mysqld
chkconfig --list mysqld
mv /etc/my.cnf{,.bak}
mkdir /etc/mysql
cp support-files/my-large.cnf /etc/mysql/my.cnf
vim /etc/mysql/my.cnf
    datadir = /mydata
    innodb_file_per_table = on
    skip_name_resolve = on


vi /etc/profile.d/mysql.sh
    export PATH=/usr/local/mysql/bin:$PATH  
source /etc/profile.d/mysql.sh
mysql_secure_installation //初始化安全設置
scripts/mysql_install_db  --user=mysql  --datadir=/mysqldata
mysql -uroot -hlocalhost -p


MariaDB [(none)]> create user ‘test‘@‘192.166.%.%‘ identified by ‘123456‘;
MariaDB [(none)]> create database wordpress_db;     //創建WordPress的數據庫,為後面wp-config.php裏面填入的數據庫
MariaDB [(none)]> grant all privileges on *.* to ‘test‘@‘192.166.%.%‘;
MariaDB [(none)]> grant all privileges on *.* to ‘root‘@‘192.166.%.%‘;  //運行root用戶在遠程主機上登陸,即能在PhpMyadmin上登陸root賬號
vi /etc/ld.so.conf.d/mysql.conf
    /usr/local/mysql/lib
ldconfig
ldconfig -p | grep mysql
ln -sv /usr/local/mysql/include/ /usr/include/mysql



四、安裝WordPress和PhpMyAdmin


安裝WordPress
        在httpd主機和php主機上均安裝一次,靜態資源http主機之間響應,動態資源http服務器發送給php處理後響應

    
    http主機上
    unzip wordpress-4.9.1-zh_CN.zip -d /var/www1/
    ls /var/www1/
    cd /var/www1/
    mv wordpress/* ./
    cp wp-config-sample.php wp-config.php
    vim wp-config.php
        // define(‘DB_NAME‘, ‘database_name_here‘);
        define(‘DB_NAME‘, ‘wordpress_db‘);


        /** MySQL數據庫用戶名 */
        define(‘DB_USER‘, ‘test‘);


        /** MySQL數據庫密碼 */
        define(‘DB_PASSWORD‘, ‘123456‘);


        /** MySQL主機 */
        define(‘DB_HOST‘, ‘192.166.0.163‘);     

    
    scp wordpress-4.9.1-zh_CN.zip [email protected]:/root/tools/  

    
    在php主機上
    unzip wordpress-4.9.1-zh_CN.zip -d /var/www1/
    ls /var/www1/
    cd /var/www1/
    mv wordpress/* ./
    cp wp-config-sample.php wp-config.php
    vim wp-config.php
        // define(‘DB_NAME‘, ‘database_name_here‘);
        define(‘DB_NAME‘, ‘wordpress_db‘);


        /** MySQL數據庫用戶名 */
        define(‘DB_USER‘, ‘test‘);


        /** MySQL數據庫密碼 */
        define(‘DB_PASSWORD‘, ‘123456‘);


        /** MySQL主機 */
        define(‘DB_HOST‘, ‘192.166.0.163‘);

        
    安裝PhpMyadmin
        在httpd主機和php主機上均安裝一次,靜態資源http主機之間響應,動態資源http服務器發送給php處理後響應
    unzip phpMyAdmin-4.4.14.1-all-languages.zip 
    mv phpMyAdmin-4.4.14.1-all-languages/* /var/www2/
    cd /var/www2/
    cp config.sample.inc.php config.inc.php
    openssl rand -base64 15 //產生15位隨機數
    vim config.inc.php  
        $cfg[‘blowfish_secret‘] = ‘cG//9oBhIt/t2rfm6AVW‘; /* YOU MUST FILL IN THIS FOR COOKIE AUTH! */
        /* Authentication type */
        $cfg[‘Servers‘][$i][‘auth_type‘] = ‘cookie‘;
        /* Server parameters */
        $cfg[‘Servers‘][$i][‘host‘] = ‘192.166.0.163‘;
        $cfg[‘Servers‘][$i][‘connect_type‘] = ‘tcp‘;
        $cfg[‘Servers‘][$i][‘compress‘] = false;
        $cfg[‘Servers‘][$i][‘AllowNoPassword‘] = false;

    
    兩臺主機安裝好後,訪問測試,root用戶和test用戶均能登陸



    
    mariadb二進制包安裝詳細參考:http://blog.51cto.com/wuqingcong/2059013

三臺主機CentOS7 編譯安裝lamp