CentOS搭建LAMP
1、配置防火墙
打开防火墙配置文件
vi /etc/sysconfig/iptables
--------------------------
-A INPUT -m state –state NEW -m tcp -p tcp –dport 22 -j ACCEPT -A INPUT -m state –state NEW -m tcp -p tcp –dport 80 -j ACCEPT #允许80端口通过防火墙 -A INPUT -m state –state NEW -m tcp -p tcp –dport 3306 -j ACCEPT #允许3306端口通过防火墙
--------------------------
设置80、3306端口可以通过防火墙
重启防火墙
/etc/init.d/iptables restart #最后重启防火墙使配置生效或者是【service iptables restart 】
2、安装Apache
卸载自带httpd:
yum -y remove httpd
安装
yum -y install httpd httpd-devel
安装完成后,启动apache ,执行命令:
service httpd start #或者是【/etc/init.d/httpd start】
设为开机启动,执行命令:
chkconfig httpd on
3.安装mysql
卸载自带mysql
yum -y remove mysql mysql-server mysql-libs compat-mysql51
安装
yum -y remove mysql mysql-server mysql-libs compat-mysql51
停止mysql,执行命令:
mysqld_safe --user=mysql --skip-grant-tables --skip-networking &
进入mysql
mysql -u root mysql
mysql的使用
----------------------------------
mysql>USE mysql; mysql> UPDATE user SET Password=PASSWORD('123') where USER='root'; #修改新密码为123 mysql> FLUSH PRIVILEGES; mysql> quit;
----------------------------------
重启mysql
service mysqld restart #或者是【/etc/init.d/mysql restart】
再次重设mysql的密码
mysql -uroot -p
Enter password: <输入新设的密码newpassword>
4、安装php
yum -y remove php php-mysql php-common php-gd php-mbstring php-mcrypt php-devel php-xml yum -y install php php-mysql php-common php-gd php-mbstring php-mcrypt php-devel php-xml
重启http服务
service httpd start #或者是【/etc/init.d/httpd start】
5、测试
vi /var/www/html/test.php
写入内容
-------------------------
<?php
phpinfo();
?>
-------------------------
重启http服务
/etc/init.d/httpd restart
打开网址
http://192.168.216.130/test.php
shell脚本搭建LAMP
---------------------------------------------------------------------------------------------------
1 #!/bin/bash 2 A_apcache="httpd httpd-devel" 3 M_mysql="mysql mysql-server mysql-devel" 4 P_php="php php-mysql php-common php-gd php-mbstring php-mcrypt php-devel php-xml" 5 I_IP="ifconfig|grep 192|awk '{print $2}'|cut -d : -f 2" 6 echo -e "请输入需要安装/卸载的软件的编号 7 若已安装过软件请先卸载,运行7仅能卸载yum安装的软件" 8 echo "----------------------------------------------------" 9 echo "1 安装apcache" 10 echo "2 安装msyql" 11 echo "3 安装php" 12 echo "4 安装完成后编写一个php测试文件" 13 echo "5 设置防火墙3306端口和80端口" 14 echo "6 重启LAMP服务" 15 echo "7 先卸载自带的lamp环境" 16 echo "----------------------------------------------------" 17 read a 18 case $a in 19 1) 20 yum -y install $A_apcache & 21 ;; 22 2) 23 yum -y install $M_mysql & 24 ;; 25 3) 26 yum -y install $P_php & 27 ;; 28 4) 29 30 cat <<EOF >/var/www/html/testphp.php 31 <?php 32 phpinfo(); 33 ?> 34 EOF 35 ;; 36 5) 37 cat <<EOF >/etc/sysconfig/iptables 38 # Firewall configuration written by system-config-firewall 39 # Manual customization of this file is not recommended. 40 *filter 41 :INPUT ACCEPT [0:0] 42 :FORWARD ACCEPT [0:0] 43 :OUTPUT ACCEPT [0:0] 44 -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT 45 -A INPUT -p icmp -j ACCEPT 46 -A INPUT -i lo -j ACCEPT 47 -A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT 48 -A INPUT -m state –state NEW -m tcp -p tcp –dport 80 -j ACCEPT #允许80端口通过防火墙 49 -A INPUT -m state –state NEW -m tcp -p tcp –dport 3306 -j ACCEPT #允许3306端口通过防火墙 50 -A INPUT -j REJECT --reject-with icmp-host-prohibited 51 -A FORWARD -j REJECT --reject-with icmp-host-prohibited 52 COMMIT 53 EOF 54 echo "防火墙已经设置3306、80端口" 55 ;; 56 6) 57 service iptables restart 58 service httpd restart 59 service mysqld restart 60 ;; 61 7) 62 yum -y remove A_apcache 63 echo "----------------------------------------------------" 64 echo "Apache卸载完成" 65 echo "----------------------------------------------------" 66 yum -y remove M_mysql 67 echo "----------------------------------------------------" 68 echo "mysql卸载完成" 69 echo "----------------------------------------------------" 70 yum -y remove P_php 71 echo "----------------------------------------------------" 72 echo "php卸载完成" 73 echo "访问地址为:"$I_IP 74 ;; 75 76 *) 77 echo "输入错误" 78 break 79 ;;