CentOS 7 安裝lamp,並實現https

題目:新建三個基於域名的虛擬主機,如下:

      vhost1: pma.xujunmin.com, phpMyAdmin, 同時提供https服務;

      vhost2: wp.xujunmin.com, wordpress

      vhost3: dz.xujunmin.com, Discuz

 

一、編譯安裝Apache

1、編譯安裝aprapr-util

aprApache的可移植運行庫,主要爲上層的應用程序提供一個可以跨越多操作系統平臺使用的底層支持接口庫。

[root@localhost PKGS]# tar -xf apr-1.5.2.tar.bz2
[root@localhost PKGS]# cd apr-1.5
[root@localhost apr-1.5.2]# ./configure --prefix=/usr/local/apr
[root@localhost apr-1.5.2]# make && make install

[root@localhost PKGS]# tar -xf apr-util-1.5.4.tar.bz2
[root@localhost PKGS]# cd apr-util-1.5.4
[root@localhost apr-util-1.5.4]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
[root@localhost ~]# vim /etc/ld.so.conf.d/apr_apr-util.conf
[root@localhost apr-util-1.5.4]# make && make install


# 導出庫文件

[root@localhost ~]# vim /etc/ld.so.conf.d/apr_apr-util.conf
添加:/usr/local/apr/lib
      /usr/local/apr-util/lib

# 使庫文件生效並驗證

[root@localhost ~]# ldconfig
[root@localhost ~]# ldconfig -p | grep apr

 

2、安裝依賴包

pcre-develhttp進行正則匹配的時候需要,而openssl-develhttp開啓ssl功能的時候需要。

[root@localhost PKGS]# yum install pcre-devel openssl-devel.x86_64

 

3、編譯httpd

[root@localhost PKGS]# tar xf httpd-2.4.16.tar.gz
[root@localhost PKGS]# cd httpd-2.4.16
[root@localhost httpd-2.4.16]# ./configure --prefix=/usr/local/apache \
> --sysconfdir=/etc/httpd  --enable-so --enable-ssl --enable-cgi \
> --with-pcre --with-zlib --enable-rewrite --with-apr=/usr/local/apr \
> --with-apr-util=/usr/local/apr-util --enable-modules=most \
> --enable-mpms-shared=all --with-mpm=event

[root@localhost ~]# make && make install


4、其他操作

#編輯httpd,指定PidFile

[root@localhost ~]# vim /etc/httpd/httpd.conf
添加:PidFile "/var/run/httpd.pid"

# 導出庫文件

[root@localhost ~]# vim /etc/ld.so.conf.d/httpd.conf
/usr/local/apache/lib

# 爲可執行程序添加PATH路徑

[root@localhost ~]# echo 'export PATH=$PATH:/usr/local/apache/bin' > /etc/profile.d/httpd.sh
[root@localhost ~]# . /etc/profile.d/httpd.sh

# 導出man文件

[root@localhost ~]# vim /etc/man_db.conf
添加:MANDATORY_MANPATH                       /usr/local/apache/man

# 添加服務

[root@localhost ~]# httpd -k start


-----------------------------------------------------------------------------------------

二、編譯mysql

     此處使用MariaDB的二進制程序安裝,無需編譯

1 解壓到指定目錄

[root@localhost PKGS]# tar -xf mariadb-5.5.36-linux-x86_64.tar.gz -C /usr/local/
[root@localhost PKGS]# cd /usr/local/

2、建立軟鏈接,方便管理及以後升級

[root@localhost local]#ln -sv mariadb-5.5.36-linux-x86_64 mysql
[root@localhost ~]# mkdir /data   # 建立mysql數據存放目錄
[root@localhost ~]# chown -R mysql:mysql /data

3、創建mysql系統用戶

[root@localhost mysql]# groupadd -r mysql
[root@localhost mysql]# useradd -g mysql -r -s /sbin/nologin -d /data mysql
[root@localhost mysql]# chown -R mysql:mysql *


4、進行數據庫安裝

[root@localhost mysql]# scripts/mysql_install_db --datadir=/data --user=mysql


5、編輯mysql配置文件

[root@localhost mysql]# cp support-files/my-large.cnf /etc/my.cnf   # 覆蓋/etc/my.cnf下的                                                                       配置文件
[root@localhost mysql]# vim /etc/my.cnf
datadir = /data      #  在[mysqld]添加datadir

 

