centos7安装Lnmp(Linux+Nginx+MySql+Php)及Apache

Nginx是俄罗斯人编写的十分轻量级的HTTP服务器,Nginx是一个高性能的HTTP和反向代理服务器,Nginx 超越 Apache 的高性能和稳定性,使得国内使用 Nginx 作为 Web 服务器的网站也越来越多,我们学习PHP,以及搭建我们自己的LNMP环境,不妨先在本机上尝试学习,下面我们一步一步来完成在CentOS7 下安装LNMP(Linux+Nginx+MySQL+PHP)及Apache。

一、安装httpd

[root@localhost ~]# yum install -y httpd

安装成功

Installed:
  httpd.x86_64 0:2.4.6-40.el7.centos.1                                          

Dependency Installed:
  apr.x86_64 0:1.4.8-3.el7                      apr-util.x86_64 0:1.5.2-6.el7   
  httpd-tools.x86_64 0:2.4.6-40.el7.centos.1    mailcap.noarch 0:2.1.41-2.el7   

Complete!

二、启动httpd服务

[root@localhost ~]# systemctl start httpd.service

安装完成之后使用以下命令启动httpd服务:
#启动apache

systemctl start httpd.service

#停止apache

systemctl stop httpd.service

#重启apache

systemctl restart httpd.service

#设置apache开机启动

systemctl enable httpd.service

可以在浏览器中输入服务器所在的主机的IP即可看到apache的欢迎界面。要在另外一台主机上实现这种访问,需要关闭系统的防火墙。
在CentOS7中,修改防火墙的机制已经做了修改,在CentOS 6.x系统中可以使用以下命令:

service iptables stop

// 开机禁止启动

chkconfig iptables off

    而在CentOS7中只能使用以下命令,如果使用上面的命令并不会报任何错误,但是起不到关闭防火墙的效果:

systemctl stop firewalld.service

//禁止防火墙开机启动

systemctl disable firewalld.service

三、安装MySql数据库

MySQL数据库,新版本已经更名为Mariadb,所以这里需要安装Mariadb,可以使用下面的命令进行安装:

[root@localhost ~]# yum install -y mariadb

安装成功

Installed:
  mariadb.x86_64 1:5.5.47-1.el7_2                                               

Dependency Updated:
  mariadb-libs.x86_64 1:5.5.47-1.el7_2                                          

Complete!

如果出现:

CentOS7.2安装mariadb-server,解决Failed to start mysqld.service: Unit not found

方法一:当输入命令

~]# systemctl start mariadb.service

要启动MySQL数据库是却是这样的提示

Failed to start mariadb.service: Unit not found

解决方法如下:

  首先需要安装mariadb-server

  ~]# yum install -y mariadb-server

  启动服务

  ~]# systemctl start mariadb.service

  添加到开机启动

  ~]# systemctl enable mariadb.service

进行一些安全设置,以及修改数据库管理员密码

  ~]# mysql_sceure_installation

至此完成!

可以测试一下

  ~]#  mysql -u root -p

方法二:

centos7使用yum安装mariaDB(开源MySQL)无法启动的解决办法

centos7内置的MySQL镜像已经放弃Oracle公司的MySQL,改用MySQL的分支数据库mariaDB,使用以下安装mariadb:

yum install mariadb  

然后使用命令systemctl start mariadb,提示如下:

Failed to start mariadb.service: Unit mariadb.service failed to load: No such file or directory  

意思找不到mariadb这个服务。之所以找不到,是因为mariadb的安装本身就没有完成,执行以下命令,查看mariadb的依赖情况:

$ sudo yum search mariadb  

看到以下结果:

 

执行以下,安装缺少的依赖包:

$ yum install mariadb-embedded mariadb-libs mariadb-bench mariadb mariadb-sever  

然后再启动mariadb,正常,如果要设置开机自启动mariadb,使用以下命令:

$ systemctl enable mariadb 

一定要记得,使用yum install命令安装mysql时,需要多加一个通配符"*",使用如下命令:

yum install mariadb*  

四、开启数据库服务

安装完成以后使用下面的命令开启数据库服务:
#启动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

五、安装PHP

[root@localhost ~]# yum -y install php

安装成功:

Installed:
  php.x86_64 0:5.4.16-36.1.el7_2.1                                                       

Dependency Installed:
  libzip.x86_64 0:0.10.1-8.el7                 php-cli.x86_64 0:5.4.16-36.1.el7_2.1     
  php-common.x86_64 0:5.4.16-36.1.el7_2.1     

Complete!

使用下面的命令安装php对Mariadb的支持:

yum install php-mysql php-gd libjpeg* php-ldap php-odbc php-pear php-xml php-xmlrpc php-mbstring php-bcmath php-mhash

