阿里云部署LNMP环境

1.准备工作

实例已分配公网IP地址或绑定弹性公网IP(EIP)。
操作系统必须为CentOS 7.x。
实例安全组的入方向规则已放行22、80、443端口。


2.环境说明

Nginx版本:Nginx 1.20.1
MySQL版本:MySQL 5.7.42
PHP版本:PHP 7.0.33


3.搭建环境

步骤1:关闭防火墙和SELinux

# 1.查看当前防火墙的状态
# 如果防火墙的状态参数是inactive,则防火墙为关闭状态
# 如果防火墙的状态参数是active,则防火墙为开启状态
systemctl status firewalld

# 2.临时关闭防火墙
sudo systemctl stop firewalld
systemctl status firewalld

# 3.查看SELinux的当前状态
# 如果SELinux状态参数是Disabled,则SELinux为关闭状态
# 如果SELinux状态参数是Enforcing,则SELinux为开启状态
getenforce # 4.关闭SELinux
setenforce 0


步骤2:安装Nginx

# 1.安装Nginx
sudo yum -y install nginx

# 2.查看Nginx版本
nginx -v

返回结果类似如下所示,表示Nginx安装成功。
nginx version: nginx/1.20.1


步骤3:安装并配置MySQL

(1) 安装MySQL

# 1.更新YUM源
sudo rpm -Uvh http://dev.mysql.com/get/mysql57-community-release-el7-9.noarch.rpm

# 2.安装MySQL
sudo yum -y install mysql-community-server --nogpgcheck

# 3.查看MySQL版本号
mysql -V

返回结果如下所示,表示MySQL安装成功。
mysql Ver 14.14 Distrib 5.7.42, for Linux (x86_64) using EditLine wrapper

# 4.启动MySQL
sudo systemctl start mysqld

# 5.设置开机启动MySQL
sudo systemctl enable mysqld
sudo systemctl daemon-reload


(2) 配置MySQL

# 1.获取并记录root用户的初始密码
sudo grep 'temporary password' /var/log/mysqld.log

命令行返回结果如下
2021-11-10T07:01:26.595215Z 1 [Note] A temporary password is generated for root@localhost: ARQTRy3+****

# 2.配置MySQL的安全性
sudo mysql_secure_installation

# 3.输入MySQL的初始密码
Securing the MySQL server deployment.
Enter password for user root: #输入上一步获取的root用户初始密码

# 4.设置MySQL的新密码
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 #返回结果包含您设置的密码强度。
Change the password for root ? (Press y|Y for Yes, any other key for No) :Y #您需要输入Y以确认使用新密码。

# 5.新密码设置完成后,需要再次验证新密码。
New password:#再次输入新密码。

Re-enter new password:#再次确认新密码。

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 #您需要输入Y,再次确认使用新密码。

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

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

# 8.输入Y删除test库以及用户对test库的访问权限。
Remove test database and access to it? (Press y|Y for Yes, any other key for No) :Y
- Dropping test database...
Success.

- Removing privileges on test database...
Success.

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

All done!


步骤4:安装并配置PHP

(1) 安装PHP

# 1.添加EPEL源
sudo yum install \
https://repo.ius.io/ius-release-el7.rpm \
https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm

# 2.添加Webtatic源
sudo rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm

# 3.安装PHP
sudo 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

# 4.查看PHP版本
php -v

返回结果如下所示,表示安装成功。
PHP 7.0.33 (cli) (built: Dec 6 2018 22:30:44) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2017 Zend Technologies
with Zend OPcache v7.0.33, Copyright (c) 1999-2017, by Zend Technologies


(2) 配置PHP

# 1.备份Nginx配置文件
sudo cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak

# 2.修改Nginx配置文件,添加Nginx对PHP的支持
# 打开Nginx配置文件,按i进入编辑模式
sudo vim /etc/nginx/nginx.conf

# 3.在server大括号内,添加或修改location /配置信息
location / {
index index.php index.html index.htm;
}

# 4.在server大括号内,添加或修改location ~ .php$配置信息。
# 添加下列信息,配置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请求。
}

