本次環境:
虛擬機軟件:VMware workstation 15.5
虛擬機:CentOS7
一、更換國內yum源(浙大)
1.什麼是yum?
簡單來說yum倉庫就是通常使用 yum install 命令來在線安裝Linux系統軟件的一個倉庫,它可以自動處理依賴性關係,並且一次性安裝所有依賴的軟件包,但是經常會遇到下載速度過慢等問題,所以此次選擇更換yum源。(之前搭建過一個本地的yum源)。yum配置文件在 /etc/yum.repos.d 目錄下,一般爲 CentOS-Base.repo
2.備份原來的源
sudo mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bk #如果搭建過本地yum,將文件名爲local的那個.repo文件備份即可
3.下載浙大yum源(可自行挑選國內源)
$ cd /etc/yum.repos.d $ sudo wget -nc http://mirrors.yangxingzhen.com/help/CentOS7-Base-zju.repo
下面附上一些國內源
網易: CentOS5 :http://mirrors.163.com/.help/CentOS5-Base-163.repo CentOS6 :http://mirrors.163.com/.help/CentOS6-Base-163.repo CentOS7 :http://mirrors.163.com/.help/CentOS7-Base-163.repo 阿里: CentOS5 : http://mirrors.aliyun.com/repo/Centos-5.repo CentOS6 : http://mirrors.aliyun.com/repo/Centos-6.repo CentOS7 : http://mirrors.aliyun.com/repo/Centos-7.repo 中科大: CentOS5 : https://lug.ustc.edu.cn/wiki/_export/code/mirrors/help/centos?codeblock=1 CentOS6 : https://lug.ustc.edu.cn/wiki/_export/code/mirrors/help/centos?codeblock=2 CentOS7 : https://lug.ustc.edu.cn/wiki/_export/code/mirrors/help/centos?codeblock=3 清華大學: CentOS5 : https://mirrors.yangxingzhen.com/help/CentOS5-Base-tuna.repo CentOS6 : https://mirrors.yangxingzhen.com/help/CentOS6-Base-tuna.repo CentOS7 : https://mirrors.yangxingzhen.com/help/CentOS7-Base-tuna.repo 浙江大學: CentOS6 : https://mirrors.yangxingzhen.com/help/CentOS6-Base-zju.repo CentOS7 : https://mirrors.yangxingzhen.com/help/CentOS7-Base-zju.repo 中國科技大學: CentOS5 : https://mirrors.yangxingzhen.com/help/CentOS5-Base-ustc.repo CentOS6 : https://mirrors.yangxingzhen.com/help/CentOS6-Base-ustc.repo CentOS7 : https://mirrors.yangxingzhen.com/help/CentOS7-Base-ustc.repo
4.更改浙大yum源爲默認源
sudo mv CentOS7-Base-zju.repo CentOS-Base.repo
5.更新本地yum緩存
sudo yum clean all #清除本地所有緩存
sudo yum list #更新列表
sudo yum makecahe #(可選)緩存yum包信息到本機,提高搜索速度
二、搭建LNMP環境
1.什麼是LNMP?
LNMP就是Linux下 Nginx+MySQL+PHP網站服務架構。Nginx是一個高性能的HTTP和反向代理服務器,也是一個IMAP/POP3/SMTP代理服務器;Mysql是一個小型關係型數據庫管理系統;PHP是一種在服務器端執行的嵌入HTML文檔的腳本語言。
2.搭建Nginx
可以先看一下自己的yum源有沒有Nginx,現在國內的源基本都有了。
sudo yum search nginx
如果沒有Nginx的話,可以官網下載相應版本的Nginx再放到相應虛擬機的目錄下解壓;也可像下面這樣:
在 /etc/yum.repos.d/ 目錄下生成 nginx.repo 文件
vim /etc/yum.repos.d/nginx.repo
向nginx.repo文件內添加如下內容
[nginx] name = nginx repo baseurl = https://nginx.org/packages/mainline/centos/7/$basearch/ gpgcheck = 0 enabled = 1
利用yum安裝nginx
sudo yum -y install nginx
開啓nginx服務&&設置開機自啓nginx服務
$ systemctl start nginx
$ systemctl enable nginx
打開瀏覽器輸入本機IP,如果頁面出現welcome nginx!即爲安裝成功,如果服務開啓失敗可以看看是否沒有打開80或者443端口。(也可在圖形界面中找到防火牆設置,開啓端口即可)
$ systemctl start firewalld #開啓防火牆 $ firewall-cmd --zone=public --add-port=80/tcp --permanent #對外開啓80端口 $ firewall-cmd --zone=public --add-port=443/tcp --permanent #對外開啓443端口
3.搭建MySQL
3.1 下載並安裝MySQL官方的 Yum Repository
wget -i -c http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
安裝MySQL的Yum Repository
yum -y install mysql57-community-release-el7-10.noarch.rpm
3.2 安裝MySQL服務器(自動覆蓋掉原有的mariadb)
sudo yum -y install mysql-community-server
3.3 配置MySQL
先啓動MySQL並設置開機自啓
$ systemctl start mysqld.service #啓動MySQL
$ systemctl enable mysqld.service #設置開機自啓
通過日誌文件查看MySQL中root用戶的密碼
$ cat /var/log/mysqld.log | grep "password"