6、添加mysql的服務腳本

[root@localhost mysql]# cp support-files/mysql.server /etc/rc.d/init.d/mysqld
[root@localhost mysql]# chkconfig --add mysqld
[root@localhost mysql]# chkconfig mysqld on
[root@localhost mysql]# service mysql start
[root@localhost mysql]# ps -ef | grep mysqld       # 查看進程啓動是否正常

7、查看端口監聽是否正常

[root@localhost mysql]# ss -ant | grep 3306      
LISTEN     0     50                       *:3306                     *:*


8、其他操作

# 添加二進制程序的PATH路徑

[root@localhost mysql]# echo 'export PATH=$PATH:/usr/local/mysql/bin' > /etc/profile.d/mysqld.sh
 [root@localhost mysql]# . /etc/profile.d/mysqld.sh

# 導出頭文件

[root@localhost mysql]# ln -sv include /usr/include/mysql

# 導出庫文件

[root@localhost mysql]# echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf

# 修改root密碼

MariaDB [(none)]>UPDATE mysql.user SET Password = password('123456') where User = 'root';
MariaDB [(none)]>create database wordpress;      # 爲安裝wordpress做準備
Query OK, 1 row affected
(0.00 sec)
 
MariaDB [(none)]> show
databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
| wordpress          |
+--------------------+
5 rows in set (0.13 sec)
 
MariaDB [(none)]>
flush privileges;
Query OK, 0 rows affected
(0.00 sec)


-----------------------------------------------------------------------------------------


三、編譯安裝PHP

1、解壓

[root@localhost PKGS]# tar xf php-5.4.40.tar.bz2        
[root@localhost PKGS]# cd php-5.4.40

2、編譯安裝

[root@localhost ~]# ./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 \
> --with-apxs2=/usr/local/apache/bin/apxs --with-mcrypt --with-config-file-path=/etc \
> --with-config-file-scan-dir=/etc/php.d --with-bz2  --enable-maintainer-zts
[root@localhost php-5.4.40]# make && make install

3、爲php提供配置文件

[root@localhost php-5.4.40]# cp php.ini-production /etc/php.ini

4、編輯apache配置文件httpd.conf,使apache支持php

[root@localhost ~]# vim /etc/httpd/httpd.conf
 # AddType 添加對.php及.phps後綴文件的支持
   AddType application/x-httpd-php  .php
   AddType application/x-httpd-php-source  .phps

 # 添加php的索引文件

  DirectoryIndex  index.php index.html

 

四、建立虛擬主機

      三個基於域名的虛擬主機:

   vhost1: pma.xujunmin.com, phpMyAdmin, 同時提供https服務;

   vhost2: wp.xujunmin.com, wordpress

   vhost3: dz.xujunmin.com, Discuz

1 分別創建三個虛擬主機的家目錄,並將phpMyAdmin,wordpress,Discuz分別移至對應的目 錄下