# 5.按Esc键,输入:wq,按Enter键关闭并保存配置文件

# 6.启动Nginx服务
sudo systemctl start nginx

# 7.设置Nginx服务开机自启动
sudo systemctl enable nginx

修改后的内容:即在配置文件的server添加如下的配置内容



步骤5:测试环境

(1) 创建测试文件:

# 1.本文配置的网站根目录为/usr/share/nginx/html,因此需要运行以下命令新建phpinfo.php文件
sudo vim /usr/share/nginx/html/phpinfo.php

# 2.按i进入编辑模式

# 3.输入下列内容,函数phpinfo()会展示PHP的所有配置信息
<?php echo phpinfo(); ?>

# 4.按Esc键后,输入:wq并回车,保存关闭配置文件

# 5.启动PHP-FPM
sudo systemctl start php-fpm

# 6.设置PHP-FPM开机自启动
sudo systemctl enable php-fpm


(2) 访问测试文件

在浏览器的地址栏输入http://<ECS实例公网IP地址>/phpinfo.php进行访问,操作结果如下:



相关推荐

  • 获取指定目录下的所有图片信息

    1 获取指定目录下的所有图片信息// 获取指定目录下的所有图片信息 public function getImagesInfo($directory) { $images = []; // 创建递归目录迭代器 $iterator = new \RecursiveIteratorIterator( new \RecursiveDirectoryIterator($directory, \RecursiveDirectoryIterator::SKIP_DOTS), \RecursiveIteratorIterator::LEAVES_ONLY ); // 遍历目录中的每个文件 foreach (

  • Thinkphp各版本的PHP要求

    ThinkPHP 8.0:运行环境要求PHP8.0+,兼容PHP8.3ThinkPHP 6.1:运行环境要求PHP7.2+,兼容PHP8.1ThinkPHP 6.0:运行环境要求PHP7.2+,兼容PHP8.1ThinkPHP 5.1:运行环境要求PHP5.6+,兼容PHP8.0ThinkPHP 5.0:运行环境要求PHP5.4+,兼容PHP7.3

  • Thinkphp5.1路径常量

    1 配置文件位置根目录/config/template.php2 配置文件内容&lt;?php// +----------------------------------------------------------------------// | ThinkPHP [ WE CAN DO IT JUST THINK ]// +----------------------------------------------------------------------// | Copyright (c) 2006~2018 http://thinkphp.cn All rights reser

  • Thinkphp5.0路径常量

    1 配置文件位置根目录/application/模块名/config.php2 配置文件内容&lt;?php//配置文件return [ // 后台视图输出字符串内容替换 'view_replace_str' =&gt; [ '__PUBLIC__' =&gt; '/', '__STATIC__' =&gt; '/static', '__CONSOLE__' =&gt; '/static/console', '__CONSOLE_CSS__' =&gt; '/static/console/css', '__CONSOLE_IMAGES__' =&gt; '/static/console/ima

  • wp站点防止别人进行DDOS攻击

    1 简介wp站点防止别人进行DDOS攻击。2 配置位置位置:根目录/wp-config.php3 配置内容在【根目录/wp-config.php】文件的开头添加如下代码:if(strpos($_SERVER['REQUEST_URI'], 'xmlrpc.php') !== false){ $protocol = $_SERVER['SERVER_PROTOCOL'] ?? ''; if(!in_array($protocol, ['HTTP/1.1', 'HTTP/2', 'HTTP/2.0', 'HTTP/3'], true)){ $protocol = 'HTTP/1.0'; } hea

  • 只读属性

    1 只读属性简介只读属性的声明方式类似于普通属性,但需要使用 readonly 关键字。2 只读属性例子class Point { public readonly float $x; public readonly float $y; public function __construct(float $x, float $y) { $this-&gt;x = $x; $this-&gt;y = $y; }}$point = new Point(3.5, 2.8);echo $point-&gt;x; // 输出: 3.5echo $point-&gt;y; // 输出: 2.8// 下面的尝