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

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

Nginx是俄罗斯人编写的十分轻量级的HTTP服务器,Nginx是一个高性能的HTTP和反向代理服务器,Nginx 超越 Apache 的高性能和稳定性,使得国内使用 Nginx 作为 Web 服务器的网站也越来越多,

我们学习PHP,以及搭建我们自己的LNMP环境,不妨先在本机上尝试学习,下面我们一步一步来完成在CentOS7 下安装LNMP(Linux+Nginx+MySQL+PHP)及Apache。

查看已经开放的端口
firewall-cmd --list-ports
查看开放的服务
firewall-cmd --list-services

开启端口
firewall-cmd --zone=public --add-port=80/tcp --permanent

firewall-cmd --zone=public --add-port=3306/tcp --permanent

firewall-cmd --zone=public --add-port=8080/tcp --permanent
命令含义:
–zone #作用域
–add-port=80/tcp #添加端口,格式为:端口/通讯协议
–permanent #永久生效,没有此参数重启后失效
重启防火墙
firewall-cmd --reload #重启firewall
systemctl stop firewalld.service #停止firewall
systemctl disable firewalld.service #禁止firewall开机启动

关闭SELINUX
vi /etc/selinux/config
注释掉如下两句,添加最后一项
\#SELINUX=enforcing #注释掉
\#SELINUXTYPE=targeted #注释掉
SELINUX=disabled #增加
:wq! 保存退出
输入如下命令
setenforce 0 #使配置立即生效

getenforce  查看

sestatus

setenforce 0

yum安装 程序过程中 出现占用yum情况:

rm -f /var/run/yum.pid

一、安装httpd

  sudo su -

 yum install -y httpd 

二、启动httpd服务

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

#启动apache

systemctl start httpd.service

#设置apache开机启动

systemctl enable httpd.service

可以在浏览器中输入服务器所在的主机的IP即可看到apache的欢迎界面。

要在另外一台主机上实现这种访问,需要关闭系统的防火墙。

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

systemctl stop firewalld.service 

  //禁止防火墙开机启动

systemctl disable firewalld.service 

防火墙开放端口和服务

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

firewall-cmd --reload

firewall-cmd --list-all

三、安装MySql数据库

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

 yum install -y mariadb mariadb-server

四、开启数据库服务

1、安装完成以后使用下面的命令开启数据库服务:
#启动MariaDB

 systemctl start mariadb.service   

#重启MariaDB

systemctl restart mariadb.service  

#设置开机启动

systemctl enable mariadb.service  

2、配置MariaDB的字符集

vim /etc/my.cnf.d/server.cnf

在[mysqld]标签下添加
init_connect='SET collation_connection = utf8_unicode_ci' 
init_connect='SET NAMES utf8' 
character-set-server=utf8 
collation-server=utf8_unicode_ci 
skip-character-set-client-handshake

文件/etc/my.cnf.d/client.cnf
vi /etc/my.cnf.d/client.cnf

在[client]中添加
default-character-set=utf8
文件/etc/my.cnf.d/mysql-clients.cnf
vi /etc/my.cnf.d/mysql-clients.cnf
在[mysql]中添加
default-character-set=utf8
 全部配置完成,重启mariadb
systemctl restart mariadb
之后进入MariaDB查看字符集

mysql -uroot -p 回车

mysql> show variables like "%character%";show variables like "%collation%";
字符集配置完成。

3、添加用户,设置权限
创建用户命令
mysql>create user username@localhost identified by 'password';
直接创建用户并授权的命令
mysql>grant all on *.* to username@localhost identified by 'password';
授予外网登陆权限 
mysql>grant all privileges on *.* to username@'%' identified by 'password';
授予权限并且可以授权
mysql>grant all privileges on *.* to username@'hostname' identified by 'password' with grant option;

简单的用户和权限配置基本就这样了。
其中只授予部分权限把 其中 all privileges或者all改为select,insert,update,delete,create,drop,index,alter,grant,references,reload,shutdown,process,file其中一部分。
设置root账户密码
mysql_secure_installation
然后一路输入y就可以。
设置root密码后,重启MariaDB生效
systemctl restart mariadb.service
测试访问数据库:
mysql -uroot -p
show databases;
退命令:
exit;

五、安装PHP

 yum -y install php 

使用下面的命令安装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”

php --version

五、重启Mariadb和httpd服务:

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

#重启MariaDB

systemctl restart mariadb.service 

#重启apache

systemctl restart httpd.service 

vim /var/www/html/index.php ->内容:<?php phpinfo();  ?>

六、安装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 -y install nginx
由于安装了Httpd服务,所以要先停止,关闭apache之后再次启动nginx。

停止Httpd

systemctl stop httpd.service 

测试一下 nginx 服务:

systemctl status httpd.service

测试一下 nginx 的配置文件:

nginx -t   

返回

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

说明配置文件成功!

开启服务:systemctl start nginx.service

开机启动:systemctl enable nginx.service

七、配置服务

这里使用的是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端口访问nginx服务器:

http://localhost

(4)指定8080端口访问apache服务器:

http://localhost:8080

(5)指定8080端口访问apache服务器下的mysql->mariadb

http://locahost:8080/phpmyadmin

参考:http://www.kimsom.com/article/138

http://www.jb51.net/article/68026.htm

添加nginx服务:

 http://www.cnblogs.com/riverdubu/p/6426852.html

猜你喜欢

转载自blog.csdn.net/yongzhen150/article/details/81114188