centos lamp环境搭建 centos7搭建apache服务器
大家好,如果您还对centos lamp环境搭建不太了解,没有关系,今天就由本站为大家分享centos lamp环境搭建的知识,包括centos7搭建apache服务器的问题都会给大家分析到,还望可以解决大家的问题,下面我们就开始吧!
如何搭建lamp(CentOS7+Apache+MySQL+PHP)环境
在网上搜资料,自己在本地虚拟机上尝试搭建,弄了整整一天一夜,终于弄好了.网上的资料,虽然很多,但大多都是重复的,拿去试了之后,又很多都不能得到正确的结果.最终找到了适合我的linux环境的搭建方式;在这里贴出来:度娘真不给力啊,没搜出来靠谱的方法。最后用喜乐搜找到的靠谱的教程。
这里还是要总结一下我的LAMP环境的搭建步骤。我先在电脑里装了虚拟机,在虚拟机中测试了数次之后,再在服务器上搭建的。说说我的环境:虚拟机是:VMware® Workstation 12.1.1 Pro;Linux系统用的是:CentOS-7-x86_64-DVD-1511.iso;(阿里云上也是用的CentOS7-64bit)准备好这两个之后,就开始一步一步搭建我们的LAMP环境了。
一、安装虚拟机
二、安装CentOS7
注意:以下安装,我都是用的root权限。
三、安装Apache1.安装yum-y install httpd2.开启apache服务systemctl start httpd.service3.设置apache服务开机启动systemctl enable httpd.service4.验证apache服务是否安装成功在本机浏览器中输入虚拟机的ip地址,CentOS7查看ip地址的方式为:ip addr(阿里云不需要用这种方式查看,外网ip已经在你主机列表那里给你写出来了的;)这里是访问不成功的(阿里云用外网访问,能成功,不需要做以下步骤)查了资料,说法是,CentOS7用的是Firewall-cmd,CentOS7之前用的是iptables防火墙;要想让外网能访问到apache主目录,就需要做以下的操作:firewall-cmd--permanent--zone=public--add-service=httpfirewall-cmd--permanent--zone=public--add-service=httpsfirewall-cmd--reload然后再访问外网ip,如果看到apache默认的页面--有Testing 123...字样,便是成功安装了apache服务了;
四、安装PHP1.安装yum-y install php2.重启apache服务systemctl restart httpd或者systemctl restart httpd.service然后,你可以写一个php文件在浏览器中运行一下了;eg:vi/var/www/html/info.phpi<?php phpinfo();?>Esc:wq然后,在自己电脑浏览器输入 192.168.1.1/info.php运行,会出现php的一些信息
五、安装MySQL我这里根据所学的那个教程,也安装了MariaDB1.安装
yum-y install mariadb*
2.开启MySQL服务systemctl start mariadb.service3.设置开机启动MySQL服务systemctl enable mariadb.service4.设置root帐户的密码mysql_secure_installation然后会出现一串东西,可以仔细读一下,如果你懒得读,就在提示出来的时候,按Enter就好了,让你设置密码的时候,你就输入你想要的密码就行,然后继续在让你选择y/n是,Enter就好了;当一切结束的时候,你可以输入mysql-uroot-p的方式,验证一下;
六、将PHP和MySQL关联起来yum search php,选择你需要的安装:yum-y install php-mysql
七、安装常用的PHP模块例如,GD库,curl,mbstring,...1.安装:yum-y install php-gd php-ldap php-odbc php-pear php-xml php-xmlrpc php-mbstring php-snmp php-soap curl curl-devel2.重启apache服务systemctl restart httpd.service然后,再次在浏览器中运行info.php,你会看到安装的模块的信息;
至此,LAMP环境就搭建好了。
如何搭建lamp(CentOS7+Apache+MySQL+PHP)环境
标签:
最小化Centos7虚拟机环境搭建
搭建CentOS7虚拟机环境提供了一个简便且灵活的解决方案,适用于学习Linux系统使用、系统环境搭建或掌握docker。此方案在Windows10系统中运行,能够随时开启、关闭Linux环境,且支持创建快照,方便还原一个纯净的系统。我们选择了CentOS7 minimal版本作为基础,它只包含必要的Linux软件,体积小巧(约1G),适合后续用户根据需求自行安装所需软件。
搭建所需软件包括:CentOS-7-x86_64-Minimal-2003.iso Linux系统镜像、虚拟机(推荐使用VMware Workstation 12 Pro,试用期30天,需购买license以长期使用)、Putty(强大的远程ssh登录工具)以及运行Windows 10, 64-bit(Build 18363) 10.0.18363系统的电脑。
在VMware虚拟机软件中,选择新建虚拟机并配置典型安装,随后安装最小化centos7的iso文件。设置虚拟机名称、保存位置,然后开始安装系统。安装过程中,可自定义日期、时间、网络、主机名等配置,完成系统安装并设置root账号密码及个人账户。
安装完成CentOS7后,需进行网络配置,选择NAT模式,并通过命令行验证网络连接状态。配置完成后,安装vim、wget等常用工具,更新为国内网络源以提高下载速度,同时安装ifconfig并关闭防火墙。创建快照功能便于在系统出现问题时恢复到特定状态。
使用Putty作为远程访问工具,实现与CentOS虚拟机的连接,方便在Windows和虚拟机之间切换操作。至此,CentOS7虚拟机环境搭建完成,具备基本功能,后续可在此基础上安装LAMP系统以实现具体应用。
怎么安装PHP环境
LAMP/LNMP环境搭建
一.检查系统环境
1.确认centos版本
[root@localhost~]# cat/etc/redhat-release
2.检查是否安装过apache
rpm-qa| grep httpd
或者:
apachectl-v
或者:
httpd-v
3.检查是否安装过Mysql
service mysqld start
如果未被识别则没有安装
如果系统安装过,或者安装失败,清理一下系统
4.清理Mysql痕迹
yum remove mysql
rm-f/etc/my.cnf
5.卸载Apache包
rpm-qa|grep httpd
注意:如果是新的系统或者你从来没有尝试安装过,则以上步骤省略
二.安装Apache、PHP、Mysql
停止防火墙服务
[root@localhost~]# systemctl stop firewalld.service
禁用防火墙开机启动服务
[root@localhost~]# systemctl disable firewalld.service
1.安装apache
[root@localhost~]# yum-y install httpd
2.安装php
[root@localhost~]# yum-y install php
3.安装php-fpm
[root@localhost~]# yum-y install php-fpm
4.安装Mysql
[root@localhost~]# yum-y install mysql
5.安装 mysql-server
[root@localhost~]# yum install mariadb-server
CentOS 7+版本将MySQL数据库软件从默认的程序列表中移除,用mariadb代替了,entos7配置教程上,大多都是安装mariadb,因为centos7默认将mariadb视作mysql。
因为mysql被oracle收购后,原作者担心mysql闭源,所以又写了一个mariadb,这个数据库可以理解为mysql的分支。如果需要安装mariadb,只需通过yum就可。
6.安装 php-mysql
[root@localhost~]# yum-y install php-mysql
三.安装基本常用扩展包
1.安装Apache扩展包
yum-y install httpd-manual mod_ssl mod_perl mod_auth_mysql
2.安装PHP扩展包
yum-y install php-gd php-xml php-mbstring php-ldap php-pear php-xmlrpc php-devel
3.安装Mysql扩展包
yum-y install mysql-connector-odbc mysql-devel libdbi-dbd-mysql
四.配置Apache、mysql开机启动
重启Apache、mysql服务(注意这里和centos6有区别,Cenots7+不能使用6的方式)
systemctl start httpd.service#启动apache
systemctl stop httpd.service#停止apache
systemctl restart httpd.service#重启apache
systemctl enable httpd.service#设置apache开机启动
重启数据库
#启动MariaDB
[root@localhost~]# systemctl start mariadb.service
#停止MariaDB
[root@localhost~]# systemctl stop mariadb.service
#重启MariaDB
[root@localhost~]# systemctl restart mariadb.service
#设置开机启动
[root@localhost~]# systemctl enable mariadb.service
五.配置Mysql
初次安装mysql是没有密码的,我们要设置密码,mysql的默认账户为root
方式1:设置 MySQL数据 root账户的密码:
[root@localhost~]# mysql_secure_installation
当出现如下提示时候直接按回车:
Enter current password for root
出现如下再次回车:
Set root password? [Y/n]
出现如下提示输入你需要设置的密码,这里输入了root,输入密码是不显示的,回车后再输入一次确认:
New password:
接下来还会有四个确认,分别是:
Remove anonymous users? [Y/n]
Disallow root login remotely? [Y/n]
Remove test database and access to it? [Y/n]
Reload privilege tables now? [Y/n]
直接回车即可。
方式2:进入mysql mysql-r
修改mysql密码:set password for'root'@'localhost'=password('root');
mysql授权远程连接(navicat等): grant all on*.* to root identified by'root';
六.测试环境
我们在浏览器地址栏输入,正常显示,说明我们的lamp环境搭建成功
七.安装nginx
yum install yum-priorities-y
wget
rpm-ivh nginx-release-centos-7-0.el7.ngx.noarch.rpm
yum install nginx
Nginx版本号可变更$版本
八.配置nginx
1.nginx启动,停止,重启
systemctl start nginx.service#启动nginx
systemctl stop nginx.service#停止
systemctl restart nginx.service#重启
systemctl enable nginx.service#设置开机启动
更改nginx端口号(根据自己需求)
cd/etc/nginx/conf.d/
vim default.conf
把listen 80改成listen 81
3.访问即可看到nginx首页
安装完成访问时候需要启动php-fpm,不重启访问会出现下载文件,重启命令如下
systemctl start php-fpm.service#启动php-fpm
systemctl enable php-fpm.service#设置开机启动
4.更改nginx配置文件识别php
vi/etc/nginx/conf.d/default.conf,把之前的#给去掉就可以了,顺手改一下
location~.php${
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME/usr/share/nginx/html/$fastcgi_script_name;
include fastcgi_params;
}
方法二安装nginx
yum install nginx
启动nginx,并设置为开机启动
systemctl start nginx
systemctl enable nginx
vim/etc/nginx/conf.d/default.conf
5.在/usr/share/nginx/html中新建一个test.php
访问即可看到php页面
修改完成配置记得启动apache和php-fpm哦!
九.负载配置
upstream site{
server 172.16.170.138;
server 172.16.170.139;
}
server{
listen 80;
server_name localhost;
#charset koi8-r;
#access_log/var/log/nginx/log/host.access.log main;
location/{
root/usr/share/nginx/html;
index index.html index.htm;
proxy_pass ;
}
apache默认目录/var/www/html
nginx默认目录/usr/share/nginx/html
升级php版本
centos7默认安装php是5.4
查看yum的可安装的php版本列表
yum provides php
开始升级PHP更新源:
rpm-Uvh
rpm-Uvh
yum remove php-common-y#移除系统自带的php-common
php72版本
yum install-y php72w php72w-opcache php72w-xml php72w-mcrypt php72w-gd php72w-devel php72w-mysql php72w-intl php72w-mbstring
php56版本
yum install-y php56w php56w-opcache php56w-xml php56w-mcrypt php56w-gd php56w-devel php56w-mysql php56w-intl php56w-mbstring#安装依赖包
查看php版本
php-v
安装php fpm:
yum install php72w-fpm
yum provides php-fpm#因为我是准备搭建lnmp,所以安装php-fpm,这里会提示多个安装源,选择5.6版本的安装就可以了
yum install php56w-fpm-5.6.31-1.w7.x86_64-y
systemctl start php-fpm.service【启动】
systemctl enable php-fpm.service【开机自启动】
CentOS yum有时出现“Could not retrieve mirrorlist”的解决办法——resolv.conf的配置
原因:没有配置resolv.conf
解决方法:
到/etc目录下配置resolv.conf加入nameserver IP,如:
nameserver 8.8.8.8
nameserver 8.8.4.4
search localdomain
保存再次运行上面的命令就可以。
nginx目录:/usr/share/nginx/html
/etc/nginx/conf.d/default.conf nginx配置目录
apache目录:/var/www/html
/etc/httpd/conf/httpd.conf apache配置文件
nginx配置域名
cd/etc/nginx
cp default.conf imooc.conf
修改server_name imooc.test.com以及项目目录
配置虚拟域名 windows访问需要在host增加 linuxip
ServerName www.nine.com
DocumentRoot"/var/www/html/learnlaravel/public"
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
关闭防火墙
setenforce 0