標籤:centos 7 amp + xcache php-fpm rpm包
1、實驗需求:
1)CentOS 7, amp + xcache, rpm包,php-fpm;
a) httpd, php, mariadb分別部署在一個單獨的主機上;
b) 一個虛擬機器主機提供wordpress,另一個虛擬機器主機提供;phpMyAdmin
c) 為phpMyAdmim提供https服務;
2、實驗環境:
1)伺服器環境
Linux伺服器作業系統版本:CentOS release 6.7 (Final)
http) IP: 172.16.66.60
php-fpm) IP:172.16.66.70
mariadb) IP:172.16.66.70
2)測試環境
WIN7系統客戶機):IP:172.16.66.100
3、實驗前提:
1)關閉防火牆和SELinux
~]# service iptables stop
~]# sed -i s/SELINUX=enforcing/SELINUX=disabled/g /etc/selinux/config
4、實驗過程:
第一部分:
部署主機IP: 172.16.66.60
1 基本設定
1.1 設定 httpd 主機名稱 HOSTNAME
echo "HOSTNAME=www1" >> /etc/sysconfig/network
1.2 更新 HOSTS 設定檔 /etc/hosts
vim /etc/hosts
172.16.66.60 www1
1.3 修改 DNS 解析設定
vim /etc/resolv.conf
2 安裝 LAMP
2.2 安裝並配置 Apache 網路伺服器
yum install httpd
2.3備份設定檔(建議對於所有的設定檔,做任何更改前都先備份一份,以便應對未知錯誤)
mkdir ~/confbak
cp -R /etc/httpd ~/confbak
其中 ~ 表示當前登入使用者的使用者檔案夾;-R 參數表示遞迴到所有子目錄。
2.4配置虛擬機器主機(/etc/httpd/conf.d/www1.conf )
vim /etc/httpd/conf.d/www1.conf
主機www1
[[email protected] conf.d]# cat www1.conf
<VirtualHost *:80>
ServerName www1
# ServerAlias www
DocumentRoot /data/vhosts/www1 #注意這行末尾不要帶 /
ProxyRequests Off
DirectoryIndex index.php
ProxyPassMatch ^/(.*\.php)$ fcgi://172.16.66.70:9000/data/vhosts/www1/$1
<Directory "/data/vhosts/www1">
Options None
AllowOverride None
Require all granted
</Directory>
ErrorLog logs/www1-error_log
CustomLog logs/www1-access_log combien
#ServerSignature Off
</VirtualHost>
2.5配置虛擬機器主機(/etc/httpd/conf.d/www2.conf )
vim /etc/httpd/conf.d/www2.conf
主機 www2
[[email protected] conf.d]# cat www2.conf
<VirtualHost *:80>
ServerName www2
DocumentRoot /data/vhosts/www2
ProxyRequests Off
DirectoryIndex index.php
ProxyPassMatch ^/(.*\.php)$ fcgi://172.16.66.70:9000/data/vhosts/www2/$1
<Directory "/data/vhosts/www2">
Options None
AllowOverride None
Require all granted
</Directory>
ErrorLog logs/www2-error_log
CustomLog logs/www2-access_log combien
#ServerSignature Off
</VirtualHost>
2.6為虛擬機器主機建立(網站目錄)
主機 1 的
mkdir /data/vhosts/www1/ -p
主機 2 的
mkdir /data/vhosts/www2/ -p
2.7為了能夠在系統啟動時自動運行 Apache 伺服器,需要運行下面的指令:
systemctl enable httpd
輸出類似於
ln -s ‘/usr/lib/systemd/system/httpd.service‘ ‘/etc/systemd/system/multi-user.target.wants/httpd.service‘
2.8啟動 Apache 服務
systemctl start httpd
2.9提示Apache已啟動重啟載入
systemctl reload httpd
現在需要將 http 服務加入防火牆以允許外部存取(也就是將 HTTP 預設使用的連接埠 80 加入到防火牆允許清單裡),
firewall-cmd --add-service=http --permanent
–permanent 參數表示這是一條永久防火牆規則,如果不加這個參數則重啟系統後就沒有這條規則了。
重啟 Firewalld 使該規則生效
systemctl restart firewalld
如果防火牆預設沒有啟動,則上述指令會提示錯誤,“FirewallD is not running”。那麼先啟用防火牆服務。
systemctl enable firewalld && systemctl start firewalld
如果要查看加入後的防火牆規則,使用如下命令。
firewall-cmd --list-all
總結一下關鍵點,httpd 服務預設設定檔:
預設設定檔: /etc/httpd/conf/httpd.conf
載入模組的設定檔: /etc/httpd/conf.modules.d/ directory (e.g. PHP)
選擇 MPMs (處理模式) [worker, prefork (預設是這個)] 和 event: /etc/httpd/conf.modules.d/00-mpm.conf
預設連接埠: 80 和 443 (SSL)
預設日誌: /var/log/httpd/{access_log,error_log}
還可以直接用 apachectl 來控制 Apache 服務執行一些操作,比如優雅地重新載入配置,
apachectl graceful
“優雅地”的意思是不中斷客戶的訪問的情況下逐漸地將所有 httpd 進程更新為使用新配置的新進程。
詳情需要查看其簡單的協助檔案,
apachectl -h
其它重要的防火牆 Firewalld 選項有,
# firewall-cmd --state
# firewall-cmd --list-all
# firewall-cmd --list-interfaces
# firewall-cmd --get-service
# firewall-cmd --query-service service_name
# firewall-cmd --add-port=8080/tcp
第二部分:
部署php-fpm主機IP: 172.16.66.70
1安裝和配置 php-fpm
1.1安裝 php-fpm
yum install php-fpm mysql-server -y
1.2備份設定檔 /etc/php.ini,還有 php.conf 以及 00-php.conf,
cp /etc/php.ini ~/confbak/php.ini.bak
cp /etc/httpd/conf.d/php.conf ~/confbak/httpd/conf.d/php.conf.bak
cp /etc/httpd/conf.modules.d/00-php.conf ~/confbak/httpd/conf.modules.d/00-php.conf.bak
1.3並確保 /etc/php.ini 中有下面的語句(不同的就修改,沒有的就添加,某些數值可以後再調整,這是針對一個簡單的運行 WordPress 的伺服器的配置):
error_reporting = E_COMPILE_ERROR|E_RECOVERABLE_ERROR|E_ERROR|E_CORE_ERROR
display_errors = Off
log_errors = On
max_execution_time = 300
memory_limit = 32M
2安裝和配置php-mysql (為了在 PHP 中使用 MySQL,還需要安裝這個 php-mysql 包):
2.1安裝php-mysql
yum install php-mysql -y
第三部分:
部署mariadb主機IP: 172.16.66.80
1安裝和配置 Mariadb 資料庫服務
MariaDB 是在 MySQL 基礎上重建的一個資料庫軟體,各 Linux 發行版都陸陸續續從 MySQL 切換到了 MariaDB。CentOS 從 7 開始預設使用 MariaDB。
1.1安裝
yum install mariadb-server mariadb
1.2 加入隨系統啟動
systemctl enable mariadb
1.3 啟動 mariadb 守護進程(mysqld)其預設使用者名還是 mysql
systemctl start mariadb
1.4以查看記憶體佔用情況。
top -u mysql
1.5停止/重啟或停用 mariadb 服務的一些指令:
停止
sudo systemctl stop mariad
重啟
sudo systemctl restart mariadb
禁用
sudo systemctl disable mariadb
檢查
sudo systemctl is-active mariadb
2安全配置 MariaDB
使用 MariaDB 內建的安全配置指令碼進行配置
mysql_secure_installation
這裡需要配置 mysql 根使用者和密碼、清除其他使用者、清除不需要的資料庫等。輸出類似於下面的執行過程,其中需要我們從鍵盤輸入的內容用藍色注釋出來了:
/usr/bin/mysql_secure_installation
/usr/bin/mysql_secure_installation: line 379: find_mysql_client: command not found
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):這裡直接斷行符號,這裡可不是 Linux root 使用者,而是 MariaDB 資料庫的 root 使用者
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
New password:輸入你的資料庫root使用者密碼
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 --刪除匿名使用者?
... 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 --刪除測試資料庫和訪問嗎?
- 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.1輸入密碼後斷行符號,下面是輸出樣本,可以看到命令提示字元變為 MariaDB [(none)]>
mysql -u root -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 10
Server version: 5.5.37-MariaDB MariaDB Server
Copyright (c) 2000, 2014, Oracle, Monty Program Ab and others.
Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.
MariaDB [(none)]>
2.1建立一個新資料庫給 WordPress 用(這裡取名為 wordpress,也可以用別的名字)
MariaDB [(none)]> create database wordpress;
2.3建立一個新使用者,並將該資料庫的許可權賦給他(這裡只是舉例,使用者名稱為 ly,密碼為 lyuserpassword)
MariaDB [(none)]> grant all on wordpress.* to ‘ly‘@‘172.16.%.%‘ identified by ‘lyuserpassword‘;
2.4更新許可權
MariaDB [(none)]> flush privileges;
2.5退出資料庫
MariaDB [(none)]> quit
2.6備份設定檔
cp /etc/my.cnf ~/confbak/my.cnf.bak
第四部分: 安裝和配置 wordpress,phpMyAdmin
在php-fpm主機環境中 IP: 172.16.66.70
1.安裝和配置wordpress
1.1解壓wordpress包
tools]# unzip wordpress-4.3.1-zh_CN.zip
1.2拷貝到網站目錄www1中
cp wordpress /data/vhosts/www1
1.3改名wordpress設定檔為wp-config.php
]# cp wp-config-sample.php wp-config.php
1.4修改wp-config.php檔案串連資料庫
~]# sed -n ‘22,38p‘ /data/vhosts/www1/wordpress/wp-config.php
/** WordPress資料庫的名稱 */
define(‘DB_NAME‘, ‘wordpress‘);
/** MySQL資料庫使用者名稱 */
define(‘DB_USER‘, ‘ly‘);
/** MySQL資料庫密碼 */
define(‘DB_PASSWORD‘, ‘liyang‘);
/** MySQL主機 */
define(‘DB_HOST‘, ‘172.16.66.80‘);
/** 建立資料表時預設的文字編碼 */
define(‘DB_CHARSET‘, ‘utf8‘);
/** 資料庫清理類型。如不確定請勿更改 */
define(‘DB_COLLATE‘, ‘‘);
2.安裝和配置phpMyAdmin
2.1解壓phpMyAdmin包
tools]# unzip phpMyAdmin-4.4.14.1-all-languages.zip
2.2拷貝到網站目錄www2中
~]# cp -r phpMyAdmin-4.4.14.1-all-languages /data/vhosts/www2
2.3配置phpMyAdmin軟體
# ln -sv phpMyAdmin-4.4.14.1-all-languages/ phpMyAdmin
2.4改名設定檔名
~]# cp config.sample.inc.php config.inc.php
2.5產生隨機數
~]# openssl rand -hex 8 #-->(640b56f72820ace8)
2.6修改設定檔config.inc.php
~]# vim config.inc.php
$cfg[‘blowfish_secret‘] = ‘640b56f72820ace8‘
3.測試php和mariad連通性
3.1 httpd-->php是否可以訪問
www1]# cat admin.php
<?php
phpinfo();
?>
3.2 httpd-->php--mariadb是否可以訪問
www1]#cat index.php
<?php
$conn = mysql_connect(‘172.16.100.71‘,‘testuser‘,‘testpass‘);
if($conn)
cho "OK";
else
echo "Failure";
?>
4.測試wordpress和phpMyAdmin
4.1在PC瀏覽器中測試,wordpress是否能正常方式
http://www1/wordpress通過80連接埠訪問
4.2訪問提示:沒有擴充,安裝 php-mbstring 可以解決
~]# yum install php-mbstring
5.3在PC瀏覽器中測試,根據提示輸入資料庫名和密碼(主機帳號和密碼是授權wordpress中使用者)
http://www2/phpMyAdmin/index.php
5.為php-fpm安裝xcache加速器並配置
5.1yum 安裝php-xcache
~]# yum install php-xcache
第五部分:為phpMyAdmim提供https服務
在httpd主機環境中 IP: 172.16.66.60
工作目錄:/etc/pki/CA/
1.建立私人CA
1.1產生私密金鑰
CA]# (umask 077; openssl genrsa -out private/cakey.pem 2048)
1.2產生自簽認證
CA]# openssl req -new -x509 -key private/cakey.pem -out cacert.pem
Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:Beijing
Common Name (eg, your name or your server‘s hostname) []:www2
1.3提供輔助檔案
CA]# touch index.txt
CA]# echo 01 > serial 序號
CA]# tree
.
├── cacert.pem
├── certs
├── crl
├── index.txt
├── newcerts
├── private
│ └── cakey.pem
└── serial
2.節點申請認證
2.1產生私密金鑰
~]# mkdir -pv /etc/httpd/ssl
ssl]# (umask 077; openssl genrsa -out httpd.key 1024)
2.2產生認證簽署請求:
ssl]# openssl req -new -key httpd.key -out httpd.csr
Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:Beijing
Common Name (eg, your name or your server‘s hostname) []:www2
2.3把請求發給CA
ssl]# cp httpd.csr /tmp/
3.CA簽發認證
3.1簽署認證
~]# openssl ca -in /tmp/httpd.csr -out /etc/pki/CA/certs/httpd.crt
3.2把簽署好的認證發還給要求者。
~]# cp /etc/pki/CA/certs/httpd.crt /etc/httpd/ssl/
注意:本次私建CA和節點申請認證在同一台機器完成。
4.配置httpd支援使用ssl,及使用的認證
4.1yum安裝mod_ssl模組
~]# httpd -M | grep ssl
~]# yum install mod_ssl -y
~]# rpm -ql mod_ssl
4.2修改設定檔
~]# cat /etc/httpd/conf.d/ssl.conf
<VirtualHost>
DocumentRoot "/data/vhosts/www2"
ServerName www2:443
ProxyRequests Off
DirectoryIndex index.php
ProxyPassMatch ^/(.*\.php)$ fcgi://172.16.66.70:9000/data/vhosts/www2/$1
SSLCertificateFile /etc/httpd/ssl/httpd.crt
SSLCertificateKeyFile /etc/httpd/ssl/httpd.key
<Directory "/data/vhosts/www2">
SSLOptions +StdEnvVars
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
第六部分:壓力測試報告
本文出自 “8752057” 部落格,請務必保留此出處http://yang90.blog.51cto.com/8752057/1827371
CentOS 7.2 amp + xcache, rpm包,php-fpm