共計 9248 個字符,預計需要花費 24 分鐘才能閱讀完成。
丸趣 TV 小編給大家分享一下一臺 centos7 主機如何部署 LAMP 并提供 https 服務,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
在一臺 centos7 上部署 LAMP 以及 xcache 并安裝分別在 2 個虛擬主機上部署 wordpress 和 php-myadmin 且為 phpmyadmin 提供 https 服務;
#用 rpm 包快速部署:LAMP
1、yum 安裝:
yum install -y httpd php php-mysql php-gd php-mbstring php-xml mariadb-server mod_ssl
Installed:
httpd.x86_64 0:2.4.6-40.el7.centos mariadb-server.x86_64 1:5.5.44-2.el7.centos php.x86_64 0:5.4.16-36.el7_1 php-gd.x86_64 0:5.4.16-36.el7_1 php-mbstring.x86_64 0:5.4.16-36.el7_1
php-mysql.x86_64 0:5.4.16-36.el7_1 php-xml.x86_64 0:5.4.16-36.el7_1
2、 檢查 httpd 服務是否可以啟動成功,以及 php 頁面是否能夠成功開啟:
檢查 httpd 服務是否能夠成功開啟以及 80 端口是否開啟后啟動:
systemctl start htppd.service 啟動服務
systemctl status httpd.service 檢查服務開啟狀態
ss -tnl 查看 80 端口
ps -aux 檢查進程
httpd -M | grep mpm :查看是否是 prefork 模塊以及是否是開啟狀態(shared)
#確認 httpd 服務啟動成功
[root@1 ~]# systemctl status httpd
● httpd.service – The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
Active: active (running) since Sat 2016-07-16 15:32:43 CST; 4min 18s ago
Docs: man:httpd(8)
man:apachectl(8)
Main PID: 6535 (httpd)
Status: Total requests: 0; Current requests/sec: 0; Current traffic: 0 B/sec
CGroup: /system.slice/httpd.service
├─6535 /usr/sbin/httpd -DFOREGROUND
├─6537 /usr/sbin/httpd -DFOREGROUND
├─6538 /usr/sbin/httpd -DFOREGROUND
├─6539 /usr/sbin/httpd -DFOREGROUND
├─6540 /usr/sbin/httpd -DFOREGROUND
└─6541 /usr/sbin/httpd -DFOREGROUND
#確認 80 端口開啟狀態
[root@1~]# ss -tnl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 5 192.168.122.1:53 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 128 127.0.0.1:631 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 :::80 :::*
LISTEN 0 128 :::22 :::*
LISTEN 0 128 ::1:631 :::*
LISTEN 0 100 ::1:25
#確認進程啟動
[root@1 ~]# ps -aux
root 6535 0.0 0.8 450548 15064 ? Ss 15:32 0:00 /usr/sbin/httpd -DFOREGROUND
apache 6537 0.0 0.4 452632 7888 ? S 15:32 0:00 /usr/sbin/httpd -DFOREGROUND
apache 6538 0.0 0.4 452632 7888 ? S 15:32 0:00 /usr/sbin/httpd -DFOREGROUND
apache 6539 0.0 0.4 452632 7888 ? S 15:32 0:00 /usr/sbin/httpd -DFOREGROUND
apache 6540 0.0 0.4 452632 7888 ? S 15:32 0:00 /usr/sbin/httpd -DFOREGROUND
apache 6541 0.0 0.4 452632 7888 ? S 15:32 0:00 /usr/sbin/httpd -DFOREGROUND
#確認默認啟動模塊 prefok 是 shared 狀態
[root@1 ~]# httpd -M | grep mpm
AH00558: httpd: Could not reliably determine the server s fully qualified domain name, using 0.0.0.1. Set the ServerName directive globally to suppress this message
mpm_prefork_module (shared)
#確認 ssl 模塊是否啟用成功
[root@1 ~]# httpd -M |grep ssl
AH00558: httpd: Could not reliably determine the server s fully qualified domain name, using 0.0.0.1. Set the ServerName directive globally to suppress this message
ssl_module (shared)
#配置 mysql 數據庫,并啟動起來;
systemctl start mariadb.service
#然后授權創建用戶
先進 mysql 然后如下
mysql GRANT ALL ON wpdb.* TO wpuser @ 172.16.%.% IDENTIFIED BY wppass
mysql FLUSH PRIVILEGES;
mysql CREATE DATABASE wpdb;
#建 2 個目錄作為虛擬主機 FQDN 的資源映射路徑,
[root@1 ~]# mkdir -pv /data/vhost/www{1,2}
#配置默認測試頁面
[root@1 ~]# vim /data/vhost/www1/index.php
h2 第一臺虛擬主機 /h2
?php
$conn = mysql_connect(172.16.100.34 , wpuser , wppass
if($conn)
echo OK
else
echo Failure
phpinfo();
?
[root@1 ~]# vim /data/vhost/www2/index.php
h2 第二臺虛擬主機 /h2
?php
$conn = mysql_connect(172.16.100.34 , wpuser , wppass
if($conn)
echo OK
else
echo Failure
phpinfo();
?
#配置 2 個 FQDN 虛擬主機的配置文件
[root@1 ~]# vim /etc/httpd/conf.d/vhost1.conf
VirtualHost 172.16.100.34:80
ServerName www1.wufeng.com
DocumentRoot /data/vhost/www1
ProxyRequests on
DirectoryIndex index.php
Directory /data/vhost/www1
Options None
AllowOverride None
Require all granted
/Directory
/VirtualHost
[root@1 ~]# vim /etc/httpd/conf.d/vhost2.conf
VirtualHost 172.16.100.34:80
ServerName www2.wufeng.com
DocumentRoot /data/vhost/www2
ProxyRequests on
DirectoryIndex index.php
Directory /data/vhost/www2
Options None
AllowOverride None
Require all granted
/Directory
/VirtualHost
#臨時修改 DNS 指向自己
[root@1 ~]# vim /etc/resolv.conf
# Generated by NetworkManager
DNS=172.16.100.34
#本地域名解析增加條目
[root@1 ~]# vim /etc/hosts
172.16.100.31 www1.wufeng.com www2.wufeng.com
#測試 php 頁面是否可以正常連接 以及數據庫連接是否正常
#用 www2.wufeng.com 訪問也沒問題 而且加速器也成功安裝了
#下面我們就剩下搞定 wordpress 以及 php-myadmin
#因為我是本地 ftp 服務器所以直接下載下來解壓縮了;
[root@1~]# unzip wordpress-4.3.1-zh_CN.zip
#并移動至第一個虛擬主機的資源映射路徑下
[root@1~]# mv wordpress /data/vhost/www1
#cd 進入該目錄下并且修改配置文件
#修改配置文件名并修改之;
[root@localhost ~]# cd /data/vhost/www1/wordpress/
[root@localhost wordpress]# ln -s wp-config-sample.php wp-config.php
[root@localhost wordpress]# vim wp-config.php
// ** MySQL 設置 – 具體信息來自您正在使用的主機 ** //
/** WordPress 數據庫的名稱 */
define(DB_NAME , wpdb
/** MySQL 數據庫用戶名 */
define(DB_USER , wpuser
/** MySQL 數據庫密碼 */
define(DB_PASSWORD , wppass
/** MySQL 主機 */
define(DB_HOST , 172.16.100.34 這里的地址是指向數據庫地址
/** 創建數據表時默認的文字編碼 */
define(DB_CHARSET , utf8
/** 數據庫整理類型。如不確定請勿更改 */
define(DB_COLLATE ,
那么 wordpress 基本上已經配置好了 不急于測試 下面把 php-myadmin 也一起安裝了
#php-myadmin 也是在 ftp 服務器上下載的所以直接解壓了
[root@1 ~]# unzip phpMyAdmin-4.4.14.1-all-languages.zip
#并且放在第 2 臺主機上的第 2 個虛擬主機資源映射路徑下;
[root@1 myadmin]# mv phpMyAdmin-4.4.14.1-all-languages /data/vhost/www2/myadmin
#cd 進該目錄下并且修改文件名修改配置
[root@1 www2]# cd myadmin/ 進入目標目錄
[root@1 myadmin]# mv config.sample.inc.php config.inc.php 修改配置文件名
[root@1 myadmin]# vim config.inc.php 修改配置文件
$cfg[blowfish_secret] = 4pfPnJU4R8pA4WMWaQxD /* YOU MUST FILL IN THIS FOR COOKIE AUTH! */
上面這 2 個單引號中間加上隨機碼 用 openssl rand -base64 15 生成 用于傳輸加密
/*
* Servers configuration
*/
$i = 0;
/*
* First server
*/
$i++;
/* Authentication type */
$cfg[Servers][$i][auth_type] = cookie
/* Server parameters */
$cfg[Servers][$i][host] = 172.16.100.34 這里這指向 mysql 數據庫主機的地址
$cfg[Servers][$i][connect_type] = tcp
$cfg[Servers][$i][compress] = false;
$cfg[Servers][$i][AllowNoPassword] = false;
#下面就測試這 2 個程序是否可以連接
#需要安裝以及創建賬戶密碼然后登陸即可
#測試第 2 臺虛擬主機上的 myadmin
#到這里測試全部成功,然后我們最后要為 myadmin 提供 https 服務
#簽署 CA 證書并為 phpmyadmin 提供 https 服務;
#在主機上安裝 mod_ssl 模塊 因為需要 http 來加載模塊并啟動 443 端口我們前面已經安裝了
[root@localhost CA]# yum install mod_ssl.x86_64
#找一臺主機做 CA 比如第一臺主機
#然后在第 1 臺主機上生成 CA 自簽名證書;
1、生成密鑰
#首先進入 CA 目錄下
[root@1 wordpress]# cd /etc/pki/CA/
#在 CA 目錄下生成密鑰
[root@1 CA]# (umask 077; openssl genrsa -out private/cakey.pem 2048)
Generating RSA private key, 2048 bit long modulus
………………………………………………………………….+++
……………+++
e is 65537 (0x10001)
2、生成自簽證書
[root@1 CA]# openssl req -new -x509 -key private/cakey.pem -out cacert.pem
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter . , the field will be left blank.
—–
Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:beijing
Locality Name (eg, city) [Default City]:beijing
Organization Name (eg, company) [Default Company Ltd]:wufeng
Organizational Unit Name (eg, section) []:ops
Common Name (eg, your name or your server s hostname) []:ca.wufeng.com
Email Address []:admin@wufeng.com
補充文件
[root@1 CA]# touch index.txt
[root@1 CA]# echo 01 serial
、
#然后在去第 2 臺主機上生成請求簽署文件以及密鑰
在 /etc/httpd/ 目錄下創建一個目錄
~]# mkdir ssl
~]# cd ssl
生成密鑰
[root@ ssl]# (umask 077; openssl genrsa -out httpd.key 1024)
Generating RSA private key, 1024 bit long modulus
.++++++
………………………………….++++++
e is 65537 (0x10001)
生成簽署請求文件:
[root@ ssl]# openssl req -new -key httpd.key -out httpd.csr
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter . , the field will be left blank.
—–
Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:beijing
Locality Name (eg, city) [Default City]:beijing
Organization Name (eg, company) [Default Company Ltd]:wufeng
Organizational Unit Name (eg, section) []:ops
Common Name (eg, your name or your server s hostname) []:www2.wufeng.com 這個要和客戶訪問要加密的主機名一樣
Email Address []:www1admin@wufeng.com
Please enter the following extra attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
復制給 CA 主機簽署證書
[root@ ssl]# scp httpd.csr 172.16.100.32:/tmp
然后到 centos7 主機上簽署文件
[root@1 CA]# openssl ca -in /tmp/httpd.csr -out /etc/pki/CA/certs/httpd.crt
連續 2 個 yes 簡寫 y
簽署完成后 復制給請求主機
[root@1 CA]# scp certs/httpd.crt 172.16.100.31:/etc/httpd/ssl/
在請求主機上也就是第 2 臺 centos7 上
編輯這個文件
[root@1 ~]# vim /etc/httpd/conf.d/ssl.conf
DocumentRoot /data/vhost/www2/myadmin 這 2 個啟用起來 把前面的 #號去掉 改成自己的資源映射路徑
ServerName www2.wufeng.com:443 主機名也改成自己的
SSLCertificateFile /etc/httpd/ssl/httpd.crt 指明簽署的證書文件路徑
SSLCertificateKeyFile /etc/httpd/ssl/httpd.key 指明私鑰文件的路徑
然后 reload 一下
然后 ss -tnl 查看一下 443 端口起來沒
在 centos7 主 CA 機器上 測試基于 https 訪問相應的主機;
[root@localhost CA]# openssl s_client -connect 172.16.100.31:443 -CAfile /etc/pki/CA/cacert.pem
#然后打開瀏覽器輸入 myadmin 地址訪問看看是否提供了 https 服務
在主配置文件里把這個給禁了 免得沖突
#DocumentRoot /var/www/html
成功了 下面我們做個 ab 壓力測試
并且比較 2 個架構性能的差別
單臺主機部署 LAMP
三臺主機分別部署 LAMP
以上是“一臺 centos7 主機如何部署 LAMP 并提供 https 服務”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注丸趣 TV 行業資訊頻道!