美文网首页阿里云服务器Unix/Linux服务器技术分享程序员
如何在服务器上搭建一个lamp(Linux(CentOS7)+A

如何在服务器上搭建一个lamp(Linux(CentOS7)+A

作者: 松饼水果十指鱼 | 来源:发表于2017-08-26 16:02 被阅读173次

最近购买了阿里云的ECS云服务器学生机用于建站学习,但是苦于不太会网站环境的搭建,上网搜罗了一大堆教程但是重复性又太高,最后终于找到了合适的解决方法,特地总结分享一下。 

也可以到我的CSDN上查看相关文章:http://blog.csdn.net/qq_36113598/article/details/77532647

PS:我使用的是CentOS 7.3 系统,不同操作系统之间安装代码可能不同,故这篇文章主要针对CentOS 系统

一、 安装Apache

1.安装

yum -y install httpd

2.开启apache服务

systemctl start httpd.service

3.设置apache服务开机启动

systemctl enable httpd.service

4.验证apache服务是否安装成功

在浏览器中输入你的主机外网IP地址,若安装成功则会出现一个Apache欢迎页面,有Testing 123…字样,便是成功安装了apache服务了;

如果不能连接上也不用担心,是因为还没有设置防火墙,解决方法:

CentOS7用的是Firewall-cmd,CentOS7之前用的是iptables防火墙;要想让外网能访问到apache主目录,就需要做以下的操作:

firewall-cmd –permanent –zone=public –add-service=http

firewall-cmd –permanent –zone=public –add-service=https

firewall-cmd –reload

如果上述方法不行也可以尝试把firewalld服务关闭换用CentOS 6以前的防火墙服务iptables,具体如下:

systemctl stop firewalld.service  //关闭firewalld

systemctl start iptables.service  //开启iptables

systemctl enable iptables.service  //自启动iptables

还有很重要的一点是,要在你的阿里云服务器安全组里面设置允许外访问,否则无法从外部访问网站

二、安装PHP

1.安装

yum -yinstall php

2.重启apache服务

systemctl restart httpd或者systemctl restart httpd.service

然后,你可以写一个php文件在浏览器中运行一下了

example:

通过下列vim代码修改创建info.php页面,显示你的系统信息:

vi /var/www/html/info.php

i

<?php phpinfo(); ?>

Esc

:wq

然后,在自己电脑浏览器里输入你的IP(如:192.168.1.1)/info.php

正常情况下就会显示出你的服务器安装的信息,则PHP安装成功,服务器可以搭载动态网页啦!

三、安装MySQL

当然啦,动态网页还需要数据库来存储各种数据,那么接下来就要安装MySQL了;

我这里安装的不是甲骨文的MySQL,而是选择了安装MariaDB

1.安装

yum-yinstall mariadb-servicemariadb

yum-yinstall MariaDB-serverMariaDB-client

yum install mariadb-embeddedmariadb-libsmariadb-benchmariadb mariadb-sever

yuminstallmariadb*

-

PS:上面的组件最好都安装一遍,以免连接PHP失败

-

2.开启MySQL服务

systemctlstartmariadb.service

3.设置开机启动MySQL服务

systemctl enable mariadb.service

4.设置root帐户的密码

mysql_secure_installation

然后会出现一串东西,,按Enter就好了,然后继续在让你选择y/n时,Enter就好了;当一切结束的时候,你可以输入mysql -uroot -p来登录数据库,验证一下是否设置成功;

四、将PHP和MySQL关联起来

首先输入yum search php,选择你需要的安装,然后输入yum -y install php-mysql

五、安装常用的PHP模块

1.安装:

一大串代码,直接复制就好

yum-yinstall php-gdphp-ldapphp-odbcphp-pearphp-xmlphp-xmlrpcphp-mbstringphp-snmpphp-soapcurl curl-devel

2.重启apache服务

systemctl restart httpd.service

然后,再次在浏览器中运行info.php,你会看到安装的模块的信息;

至此,LAMP环境就搭建好了,部署一个动态网页试试吧!

相关文章

网友评论

    本文标题:如何在服务器上搭建一个lamp(Linux(CentOS7)+A

    本文链接:https://www.haomeiwen.com/subject/haglnttx.html