1. 程式人生 > >centos7 yum安裝LAMP

centos7 yum安裝LAMP

說明:我安裝後的版本號分別是:

apache : Apache/2.4.6 (CentOS)
mysql:5.6.42
php:5.6.39

 

一、配置網路。

我們首先需要讓我們的虛擬機器能夠連線上外網,這樣才能方便我們使用yum進行安裝。

1、如果只是自己開發用的話,最簡潔的配置,我們只需要配置一個IP和閘道器就可以了(根據自己的實際情況)

vim /etc/sysconfig/network-scripts/ifcfg-ens33

DEVICE=ens33
IPADDR=192.168.1.101
GATEWAY=192.168.1.1 
ZONE=public

2、配置DNS,具體的值可以在 cmd 的ipconfig -all 進行檢視

vim /etc/resolv.conf

nameserver xxx.xxx.xxx.xxx

3、service network restart // 重啟網路服務

4、ping 192.168.1.101 或 ping www.baidul.com //檢測網路是否可以連線網路了。


二、配置防火牆和SELINUX

開啟80、3306埠。CentOS 7.0預設使用的是firewall作為防火牆,這裡改為iptables防火牆

1、關閉firewall

#停止firewall服務
systemctl stop firewalld.service

#禁止firewall開機啟動
systemctl disable firewalld.service

2、安裝iptables

yum install iptables-services


#編輯防火牆配置檔案

vim /etc/sysconfig/iptables

加入紅色的兩行程式碼,請注意位置一定要對應。

1 # sample configuration for iptables service
2 # you can edit this manually or use system-config-firewall
3 # please do not ask us to add additional ports/services to this default configuration
4 *filter 5 :INPUT ACCEPT [0:0] 6 :FORWARD ACCEPT [0:0] 7 :OUTPUT ACCEPT [0:0] 8 -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT 9 -A INPUT -p icmp -j ACCEPT 10 -A INPUT -i lo -j ACCEPT 11 -A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT 12 -A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT 13 -A INPUT -p tcp -m state --state NEW -m tcp --dport 3306 -j ACCEPT 14 -A INPUT -j REJECT --reject-with icmp-host-prohibited 15 -A FORWARD -j REJECT --reject-with icmp-host-prohibited 16 COMMIT


3、最後重啟防火牆使配置生效

systemctl restart iptables.service
#設定防火牆開機啟動
systemctl enable iptables.service

4、關閉selinux
#修改配置檔案
vi /etc/selinux/config

1 
2 # This file controls the state of SELinux on the system.
3 # SELINUX= can take one of these three values:
4 # enforcing - SELinux security policy is enforced.
5 # permissive - SELinux prints warnings instead of enforcing.
6 # disabled - No SELinux policy is loaded.
7 #SELINUX=enforcing
8 SELINUX=disabled
9 # SELINUXTYPE= can take one of three two values:
10 # targeted - Targeted processes are protected,
11 # minimum - Modification of targeted policy. Only selected processes are protected. 
12 # mls - Multi Level Security protection.
13 #SELINUXTYPE=targeted

 

5、使配置立即生效
setenforce 0


三、安裝apache

yum install -y httpd

可能會用到的:

systemctl start httpd.service //啟動apache

systemctl stop httpd.service //停止apache

systemctl restart httpd.service //重啟apache

systemctl enable httpd.service //設定apache開機啟動

systemctl restart httpd.service //重啟服務

輸入 192.168.1.101 出現如下介面,就代表apache安裝成功。

 

四、安裝mysql

由於yum源上沒有mysql-server。所以必須去官網下載後在安裝嗎,這裡我們用wget命令,直接獲取。 

wget http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm

rpm -ivh mysql-community-release-el7-5.noarch.rpm

yum install -y mysql-community-server

systemctl restarat mysqld.service //安裝完成後重啟mysql

初始安裝 root使用者沒有密碼,設定一個祕密

mysql -u root

#設定msyql密碼為 123456
mysql> set password for 'root'@'localhost' =password('123456');

#遠端連線設定,所有以root賬號連線的遠端使用者,設其密碼為 123456
mysql> grant all privileges on *.* to [email protected]'%'identified by '123456';

#更新許可權
mysql>flush privileges;  


五、安裝PHP

由於自帶的yum 源php版本是 php5.4 ,我覺得有點兒低,在此安裝php5.6

首先我們需要追加CentOS 7.0的epel及remi源。

yum install -y epel-release

rpm -ivh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm

//使用yum list命令檢視可安裝的包(Packege)。
yum list --enablerepo=remi --enablerepo=remi-php56 | grep php

//yum源配置好了,下一步就安裝PHP5.6。

yum install -y --enablerepo=remi --enablerepo=remi-php56 php php-opcache php-devel php-mbstring php-mcrypt php-mysqlnd php-phpunit-PHPUnit php-pecl-xdebug php-pecl-xhprof

//用PHP命令檢視版本。
php --version

六、驗證LAMP是否安裝成功

1、重啟一下 apache 和 mysql

2、cd /var/www/html

vim index.php 寫入 phpinfo();

驗證 http://192.168.4.147/index.php ,出現如下介面,代表安裝OK。

 

到此為止,我們的LAMP環境就搭建好啦!

參考了博友的部落格:https://www.linuxidc.com/Linux/2016-11/136766.htm

https://www.cnblogs.com/jie-hu/p/5950584.html

感謝他們!