LNMP环境搭建

本文详细介绍了如何在LinuxContOS7.9系统上快速搭建Nginx1.2、MySQL8.0和PHP8.0的一站式LNMP环境。首先,关闭防火墙并安装必要依赖,然后分别安装并配置Nginx、MySQL和PHP。在Nginx中配置PHP解析,最后重启服务并验证环境搭建成功。整个过程包括了命令行操作和配置文件修改,确保服务器能正常运行PHP应用。
摘要由CSDN通过智能技术生成

环境版本:

        Linux ContOS 7.9

        Nginx 1.2

        MySql 8.0

        PHP 8.0

一站式搭建

平台地址:https://oneinstack.com/auto/
注:运行完成重新加载配置文件,使命令全局生效

# 重新加载配置文件
source /etc/profile

使用yum搭建

关闭防火墙

# 关闭防火墙
systemctl stop firewalld

# 关闭开机自启动
systemctl disable firewalld

nginx安装

# 安装nginx所需依赖
yum install vim gcc gcc++ wget libxml2-devel wget -y

# 添加nginx软件源(可指定安装版本)
yum localinstall http://nginx.org/packages/centos/7/x86_64/RPMS/nginx-1.20.2-1.el7.ngx.x86_64.rpm

# 安装nginx
yum install nginx -y

# 启动nginx服务
systemctl start nginx

# 设置开机自启
systemctl enable nginx

mysql安装

# 添加mysql软件源(可指定安装版本)
yum localinstall http://dev.mysql.com/get/mysql80-community-release-el7-6.noarch.rpm

# 安装mysql
yum install mysql-community-server mysql-community-devel -y

# 没有问题无需执行,遇到获取 《GPG 密钥失败》 获取密钥后 重新安装mysql
rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022

# 启动mysql服务
systemctl start mysqld

# 设置mysql开机自启动
systemctl enable mysqld

# 获取root账户临时密码
grep 'password' /var/log/mysqld.log

# 通过临时密码 登录mysql
mysql -uroot -p
输入临时密码

# 设置root账户密码 注:密码必须含英文大小写、数字、_,否则报错密码不符合规范
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'Root123_';

# 使新密码生效
flush privileges;

# 新密码登录
mysql -uroot -pRoot123_

PHP 安装

# 获取最新版yum包
yum -y install https://mirrors.aliyun.com/remi/enterprise/remi-release-7.rpm
yum -y install yum-utils

# 切换PHP remi源 到PHP8
yum-config-manager --enable remi-php80

# 安装PHP
yum install -y php

# 安装PHP所需依赖
yum install  php-cli php-fpm php-mysqlnd php-zip php-devel php-gd php-mbstring php-curl php-xml php-pear php-bcmath php-json php-redis  --skip-broken

验证是否安装成功

# 查看Linux版本
cat /etc/os-release

# 查看Nginx版本
nginx -v

# 查看Mysql版本 注:大写V
mysql -V

# 查看PHP版本
php -v

在 Nginx 中配置 PHP

        1、取消php相关配置的注释

        2、更改root目录(root路径 就是 nginx 服务默认运行脚本目录,可更改

        2、更改 fastcgi_param 值

# 配置文件
vim /etc/nginx/conf.d/default.conf

# 修改内容
server {
    listen       80;
    server_name  localhost;

    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /usr/share/nginx/html;
        index  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;
    }

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

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    ### 更改内容
    location ~ \.php$ {
        root           /usr/share/nginx/html;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

重新启动 Nginx、PHP 服务

# 重启nginx服务
systemctl restart nginx.service

# 重启PHP服务
systemctl restart php-fpm.service

验证环境是否搭建成功

# 创建 PHP 脚本
vim /usr/share/nginx/html/index.php

# 输入内容 保存退出
<?php
    echo phpinfo();

在浏览器输入服务器地址:IP(192.168.1.1) 或者 域名(www.aaa.com)

浏览器输入服务器地址:IP(192.168.1.1/index.php) 或者 域名(www.aaa.com/index.php)

 LNMP环境搭建 成功!!!

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值