使用阿里云服务器手动部署LNMP环境(CentOS 7)

手动部署LNMP环境(CentOS 7)

Nginx是一款小巧而高效的Web服务器软件,可帮您在Linux系统下快速方便地搭建出LNMP Web服务环境。本教程介绍如何手动在ECS实例上搭建LNMP环境,其中LNMP分别代表Linux、Nginx、MySQL和PHP。

部署LNMP环境操作步骤演示

    • 手动部署LNMP环境(CentOS 7)
      • 前提准备及条件
      • 配置及软件版本说明
      • 部署操作步骤演示
        • 操作流程
        • 步骤一:准备编译环境
          • 1、远程连接实例(本实例使用FinalShell)
          • 2、关闭防火墙
          • 3、关闭SElinux
        • 步骤二:安装Nginx
        • 步骤三:安装MySQL
        • 步骤四:安装PHP
        • 步骤五:配置Nginx
        • 步骤六:配置MySQL
        • 步骤七:配置PHP
        • 步骤八:测试访问LNMP平台
        • 后续步骤

前提准备及条件

  • 已注册阿里云账号;
  • 已完成实名认证;
  • 已创建ECS实例并为实例分配公网IP地址;
  • 已在实例安全组的入方向添加安全组规则并放行80端口。
  • 若使用SSH远程连接Linux实例,需要放行22端口;

配置及软件版本说明

本篇在示例步骤中使用了以下配置的ECS实例,实例规格为ecs.c5.large。实际操作时,请以自己的实例配置为准。

  • CPU:2 vCPU
  • 内存:4GiB
  • 网络类型:专有网络VPC
  • IP地址:公网IP

使用的相关软件的版本:

  • 操作系统: 公共镜像CentOS 7.2 64位
  • Nginx版本:Nginx 1.16.1
  • MySQL版本:MySQL 5.7.28
  • PHP版本: 7.0.33

说明 当您使用不同软件版本时,可能需要根据实际情况调整命令和参数配置。

部署操作步骤演示

操作流程

使用云服务器ECS手动搭建LNMP平台的操作流程如下:

操作步骤概述

  • 步骤一:准备编译环境
  • 步骤二:安装Nginx
  • 步骤三:安装MySQL
  • 步骤四:安装PHP
  • 步骤五:配置Nginx
  • 步骤六:配置MySQL
  • 步骤七:配置PHP
  • 步骤八:测试访问LNMP平台

步骤一:准备编译环境

1、远程连接实例(本实例使用FinalShell)

使用阿里云服务器手动部署LNMP环境(CentOS 7)_第1张图片

2、关闭防火墙
  • 执行关闭防火墙命令,命令如下。如图所示。
    #systemctl stop firewalld //临时关闭防火墙
    #systemctl disable firewalld //永久关闭防火墙

    使用阿里云服务器手动部署LNMP环境(CentOS 7)_第2张图片
3、关闭SElinux
  • 执行vi /etc/selinux/config 命令进入SElinux配置文件下,将enforcing模式修改成disabled模式。即关闭SElinux。修改完成后,保存退出。
    使用阿里云服务器手动部署LNMP环境(CentOS 7)_第3张图片
  • 执行getenforce 命令进行查看(需要重启才能生效)
    在这里插入图片描述
    若临时修改,可以执行setenforce 0命令,将SElinux安装模式设置位“宽容模式”(即会有安全提示信息,但允许正常运行。)

步骤二:安装Nginx

  • 运行以下命令安装Nginx
# yum install -y nginx

在这里插入图片描述

  • 安装完成。如图所示。
    使用阿里云服务器手动部署LNMP环境(CentOS 7)_第4张图片
  • 运行以下命令nginx -v查看Nginx版本。返回结果如下所示,表示Nginx安装成功。
    在这里插入图片描述

步骤三:安装MySQL

  • 运行以下命令更新yum源
rpm -Uvh  http://dev.mysql.com/get/mysql57-community-release-el7-9.noarch.rpm

使用阿里云服务器手动部署LNMP环境(CentOS 7)_第5张图片

  • 运行以下命令安装MySQL

说明 如果您使用的操作系统内核版本为el8,可能会提示报错信息No match for argument。您需要先运行命令**yum module disable mysql**禁用默认的mysql模块,再安装MySQL。

yum -y install mysql-community-server

使用阿里云服务器手动部署LNMP环境(CentOS 7)_第6张图片

  • 运行以下命令 mysql -V查看MySQL版本号.返回结果如下所示,表示MySQL安装成功.
    在这里插入图片描述
  • 运行命令systemctl start mysqld启动MySQL。
  • 运行以下命令设置开机启动MySQL。
systemctl enable mysqld
systemctl daemon-reload

在这里插入图片描述

步骤四:安装PHP

  • 更新YUM源
    (1)运行以下命令添加epel源。
yum install \
https://repo.ius.io/ius-release-el7.rpm \
https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm

在这里插入图片描述
(2)输入y确定使用阿里云服务器手动部署LNMP环境(CentOS 7)_第7张图片
使用阿里云服务器手动部署LNMP环境(CentOS 7)_第8张图片
(3) 运行以下命令添加Webtatic源。

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

使用阿里云服务器手动部署LNMP环境(CentOS 7)_第9张图片

  • 运行以下命令安装PHP。

