跳到主要內容

Linux-CentOS7-如何建置LAMP?(CentOS7+Apache2.4+MariaDB10.3+PHP7.3)

LAMP
由Linux搭建,Apache提供WEB服務,MariaDB 或 MySQL提供資料庫存取,PHP 或 Python作為網頁的程式語言。

首先安裝第三方套件庫:
1.查看CentOS官方獲社群認可的軟件庫:可供CentOS 使用的軟件庫 - CentOS Wiki
2.安裝 EPELRemi RepositoryMariaDB Repositories
  • EPEL:
  • # sudo rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
  • Remi Repository:
  • # sudo rpm -Uvh http://mirror.innosol.asia/remi/enterprise/remi-release-7.rpm
  • MariaDB Repositories:這邊使用 MariaDB 10.3
  • # sudo vim  /etc/yum.repos.d/MariaDB.repo
# MariaDB 10.3 CentOS repository list - created 2019-09-03 01:44 UTC
# http://downloads.mariadb.org/mariadb/repositories/
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.3/centos7-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1
3.清除套件庫緩存及讓repo設定生效
  • # sudo yum clean all 


安裝 Apache HTTP Server
  • # sudo yum install httpd


使用 chown 更改目錄(/var/www/html)擁有者、群組為 apache

  • sudo chown apache:apache -R /var/www/html


使用 chcon 修改 SELinux security context
  • sudo chcon -R -t httpd_sys_rw_content_t /var/www/html

開通防火牆 HTTP 端口 | 查看防火牆規則清單
  • # sudo firewall-cmd --add-service=http --permanent
  • # sudo firewall-cmd --reload
  • # sudo firewall-cmd --list-all

 查看 Apache 啟動狀態 | 重啟 Apache | 開機後啟動 httpd
  • # sudo systemctl status httpd
  • # sudo systemctl restart httpd
  • # sudo systemctl enable httpd

查看 Apache 測試頁
  • 使用 先前架設 DNS Server 做解析到 Apache Server ,URL:www.brookdns.local
Apache 完成安裝!!


Mariadb
安裝 MariaDB 10.3

  • sudo yum install mariadb-server

啟動 MariaDB | 查看 MariaDB 狀態 | 開機後啟動MariaDB
  • sudo systemctl restart mariadb
  • sudo systemctl status mariadb
  • sudo systemctl enable mariadb

執行 mysql_secure_installation 設定相關安全性
  • 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):
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:
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
 ... 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!
"
登入 MariaDB 測試
  • mysql -u root -p

MariaDB 測試完成!!

PHP
安裝PHP前有必要先確認一下版本,例如:worepress.org上就有要求PHP的版本需要7.3,後面需要安裝 wordpress 所以這邊安裝 php7.3版,但是 CentOS7預設提供的PHP只有提供5.4版,所以開始時有安裝套件庫(Remi Repository),先安裝 yum-utils 使用yum-config-manager指令啟用 Remi Repository PHP7.3。

  • sudo yum install yum-utils
  • sudo yum-config-manager --enable remi-php73
安裝PHP7.3及常用擴建
  • sudo yum install php php-xml php-gd php-mbstring php-intl php-mysqlnd php-pecl-zip php-pecl-mysql
安裝完後在 Apache 存放網頁的路徑下創建一個 PHP 信息測試頁,寫入<?php phpinfo(); ?>
  • # sudo vim /var/www/html/info.php

重新啟動 Apache 服務
  • sudo systemctl restart httpd
查看 PHP 信息頁面
  • http://your doman name/info.php 
PHP完成安裝!!

留言

這個網誌中的熱門文章

Linux-CentOS7 使用 Bind 架設 DNS Server & DNS正解

DNS (Domain Name System)服務 提供域名解析,相較於IP位址(IP Address),域名(Domain name)讓使用者容易記憶,使用域名對應IP位址。 1.安裝 BIND 套件 # sudo yum install bind

Linux-CentOS7 Bind 設置 DNS反解(rDNS) & 增加次要(Slave)伺服器

DNS 反解 : 將IP轉換成域名,通常需要 Class C 或是跟上層網路業者申請 ➽此使用內部網路做DNS反解測試。 1.在主設定檔(/etc/named.conf)添加反解區域(240.168.192.in-addr.arpr) # sudo vim /etc/named.conf zone "240.168.192.in-addr.arpa" IN { type master; file "240.168.192.txt"; }; 2.編輯 反解區域檔案(240.168.192.txt),完成後儲存檔案 # sudo vim/var/named/240.168.192.txt 3.重新啟動 Bind 服務&查看 Bind 服務狀態 # sudo systemctl restart named # sudo systemctl status named 4.使用 nslookup 工具測試 server xxx.xxx.xxx.xxx  --> 指定 DNS 服務器 反解測試完成!! DNS次要(Slave)服務器: 主要減少主伺服器的負擔,還有DNS服務器備機的用途。 1.在主要(Master)DNS服務器,正解及反解區域添加: allow-transfer { }; notify yes; 2.重啟主要(Master)DNS服務器的 Bind 服務 # sudo system restart named 3.次要(Slave)服務器安裝 Bind # sudo yum install -y bind 4.編輯 Slave 的 Bind 主設定檔 # sudo vim /etc/named.conf listen-on port 53 {}; //加入次要服務器 IP allow-query {}; //開放允許查詢的網段 // 加入正解區域 zone "brookdns.local" IN { type slave; masters {192.168.240.14

Linux & Windows 使用 SSH 公開金鑰(Public Key Authentication) 登入遠端主機(Linux Server)

目的: 讓SSH連線更加安全及便利 Step1 Linux Server 配置 1.在需要使用金鑰驗證的使用者(brookadmin)家目錄建立一個 .ssh 資料夾 ,權限 700 # mkdir .ssh 2.在 .ssh 資料夾內 建立一個 authorized_keys 的檔案,權限600,這裡會存放 client 端的Pubkey #touch authorized_keys Step2 Linux Client 使用 ssh-keygen 產生金鑰   1.進到使用者家目錄下 .ssh 目錄,使用指令 ssh-keygen 產生金鑰 2.將生成的公鑰存至要連線的服務器,使用指令 ssh-copy-id #sudo ssh-copy-id -i ~/.ssh/id_rsa.pub -p 22168 brookadmin@192.168.240.148 Windows Client 使用 ssh-keygen 產生金鑰 1.使用的windows作業系統是win 10,打開 cmd 命令提示字元,使用指令 ssh-keygen 產生金鑰,金鑰位置:C:\Users\ your username \.ssh,使用者的 .ssh 資料夾下 2.將 id_rsa.pub文件內容金鑰,添加到遠程linux server管理帳號下家目錄的 .ssh/authorized_keys 檔案裡 Step3 Linux Server 配置 1.將 Linux Client 跟 Windows Client 的 id_rsa.pub(公開金鑰)內容依序加入檔案,如果沒有金鑰可以先使用 ssh-keygen 產生,公開金鑰檔名可以自訂,需記住使用哪一組,加入金鑰後的authorized_keys 檔案內容就像圖片所示 2.編輯 SSH 設定檔 > sudo vim /etc/ssh/sshd_config #找到 PubkeyAuthentication 將註解#拿掉 並修改參數 #找到 PasswordAuthentication 修改參數為 no 3.重新啟動 ssh 服務,讓設定生效 #sudo systemctl restart sshd