部署Nginx服务与PHP服务

准备

镜像:CentOS-7-x86_64-DVD-1511.iso

配置yum源

关闭防火墙和se

安装配置基础服务

执行以下命令,打开 default.conf 文件

yum install -y nginx

执行以下命令,打开 default.conf 文件

vim /etc/nginx/conf.d/default.conf

找到 server{...},并将 server 大括号中相应的配置信息替换为如下内容。用于取消对 IPv6 地址的监听,同时配置 Nginx,实现与 PHP 的联动

server {
    listen       80;
    root   /usr/share/nginx/html;
    server_name  localhost;
    #charset koi8-r;
    #access_log  /var/log/nginx/log/host.access.log  main;
    #
    location / {
          index index.php index.html index.htm;
    }
    #error_page  404              /404.html;
    #redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
      root   /usr/share/nginx/html;
    }
    #pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    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;
    }
}

 执行以下命令启动 Nginx

systemctl start nginx

 

执行以下命令,设置 Nginx 为开机自启动

systemctl enable nginx 

 

在本地浏览器中访问以下地址,查看 Nginx 服务是否正常运行

http://虚拟机的IP

 

显示如下,则说明 Nginx 安装配置成功 

 安装数据库

执行以下命令,查看系统中是否已安装 MariaDB

rpm -qa | grep -i mariadb
##返回结果

  为避免安装版本不同造成冲突,请执行以下命令移除已安装的 MariaDB

  yum -y remove 包名

执行以下命令,安装 MariaDB。此步骤耗时较长,请关注安装进度,等待安装完毕

yum -y install MariaDB-client MariaDB-server

执行以下命令,启动 MariaDB 服务

systemctl start mariadb

执行以下命令,设置 MariaDB 为开机自启动

systemctl enable mariadb

执行以下命令,验证 MariaDB 是否安装成功

mysql

显示结果如下,则成功安装

 

安装配置 PHP

依次执行以下命令,更新 yum 中 PHP 的软件源

rpm -Uvh https://mirrors.cloud.tencent.com/epel/epel-release-latest-7.noarch.rpm
rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm

执行以下命令,安装 PHP 7.2 所需要的包

yum -y install mod_php72w.x86_64 php72w-cli.x86_64 php72w-common.x86_64 php72w-mysqlnd php72w-fpm.x86_64

 执行以下命令,启动 PHP-FPM 服务

systemctl start php-fpm

执行以下命令,设置 PHP-FPM 服务为开机自启动

systemctl enable php-fpm

验证环境配置

当您完成环境配置后,可以通过以下验证 LNMP 环境是否搭建成功。

执行以下命令,创建测试文件

echo "<?php phpinfo(); ?>" >> /usr/share/nginx/html/index.php

执行以下命令,重启 Nginx 服务

systemctl restart nginx

在本地浏览器中访问如下地址,查看环境配置是否成功

http://虚拟机地址

显示结果如下, 则说明环境配置成功

 

posted @ 2021-12-22 17:14  花花de代码生活  阅读(310)  评论(0编辑  收藏  举报