CentOS 安裝 Apache+MariaDB+PHP+phpMyAdmin 及設定

Linux作業系統安裝、設定與管理 (CentOS、Ubuntu)
回覆文章
inho
文章: 54
註冊時間: 2017-12-08, 23:00

CentOS 安裝 Apache+MariaDB+PHP+phpMyAdmin 及設定

文章 inho » 2018-03-05, 18:08

一、如何在CentOS 7 安裝 MariaDB 10.3
參考網站:
https://tecadmin.net/install-mariadb-10-centos-redhat/#
Step 1: Add MariaDB Yum Repositories
在/etc/yum.repos.d/ 目錄底下 新建mariadb.repo 檔案:
$ sudo vi /etc/yum.repos.d/mariadb.repo
檔案內容:
===============================
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.3/rhel7-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1
==============================
存檔後,執行MariaDB的安裝:
Step 2 – Install MariaDB Server
$ sudo yum install MariaDB-server MariaDB-client

完成安裝後,即可啟動 Maria DB 的服務:
$ sudo systemctl start mysql.service

Step 3 – Secure MariaDB Install,建立 MariaDB的安全管理服務
$ sudo /usr/bin/mysql_secure_installation
過程中,要要求輸入 資料庫管理者的密碼 (預設為 無,按 enter就好,然後輸入 DB root user 的密碼)
其他的詢問選項都輸入 y(yes)即可

Step 4 – Working with MariaDB
$ sudo mysql -u root -p
Enter password: (輸入剛剛建立的密碼)
MariaDB [(none)]> exit 結束 MariaDB

設定系統開機時自動執行 MariaDB的服務:
$ sudo systemctl start mariadb.service
$ sudo systemctl enable mariadb.service

二、Installing Apache
先Check Apache Version
$ /usr/sbin/httpd -v
Server version: Apache/2.4.6 (CentOS)
Server built: Aug 4 2017 03:19:10

$ rpm -q httpd
[root@inhoNB yum.repos.d]# /usr/sbin/httpd -v
Server version: Apache/2.4.6 (CentOS)
Server built: Aug 4 2017 03:19:10
httpd-2.4.6-67.el7.centos.x86_64
直接安裝:
$ sudo yum -y install httpd
設定系統啟動時,自動執行 Apache
$ sudo systemctl start httpd.service
$ sudo systemctl enable httpd.service

完成之後,透過其他電腦Browser輸入 Server 網址,即可看到 Apache Testing 123...的測試網頁。

三、Installing PHP
The PHP version that ships with CentOS as default is quite old (PHP 5.4),改安裝 php 7.2
$ sudo rpm -Uvh http://rpms.remirepo.net/enterprise/remi-release-7.rpm
$ sudo yum -y install yum-utils
$ sudo yum update
========= Installing PHP 7.2
$ sudo yum-config-manager --enable remi-php72
$ sudo yum -y install php php-opcache

安裝PHP+phpMyAdmin管理資料庫,安裝php與連接資料庫套件
$ sudo yum install php php-mysql -y

安裝完成,重新啟動 Apache 服務
$ sudo systemctl restart httpd.service

清除快取
$ sudo yum clean all -y
更新套件庫
$ sudo yum update -y

安裝phpMyAdmin
$ sudo yum install phpmyadmin -y
設定phpMyAdmin:
phpMyAdmin預設除了本地(localhost),不允許其他電腦訪問phpMyAdmin,但因為我們現在要設定所以需要修改一下預設值,使用WinSCP連線到主機上面打開,編輯 /etc/httpd/conf.d/phpMyAdmin.conf
$ sudo vi /etc/httpd/conf.d/phpMyAdmin.conf
找到,並修改:
<RequireAny>
Require ip 127.0.0.1
Require ip ::1
</RequireAny>
================ 在Require ip ::1下方新增自己的IP
<RequireAny>
Require ip 127.0.0.1
Require ip ::1
Require ip 你的Server 的 IP
</RequireAny>
==============================================
建立 phpMyAdmin 的根目錄路徑:
$ sudo ln -s /usr/share/phpMyAdmin /var/www/html


======== 如果遇到 No Permission to access 問題 , 修改如下 ==========================
#### You don't have permission to access /phpMyAdmin on this server. #####
<Directory /usr/share/phpMyAdmin/>
AddDefaultCharset UTF-8

<IfModule mod_authz_core.c>
# Apache 2.4
<RequireAny>
# Require ip 127.0.0.1
# Require ip ::1
# Require ip 192.168.7.146
Require all granted
</RequireAny>
</IfModule>
<IfModule !mod_authz_core.c>
# Apache 2.2
Order Deny,Allow
Deny from All
# Allow from 127.0.0.1
# Allow from 192.168.7.146
# Allow from ::1
Allow from All
</IfModule>
</Directory>


=================================================

設定Web Page 目錄讀取屬性
$ sudo chown -R apache:apache html

設定 httpd.conf
$ sudo vi /etc/httpd/conf/httpd.conf
## 修改
#Listen 12.34.56.78:80
Listen Server IP:80
### <IfModule dir_module>
DirectoryIndex index.html
</IfModule>
# 改為
<IfModule dir_module>
DirectoryIndex index.php index.html
</IfModule>
## 存檔
## 再來要修改 /etc/php.ini 參數
# sudo vi /etc/php.ini
## 找到 post_max_size = 8M 這一行
## 修改為 (最大檔案傳輸Size)
post_max_size = 64M

重新啟動網頁伺服器
$ sudo service httpd restart

設定防火牆:
To be able to access the web server from outside, we have to open the HTTP (80) and HTTPS (443) ports in the firewall. The default firewall on CentOS is firewalld which can be configured with the firewalld-cmd command.
$ sudo firewall-cmd --permanent --zone=public --add-service=http
$ sudo firewall-cmd --permanent --zone=public --add-service=https
$ sudo firewall-cmd --reload
查詢防火牆規則
$ sudo firewall-cmd --list-all --permanent
重新啟動防火牆
$ sudo service firewalld restart

安裝完成,測試 PHP服務
Testing PHP / Getting Details About Your PHP Installation

$ sudo nano /var/www/html/info.php
輸入以下內容:
============================
<?php
phpinfo() ;
?>
======================
Browser輸入 Server 網址: http://Web Address/info.php

參考網站:
https://www.howtoforge.com/tutorial/cen ... mysql-php/

回覆文章