yum -y install php70w-devel php70w.x86_64 php70w-cli.x86_64 php70w-common.x86_64 php70w-gd.x86_64 php70w-ldap.x86_64 php70w-mbstring.x86_64 php70w-mcrypt.x86_64 php70w-pdo.x86_64 php70w-mysqlnd php70w-fpm php70w-opcache php70w-pecl-redis php70w-pecl-mongodb
使用阿里云服务器手动部署LNMP环境(CentOS 7)_第10张图片

  • 运行命令php -v查看PHP版本。返回结果如下所示,表示安装成功。
    使用阿里云服务器手动部署LNMP环境(CentOS 7)_第11张图片

步骤五:配置Nginx

  • 运行以下命令备份Nginx配置文件。
# cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak
  • 修改Nginx配置文件,添加Nginx对PHP的支持。

说明 若不添加此配置信息,后续您使用浏览器访问PHP页面时,页面将无法显示。

(1) 运行以下命令打开Nginx配置文件

vim /etc/nginx/nginx.conf

(2) 按i进入编辑模式
(3) 在server大括号内,添加下列配置信息。

#除下面提及的需要添加的配置信息外,其他配置保持默认值即可。
location / {
    #在location大括号内添加以下信息,配置网站被访问时的默认首页
    index index.php index.html index.htm;
}
#添加下列信息,配置Nginx通过fastcgi方式处理您的PHP请求
location ~ .php$ {
    root /usr/share/nginx/html;    #将/usr/share/nginx/html替换为您的网站根目录,本教程使用/usr/share/nginx/html作为网站根目录
    fastcgi_pass 127.0.0.1:9000;   #Nginx通过本机的9000端口将PHP请求转发给PHP-FPM进行处理
    fastcgi_index index.php;
    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    include fastcgi_params;   #Nginx调用fastcgi接口处理PHP请求
}   

添加如上代码。如下图所示。
使用阿里云服务器手动部署LNMP环境(CentOS 7)_第12张图片

  • 按下Esc键后,输入:wq并回车以保存并关闭配置文件。
  • 运行以下命令启动Nginx服务。
systemctl start nginx 

使用阿里云服务器手动部署LNMP环境(CentOS 7)_第13张图片

  • 运行以下命令设置Nginx服务开机自启动。
systemctl enable nginx

在这里插入图片描述

步骤六:配置MySQL

  • 运行以下命令查看/var/log/mysqld.log文件,获取并记录root用户的初始密码。
# grep 'temporary password' /var/log/mysqld.log

返回结果如下:
在这里插入图片描述

说明 下一步重置root用户密码时,会使用该初始密码。

  • 运行以下命令配置MySQL的安全性。
[root@iZsxkfb9igxuewZ ~]# mysql_secure_installation //重置root账号密码。

Securing the MySQL server deployment.

Enter password for user root:  #输入上一步获取的root用户初始密码

The existing password for the user account root has expired. Please set a new password.

New password:  #输入新密码,长度为8至30个字符,必须同时包含大小写英文字母、数字和特殊符号。特殊符号可以是()` ~!@#$%^&*-+=|{}[]:;‘<>,.?/

Re-enter new password: #再次输入新密码
The 'validate_password' plugin is installed on the server.
The subsequent steps will run with the existing configuration
of the plugin.
Using existing password for root.

Estimated strength of the password: 100 
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : Y
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.

Remove anonymous users? (Press y|Y for Yes, any other key for No) : Y  #是否删除匿名用户,输入Y
Success.


Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.

Disallow root login remotely? (Press y|Y for Yes, any other key for No) : Y #禁止root远程登录,输入Y
Success.

By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.


Remove test database and access to it? (Press y|Y for Yes, any other key for No) : Y #是否删除test库和对它的访问权限,输入Y
 - Dropping test database...
Success.

 - Removing privileges on test database...
Success.

Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.

Reload privilege tables now? (Press y|Y for Yes, any other key for No) : Y #是否重新加载授权表,输入Y
Success.

All done! 

步骤七:配置PHP

  • 新建phpinfo.php文件,用于展示PHP信息。
    (1)运行以下命令新建文件。

vim <网站根目录>/phpinfo.php #将<网站根目录>替换为您配置的网站根目录。
网站根目录是在nginx.conf文件中location ~ .php$大括号内配置的root值,如下图所示。在这里插入图片描述

本配置的网站根目录为/usr/share/nginx/html,因此命令为:

vim /usr/share/nginx/html/phpinfo.php

(2)按i进入编辑模式。
(3)输入下列内容,函数phpinfo()会展示PHP的所有配置信息。

<?php echo phpinfo(); ?>

(4)按Esc键后,输入:wq并回车以保存并关闭配置文件。
在这里插入图片描述

  • 运行以下命令启动PHP-FPM。
systemctl start php-fpm
  • 运行以下命令设置PHP-FPM开机自启动。
systemctl enable php-fpm

在这里插入图片描述

步骤八:测试访问LNMP平台

  • 打开浏览器。
  • 在地址栏输入
    http:///phpinfo.php
    返回结果如下图所示,表示LNMP环境部署成功。
    使用阿里云服务器手动部署LNMP环境(CentOS 7)_第14张图片

后续步骤

测试访问LNMP平台成功后,建议运行以下命令将phpinfo.php文件删除,消除安全隐患。

rm -rf <网站根目录>/phpinfo.php   #将<网站根目录>替换为您在nginx.conf中配置的网站根目录

本配置的网站根目录为/usr/share/nginx/html,因此命令为:

rm -rf /usr/share/nginx/html/phpinfo.php

在这里插入图片描述

你可能感兴趣的:(阿里云)