云服务器
产品概述
购买指南
快速入门
操作指南
技术教程
Linux如何安装配置Redis
Linux如何搭建LNMP环境
Linux如何查看端口监听情况
Linux实例如何配置中文支持
云服务器常用端口作用
如何在CentOS中安装Docker
如何在Linux中使用正则表达式
如何安装宝塔Linux面板
如何安装配置Nginx
如何搭建Discuz!论坛
如何搭建WordPress站点
Linux如何搭建FTP服务
Traceroute网络诊断工具详解
iperf 网络性能测试工具原理详解
iperf 命令使用详解
Linux中pstack命令使用指南
Linux系统中重启命令有哪些?
DDoS攻击应该如何防御?
如何解决Linux云服务器提示存储空间不足
灵活运用top命令监控Linux系统
云服务器
Linux如何搭建LNMP环境

Linux如何搭建LNMP环境

非凡云为大家分享Linux何如搭建LNMP环境(Linux、Nginx、MariaDB、PHP),示例为CentOS7

一、安装Nginx

1.创建nginx.repo文件,用于配置nginx的YUM仓库源

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

并插入以下内容:

[nginx]

name = nginx repo

baseurl = https://nginx.org/packages/mainline/centos/7/$basearch/

gpgcheck = 0

enabled = 1


2.安装nginx

yum install -y nginx


3.编辑default.conf文件,它包含了Nginx服务器的基本配置指令,用于定义Nginx如何处理进入服务器的请求。将所有内容替换为如下内容,用于取消对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;

}

}


4.启动Nginx

systemctl start nginx

systemctl enable nginx


5.打开浏览器,输入您的云服务器公网IP地址查看nginx服务是否正常运行


二、安装数据库

1.检查系统中是否已安装Mariadb,示例为系统中已安装了Mariadb

rpm -qa | grep -i mariadb


2.移除已安装的Mariadb

yum -y remove mariadb-libs-5.5.60-1.el7_5.x86_64


3.创建Mariadb.repo文件,用于配置MariaDB的YUM仓库源

vi /etc/yum.repos.d/MariaDB.repo


4.插入以下内容,添加MariaDB软件库

# MariaDB 10.4 CentOS repository list - created 2019-11-05 11:56 UTC

# http://downloads.mariadb.org/mariadb/repositories/

[mariadb]

name = MariaDB

baseurl = https://mirrors.cloud.tencent.com/mariadb/yum/10.4/centos7-amd64

gpgkey=https://mirrors.cloud.tencent.com/mariadb/yum/RPM-GPG-KEY-MariaDB

gpgcheck=1


5.安装MariaDB

yum -y install MariaDB-client MariaDB-server

此步骤若遇到下图情况时,可能是YUM的缓存问题,可用命令yum clean all来清理缓存


6.启动Mariadb服务

systemctl start mariadb

systemctl enable mariadb


7.验证Mariadb安装是否成功,提示如下信息则证明安装成功

mysql


三、安装PHP

1.更新yum中的PHP软件源

yum install -y epel-release

rpm -Uvh https://mirrors.tencent.com/remi/enterprise/remi-release-7.rpm


2.启动PHP8.0仓库

yum-config-manager --enable remi-php80

若出现以下情况则表示您的系统中没有安装yum-utils软件包,则须先使用命令yum install yum-utils来安装yum-utils软件包


3.安装PHP8.0所需的包

yum install -y php php-cli php-fpm php-mysqlnd php-zip php-devel php-gd php-mcrypt php-mbstring php-curl php-xml php-pear php-bcmath php-json


4.启动PHP-FPM服务

systemctl start php-fpm

systemctl enable php-fpm


四、验证环境

1.创建index.php文件用于测试

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


2.重启Nginx服务

systemctl restart nginx


3.打开浏览器,输入您的云服务器公网IP地址,出现图示页面代表安装配置成功

http://您的云服务器公网IP地址/index.php