1. 程式人生 > >CentOS7搭建apache/nginx、mysql、php、phpmyadmin環境

CentOS7搭建apache/nginx、mysql、php、phpmyadmin環境

最近在看鳥哥的私房菜Linux,所以就在買了一個國外的伺服器來折騰,首先就搭建了一下基本的web伺服器。過程中遇到了各種坑,總結了一下,寫出來,避免大家踩坑。

1.安裝apache或nginx伺服器

/***************apache***************/

//1.安裝apache伺服器,-y表示後面的提示全部都通過
yum install httpd -y

//2.啟動apache伺服器
systemctl start httpd
//其他常用命令
//重啟apache伺服器
systemctl restart httpd
//關閉apache伺服器
systemctl stop httpd

//3.使apache伺服器開機自啟
systemctl enable httpd.service //4.關閉SELINUX vi /etc/selinux/config //註釋掉如下兩句,新增最後一項 #SELINUX=enforcing //#註釋掉 #SELINUXTYPE=targeted //#註釋掉 SELINUX=disabled //#增加 :wq! 儲存退出 //輸入如下命令 setenforce 0 //#使配置立即生效 /***************nginx***************/ //1.安裝epel倉庫 yum install epel-release -y //2.安裝nginx伺服器 yum install nginx -y //3啟動nginx服務
systemctl start nginx //4.使nginx服務開機自啟 systemctl enable nginx /* 注意事項: 1.nginx預設的web根目錄是/usr/share/nginx/html 2.如果想修改nginx的web根目錄,使用vim編輯/etc/nginx/nginx.conf,修改root=你想要的目錄 vi /etc/nginx/nginx.conf 編輯nginx的配置檔案 /root 搜尋root關鍵字 root=/web/www 修改為你想要設為web根目錄的路徑 /*

2.安裝mysql資料庫

安裝mariadb資料,這個資料庫是mysql的一個分支,有專人維護,推薦使用。畢竟mysql已經被oracle收購了,你懂得。

//1.安裝mariadb(mysql)資料庫
yum install mariadb mariadb-server -y

//2.啟動mariadb服務
systemctl start mariadb
//其他命令
//重啟mariadb服務
systemctl restart mariadb
//停止mariadb服務
systemctl stop mariadb

//3.使mariadb服務開機自啟
systemctl enable mariadb

配置mariadb資料庫

//1.輸入這個命令開始配置
mysql_secure_installation

//2.會提示你輸入當前的root密碼,如果沒有設定過,就直接回車
Enter current password for root (enter for none):

//3.提示是否設定root密碼,輸入y,回車
Set root password? [Y/n]

//4.輸入root密碼
New password: 

//5.確認root密碼
Re-enter new password:

//6.移除匿名使用者,輸入y,回車
Remove anonymous users? [Y/n]

//7.禁用遠端root使用者登入,這個看個人需求,如果你想在遠端連線mysql,你就輸入n,回車;如果為了安全考慮,不允許的話,輸入y,回車
Disallow root login remotely? [Y/n]

//8.是否移除test資料庫,不想要的話,輸入y,回車
Remove test database and access to it? [Y/n]

//9.重新整理許可權、表,輸入y,回車
Reload privilege tables now? [Y/n]

至此,mariadb安裝、配置完成。

3.安裝php環境

//1.安裝php及其擴充套件,預設安裝的是php5.4
yum install php php-mysql php-gd libjpeg* php-ldap php-odbc php-pear php-xml php-xmlrpc php-mbstring php-bcmath php-mhash -y

//2.如果想升級php到5.6/7.0
//2.1首先需要升級軟體倉庫,依次執行下面2個命令
 rpm -Uvh https://mirror.webtatic.com/yum/el7/epel-release.rpm
 rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
//2.2刪除舊的php
yum remove php-common -y
//2.3安裝所需要的版本,5.6版本,
yum install php56w php56w-opcache php56w-xml php56w-mcrypt php56w-gd php56w-devel php56w-mysql php56w-intl php56w-mbstring -y
//2.3安裝所需要的版本,7.0版本
yum install php70w php70w-opcache php70w-xml php70w-mcrypt php70w-gd php70w-devel php70w-mysql php70w-intl php70w-mbstring -y

//3.檢查php是否安裝成功
php -v //可以看到自己安裝的php版本資訊

//4.重啟伺服器
systemctl restart httpd

4.安裝phpmyadmin資料庫管理系統

//1.下載phpmyadmin包
wget https://files.phpmyadmin.net/phpMyAdmin/4.7.0/phpMyAdmin-4.7.0-all-languages.zip

//2.解壓phpmyadmin包
unzip phpMyAdmin-4.7.0-all-languages.zip
//2.如果沒有安裝unzip和zip,請先安裝,然後再執行上面的操作
yum install zip unzip -y

//3.將解壓後的phpmyadmin移動到/var/www/html/phpmyadmin資料夾下
//3.1首先建立phpmyadmin資料夾
mkdir /var/www/html/phpmyadmin
//3.2移動檔案
mv phpMyAdmin-4.7.0-all-languages/* /var/www/html/phpmyadmin/

//4.重啟apache伺服器
systemctl restart httpd

然後就可以在瀏覽器輸入 ip地址或域名/phpmyadmin訪問了,至此,CentOS7安裝apache、mysql、php、phpmyadmin完成了。

注意:如果瀏覽器打不開這個頁面,提示“拒絕了您的請求”,說明是防火牆開啟了,沒有開放這個埠。

firewall-cmd --zone=public --list-ports //檢視所有開放的埠
//如果沒有出現80/tcp,則說明http的埠沒有開放,
firewall-cmd --zone=public --add-port=80/tcp --permanent //開放80埠
firewall-cmd --reload //重新載入防火牆規則

然後就可以正常訪問了。