一路“Y”

五、重启Mariadb和httpd服务:

使用下面的命令重启Mariadb和httpd服务:

#重启MariaDB

systemctl restart mariadb.service

#重启apache

systemctl restart httpd.service

六、安装nginx

想在 CentOS 系统上安装 Nginx ,你得先去添加一个资源库,像这样:

vim /etc/yum.repos.d/nginx.repo

使用 vim 命令去打开 /etc/yum.repos.d/nginx.repo ,如果 nginx.repo 不存在,就会去创建一个这样的文件,打开以后按一下小 i 键,进入编辑模式,然后复制粘贴下面这几行代码,完成以后按 esc 键退出,再输入 :wq (保存并退出)

[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1

完成以后,我们就可以使用 yum 命令去安装 nginx 了,像这样:

yum install nginx

安装成功:

----------------------------------------------------------------------

Thanks for using nginx!

Please find the official documentation for nginx here:
* http://nginx.org/en/docs/

Commercial subscriptions for nginx are available on:
* http://nginx.com/products/

----------------------------------------------------------------------
  Verifying  : 1:nginx-1.10.1-1.el7.ngx.x86_64                                       1/1 

Installed:
  nginx.x86_64 1:1.10.1-1.el7.ngx                                                        

Complete!

由于安装了Httpd服务,所以要先停止,关闭apache之后再次启动nginx。
停止Httpd

systemctl stop httpd.service

测试一下 nginx 服务:

service nginx status

测试一下 nginx 的配置文件:

nginx -t

返回

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

说明配置文件成功!

七、配置服务

这里使用的是nginx做反向代理,将其客户端通过80端口请求的.php内容代理到apache服务器上。

要想使用nginx做反向代理,需要修改Apache的httpd和nginx的配置文件,使其监听不同的端口,这里我们使用nginx监听80端口,使用Apache监听8080端口,这里我们分别配置Apache和nginx的配置文件,修改结果如下:
(1)Apache配置文件:/etc/httpd/conf/httpd.conf

#Listen 12.34.56.78:80
Listen 8080

(2)nginx配置如下:/etc/nginx/conf.d/default.conf

# proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    location ~ \.php$ {
      proxy_pass   http://127.0.0.1:8080;
    }

(3)不指定8080端口访问:

8aa43aae35cea56028fcb835fc4576afae4.jpg

(4)指定8080端口访问:

804c69c2feeacf30e84b8242aaadaef492d.jpg

 
 
G
M
T
 
 
Detect languageAfrikaansAlbanianArabicArmenianAzerbaijaniBasqueBelarusianBengaliBosnianBulgarianCatalanCebuanoChichewaChinese (Simplified)Chinese (Traditional)CroatianCzechDanishDutchEnglishEsperantoEstonianFilipinoFinnishFrenchGalicianGeorgianGermanGreekGujaratiHaitian CreoleHausaHebrewHindiHmongHungarianIcelandicIgboIndonesianIrishItalianJapaneseJavaneseKannadaKazakhKhmerKoreanLaoLatinLatvianLithuanianMacedonianMalagasyMalayMalayalamMalteseMaoriMarathiMongolianMyanmar (Burmese)NepaliNorwegianPersianPolishPortuguesePunjabiRomanianRussianSerbianSesothoSinhalaSlovakSlovenianSomaliSpanishSundaneseSwahiliSwedishTajikTamilTeluguThaiTurkishUkrainianUrduUzbekVietnameseWelshYiddishYorubaZulu
 
AfrikaansAlbanianArabicArmenianAzerbaijaniBasqueBelarusianBengaliBosnianBulgarianCatalanCebuanoChichewaChinese (Simplified)Chinese (Traditional)CroatianCzechDanishDutchEnglishEsperantoEstonianFilipinoFinnishFrenchGalicianGeorgianGermanGreekGujaratiHaitian CreoleHausaHebrewHindiHmongHungarianIcelandicIgboIndonesianIrishItalianJapaneseJavaneseKannadaKazakhKhmerKoreanLaoLatinLatvianLithuanianMacedonianMalagasyMalayMalayalamMalteseMaoriMarathiMongolianMyanmar (Burmese)NepaliNorwegianPersianPolishPortuguesePunjabiRomanianRussianSerbianSesothoSinhalaSlovakSlovenianSomaliSpanishSundaneseSwahiliSwedishTajikTamilTeluguThaiTurkishUkrainianUrduUzbekVietnameseWelshYiddishYorubaZulu
 
 
 
 
 
 
 
 
 
Text-to-speech function is limited to 200 characters
 
 
Options : History : Feedback : DonateClose

转载于:https://my.oschina.net/botkenni/blog/1827845

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值