LAMP安裝教學 (下) – Centos 7 + Apache + MariaDB 10 + PHP 7

LAMP安裝教學 (下) – Centos 7 + Apache + MariaDB 10 + PHP 7

前一篇LAMP安裝教學 (上) 文章內容提要:月流量為8TB,平均同時在線人數大約為500人左右,現在 VPS伺服器的 TTFB過高,伺服器規格為:8CPU、16RAM、200GB SSD。

敦緯數位服務提供了以下規格的實體伺服器主機,使用SASS硬碟,4CPU、16G RAM、1TB SASS,經過三天的伺服器優化以及壓力測試,直接完勝本來的VPS伺服器,以下為優化結果:

項目某主機商敦緯數位
TTFB 5 - 8 秒(s)868毫秒(ms)
Load10.80秒(s)5.68秒(s)
Finish33.91秒(s)27.76秒(s)

接下來將繼續設定並最佳化 LAMP伺服器 ,此篇LAMP教學文章安裝的作業系統為 Centos 7 + Apache 2.4 + MariaDB 10 + PHP 7.3,如果大家實做過後有什麼問題,歡迎大家點選頁面旁的FB聊天圖示,或是填寫聯絡表單小編會盡快回覆您的問題。

安裝 ImageMagick、PHP imagick 圖片編輯工具

(1) 安裝 PHP imagick

先安裝 php-pear 及 gcc ,用來編譯 PHP 的 imagick 模組:

yum install -y php-pear gcc
yum install -y php-imagick

(2) 安裝 ImageMagick

pecl install imagick

如果出現此警告訊息,請先執行 pecl channel-update pecl.php.net

WARNING: channel "pecl.php.net" has updated its protocols, use "pecl channel-update pecl.php.net" to update
pecl channel-update pecl.php.net

接著再執行一次pecl install imagick

pecl install imagick
downloading imagick-3.4.4.tgz ...
Starting to download imagick-3.4.4.tgz (253,434 bytes)
.....................................................done: 253,434 bytes
19 source files, building
running: phpize
Configuring for:
PHP Api Version:         20170718
Zend Module Api No:      20170718
Zend Extension Api No:   320170718
Please provide the prefix of ImageMagick installation [autodetect] : autodetect

Build process completed successfully
Installing '/usr/lib64/php/modules/imagick.so'
Installing '/usr/include/php/ext/imagick/php_imagick_shared.h'
install ok: channel://pecl.php.net/imagick-3.4.4
configuration option "php_ini" is not set to php.ini location
You should add "extension=imagick.so" to php.ini

(3) 重啟Apache,使 imagick 套件生效

systemctl restart httpd

(4) 檢查 imagick 套件是否正常運作

php -m |grep imagick
imagick

設定 Apache 參數

(1) 先備份初始 http.conf 文件

cp /etc/httpd/conf/httpd.conf /etc/httpd/conf/httpd.conf.bak

(2) PHP基本設定

vi /etc/httpd/conf/httpd.conf

依需求自行修改
Timeout:調整timeout時間(秒)
KeepAlice:連續請求的功能
MaxKeepAliveRequests:Client 端建立連線後,可以要求最大的檔案數
KeepAliveTimeout:Clinet 端多久沒有在繼續請求就把他踢下線(秒)
ServerTokens:顯示或隱藏 HTTP 檔頭內 Apache、Nginx、PHP 的版本
RequestReadTimeout:防止DDOS,設定header完成的允許時間(秒)

Timeout 60
KeepAlive On
MaxKeepAliveRequests 500
KeepAliveTimeout 15
ServerTokens Prod
RequestReadTimeout header=20-40,MinRate=500 body=20,MinRate=500

(3) 重啟Apache之前,請先檢查是否有錯誤

apachectl -t
Syntax OK

(4) 重啟Apache,使 PHP 設定生效

systemctl restart httpd

贊助廣告

設定 Apache 進程模組

(1) 先備份初始 00-mpm.conf 文件

cp /etc/httpd/conf.modules.d/00-mpm.conf /etc/httpd/conf.modules.d/00-mpm.conf.bak

(2) mpm_prefork 基本設定

vi /etc/httpd/conf.modules.d/00-mpm.conf

依需求自行修改
註解
#LoadModule mpm_prefork_module modules/mod_mpm_prefork.so
取消註解
LoadModule mpm_worker_module modules/mod_mpm_worker.so

# Select the MPM module which should be used by uncommenting exactly
# one of the following LoadModule lines:

# prefork MPM: Implements a non-threaded, pre-forking web server
# See: http://httpd.apache.org/docs/2.4/mod/prefork.html
#LoadModule mpm_prefork_module modules/mod_mpm_prefork.so

# worker MPM: Multi-Processing Module implementing a hybrid
# multi-threaded multi-process web server
# See: http://httpd.apache.org/docs/2.4/mod/worker.html
#
LoadModule mpm_worker_module modules/mod_mpm_worker.so

# event MPM: A variant of the worker MPM with the goal of consuming
# threads only for connections with active processing
# See: http://httpd.apache.org/docs/2.4/mod/event.html
#
#LoadModule mpm_event_module modules/mod_mpm_event.so

(3) 重啟Apache之前,請先檢查是否有錯誤

apachectl -t
Syntax OK

(4) 重啟Apache,使 mpm_prefork 設定生效

systemctl restart httpd

(5) 檢查是否成功切換進程模組

apachectl -V
Server version: Apache/2.4.6 (CentOS)
Server built:   Apr 24 2019 13:45:48
Server's Module Magic Number: 20120211:24
Server loaded:  APR 1.4.8, APR-UTIL 1.5.2
Compiled using: APR 1.4.8, APR-UTIL 1.5.2
Architecture:   64-bit
Server MPM:     worker
  threaded:     yes (fixed thread count)
    forked:     yes (variable process count)

設定 MariaDB 初始化

(1) 執行初始化指令

mysql_secure_installation
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): enter 
#如果從來未建立過root使用者,直接按enter鍵繼續

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] y #設定root密碼
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] y #是否移除訪客使用者

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] y #是否移除訪客使用者
 ... 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] y #是否禁止root遠端連線
 ... Success!

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] y #是否移除test使用者的資料表以及權限
 - 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!

Cleaning up...

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

Thanks for using MariaDB!

(2) 重啟Mariadb,檢查MariaDB可正常啟動

systemctl restart mariadb

因應 8CPU、16RAM、流量1TB/月的進階設定

請與我聯繫,領取apache、PHP、MariaDB的設定檔

需要設定檔的人可以與我聯繫,目前小編會免費給予設定檔,但條件是:
1、按讚
2、分享至FB
3、截圖後,透過旁邊的FB聊天圖示傳給小編
小編將會傳設定檔給你,一旦發現有人隨便散發設定檔,我就會開始收費唷,畢竟這是消篇辛苦熬夜測試得心血。

高嘉松Andy

這個風風雨雨個社會  欲怎樣開花

少年家怎樣落地 咱攏是為著愛情來浪流連

Facebook 留言
(總點閱次數 : 1,729 , 今日點閱數數 : 4 )
Close Panel