[root@localhost ~]# mkdir -pv /www/{vhost1,vhost2,vhost3}
[root@localhost PKGS]# unzip phpMyAdmin-4.4.14.1-all-languages.zip
[root@localhost PKGS]# mv phpMyAdmin-4.4.14.1-all-languages/*/www/vhost1/
[root@localhost PKGS]# unzip wordpress-4.3.1-zh_CN.zip
[root@localhost PKGS]# mv wordpress/* /www/vhost2/
[root@localhost PKGS]# unzip Discuz_X3.2_SC_UTF8.zip
[root@localhost PKGS]# mv upload/* /www/vhost3/
[root@localhost PKGS# cd /www/vhost3/
[root@localhost vhost3]# chown -R daemon:root *  # 更改屬主信息否則安裝過程中提示無權限

2、配置httpd.conf文件:

[root@localhost ~]# vim /etc/httpd/httpd.conf
#DocumentRoot "/usr/local/apache/htdocs"        # 註釋掉DocumentRoot
#Virtual hosts 
Include /etc/httpd/extra/httpd-vhosts.conf   #去掉註釋,使httpd-vhosts配置生效
# Secure (SSL/TLS) connections
Include /etc/httpd/extra/httpd-ssl.conf   # 去掉前面的註釋,開啓https
LoadModule ssl_module modules/mod_ssl.so  # 去掉前面的註釋,開始ssl功能
LoadModule socache_shmcb_module modules/mod_socache_shmcb.so   # 去掉前面的註釋

3 配置 httpd-vhosts.conf:

[root@localhost ~]# vim
/etc/httpd/extra/httpd-vhosts.conf
# 配置基於域名wp.xujunmin.com的虛擬主機
<VirtualHost *:80>
    ServerAdmin [email protected]
    DocumentRoot /www/vhost2
    ServerName wp.xujunmin.com
        <Directory /www/vhost2>
            Options None
            AllowOverride None
            Require all granted
        </Directory>
    ErrorLog "logs/wp.com-error_log"
    CustomLog
"logs/wp.com-access_log" combine
</VirtualHost>
 
# 配置基於域名dz.xujunmin.com的虛擬主機
<VirtualHost *:80>
    ServerAdmin [email protected]
    DocumentRoot /www/vhost3
    ServerName dz.xujunmin.com
        <Directory /www/vhost3>
            Options None
            AllowOverride None
            Require all granted
        </Directory>
    ErrorLog "logs/dz.com-error_log"
    CustomLog
"logs/dz.com-access_log" combine
</VirtualHost>


# 重啓httpd服務

[root@localhost ~]# httpd -k start
[root@localhost ~]# ss -ant | grep 443
LISTEN     0     128                     :::443                     :::*


4 配置httpd-ssl.conf:

[root@localhost ~]# vim /etc/httpd/extra/httpd-ssl.conf
<VirtualHost 192.168.52.132:443>
    DocumentRoot "/www/vhost1/"
    ServerName pma.xujunmin.com:443
    ServerAdmin [email protected]
    <Directory /www/vhost1/>
        Options None
        AllowOverride None
        Require all granted
    </Directory>
    ErrorLog "/usr/local/apache/logs/pma.com-rror_log"
    TransferLog "/usr/local/apache/logs/pma.com-access_log"
    ...
 </VirtualHost>


5HTTPS認證:

#自建CA:

root@localhost ~]# cd /etc/pki/CA/
[root@localhost CA]# (umask 077;openssl genrsa -out private/cakey.pem 2048)   
# 生成密鑰對
[root@localhost CA]# openssl req -new -x509 -key private/cakey.pem -out cacert.pem -days 3650  # 生成自簽證書
[root@localhost CA]# touch index.txt serial crlnumber
[root@localhost CA]# echo 01 > serial

# 客戶端:

[root@localhost ~]# mkdir /etc/httpd/ssl
[root@localhost ~]# cd /etc/httpd/ssl
[root@localhost ssl]# (umask 077;openssl genrsa -out httpd.key 1024)   # 生成密鑰對
[root@localhost ssl]# openssl req -new -key httpd.key -out httpd.csr # 生成證書申請請求
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]:Magedu
Organizational Unit Name (eg, section) []:OPS               
Common Name (eg, your name or your server's hostname) []:pma.xujunmin.com   
Email Address []:admin.xujunmin.com
 
Please enter the following 'extra' attributes to be sent with your certificate request
A challenge password []:
An optional company name []:
[root@localhost ssl]# ls
httpd.csr  httpd.key

# CA簽署客戶申請證書:

[root@localhost CA]# openssl ca -in /etc/httpd/ssl/httpd.csr -out /etc/httpd/ssl/httpd.crt -days 365


 

# 將證書導入到IE證書的受信任的根證書頒發機構欄

 

wKiom1YH7fLzWxIjAAN1nNeHQms459.jpg

 

# window hosts(C:\Windows\System32\drivers\etc)中添加域名解析項

192.168.52.132 pma.xujunmin.com

192.168.52.132 wp.xujunmin.com

192.168.52.132 dz.xujunmin.com

 

 

五、測試

1 vhost1: pma.xujunmin.com

 

wKiom1YH7h6xnhHTAALZ5Rn9zjU017.jpg

 

2、wp.xujunmin.com(安裝具體過程省略)

 

wKioL1YH7nqQdQbfAALoL9wIiqI574.jpg

 

 

 

wKioL1YH7tPhYmIbAAGsXspLPZg320.jpg

wKiom1YH7zvwdZIQAAO2ivehdE8223.jpg

 


 


 

 3、dz.xujunmin.com(安裝過程省略)

 

wKiom1YH772DT09jAAS7BHITDss566.jpg

 

 

 

wKiom1YH7-2gNT7sAAIZuDnfj5k386.jpg

 


 

 

 

 


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