美文网首页PHP
centos7 下 yum 安装 nginx + php7 +

centos7 下 yum 安装 nginx + php7 +

作者: XiWeidong | 来源:发表于2017-10-11 17:50 被阅读1300次

注:为了方便说明,下面部署的这台服务器的ip为106.15.72.29,实验时请使用自己真实的ip

一 【nginx 安装】

参考官方文档 : http://nginx.org/en/linux_packages.html#stable

具体步骤
1.新建文件:vim /etc/yum.repos.d/nginx.repo
2.编辑并保持如下内容:

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

可以使用 vim /etc/yum.repos.d/nginx.repo 来编辑,也可以是用其他文本编辑工具

3.使用yum安装

yum remove nginx  
yum install nginx

yum remove nginx , 是为了有旧的nginx,先卸载

二 【php7,php-fpm 安装】

具体步骤:
1.安装epel-release源

rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm

2.安装PHP7的rpm源

rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm

webtatic-release.rpm 依赖于 epel-release-latest-7.noarch.rpm

3.安装php7

yum remove php*
yum install php70w

yum remove php* 是为了卸载旧版本的php相关软件,不然如果版本不兼容可能会报下面错误:

版本冲突报错

4.安装php相关扩展

yum install php70w-fpm php70w-mysql php70w-xml php70w-mbstring php70w-openssl php70w-gd
yum install php-redis  #redis模块,根据需要选装

php70w-fpm : FastCGI进程管理器
因为php是以FastCGI方式运行的,当用户访问.html等静态页面的时候,nginx直接返回静态页面。当访问.php文件时,nginx会把处理权交给php-fpm,当对.php文件解释执行完成后,把执行结果返回给nginx,再由nginx返回给用户。
php70w-mysql:php操作mysql数据库扩展
php扩展很多,一般根据名称就能知道具体用处,就不多解释了。

三 【安装mysql】

yum -y install mariadb-server mariadb mariadb-devel

MariaDB是MySQL的一个开源分支,基本可以和MySQL通用。
mariadb-server : 服务器端
mariadb : 命令行客户端
mariadb-devel : 开发库

四 【启动相关服务】

1.启动nginx服务

systemctl start nginx.service              

2.启动php-fpm服务

systemctl start  php-fpm.service          

3.启动mysql服务

systemctl start mariadb.service       

使用 netstat -tnl 命令查看,如果80,9000,3306端口都已经被监听,说明nginx,php-fpm,mysql服务都已经启动成功,如图:


nginx监听80,php-fpm监听9000,mysql监听3306

在服务器防火墙能通过的情况下,在浏览器里地址栏里输入: http://106.15.72.29/,如果出现下图界面,表示nginx已经工作正常,恭喜您,网站可以访问静态页面了。


nginx默认首页
如果要访问php动态页面 ,请继续往下看。

五 【nginx配置】

1.打开 /etc/nginx/conf.d/default.conf ,在最上面添加如下代码并保存:

server {
        listen       80;
        server_name  www.test.com;
        client_max_body_size 64M;
        error_page   500 502 503 504  /50x.html;
        root   /opt/web/imapp;
        try_files $uri $uri/ @rewrite;
        location @rewrite {
            rewrite ^/(.*)$ /index.php?_url=/$1;
        }
        
        
        location ~ \.php {
                fastcgi_pass   127.0.0.1:9000;
                fastcgi_index  index.php; 
                fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
                include        fastcgi_params;

                fastcgi_split_path_info ^(.+\.php)(.*)$;
                fastcgi_param PATH_INFO $fastcgi_path_info;
        }
    
}

1.server_name www.test.com; 这里的www.test.com可以根据自己的域名解析修改,当域名解析后,可以根据不同的域名访问不同的server站点
2.root /opt/web/imapp; 这个是当前server站点的主目录,可以把php代码都放到这个目录下,请自行修改。
3.这里还做了去index.php的配置,可以将 http://106.15.72.29/index.php/user/login 简化为 http://106.15.72.29/user/login,有兴趣请自行研究。

2.重新加载配置文件

systemctl reload nginx.service

3.在/opt/web/imapp目录下新建一个test.php文件,编辑php代码并保存

<?php
phpinfo();
?>

4.访问地址:

http://106.15.72.29/test.php

如果出现下图,php页面执行成功


php相关参数信息页面

恭喜您,网站可以访问php动态页面了,环境搭建完成。

六 【服务进程的管理维护】

1.查看nginx服务状态

systemctl list-unit-files --type=service | grep nginx 
查看服务器状态

2.查看nginx的运行状态

systemctl status nginx.service   
nginx服务运行状态

3.设置nginx服务开机启动

systemctl enable nginx.service  

4.设置nginx服务开机不启动

systemctl disable nginx.service  

相关文章

网友评论

    本文标题:centos7 下 yum 安装 nginx + php7 +

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