阿里云服务器安装LNMP指南

LNMP(Linux, Nginx, MySQL, PHP)是一个常见的Web服务器环境。本文将详细介绍如何在阿里云服务器上安装和配置LNMP环境。

环境准备

在开始安装之前,需要确保你的阿里云服务器已经配置好并可以访问网络。建议使用EPEL仓库来获取最新的软件包。

  1. 更新系统:
Terminal window
1
sudo yum update -y

安装Nginx

  1. 安装Nginx:
Terminal window
1
sudo yum install nginx -y
  1. 启动并设置Nginx开机自启:
Terminal window
1
sudo systemctl start nginx
2
sudo systemctl enable nginx
  1. 验证Nginx安装:

打开浏览器,输入服务器IP地址,看到Nginx欢迎页面表示安装成功。

安装MySQL

  1. 安装MySQL依赖包:
Terminal window
1
sudo yum install -y compat-openssl10
  1. 更新YUM源:
Terminal window
1
sudo rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el7-11.noarch.rpm
  1. 下载软件包索引信息:
Terminal window
1
sudo yum makecache
  1. 安装MySQL
Terminal window
1
sudo yum -y install mysql-community-server
  1. 查看MySQL版本号
Terminal window
1
mysql -V
  1. 启动并设置MySQL开机自启:
Terminal window
1
sudo systemctl start mysqld
2
sudo systemctl enable mysqld
3
sudo systemctl daemon-reload
  1. 获取临时密码并登录:
Terminal window
1
sudo grep 'temporary password' /var/log/mysqld.log
  1. 配置MySQL的安全性:
Terminal window
1
sudo mysql_secure_installation
  • Enter password for user root: #输入上一步获取的root用户初始密码
  • New password: #输入新密码。长度为8至30个字符,必须同时包含大小写英文字母、数字和特殊符号
  • Re-enter new password: #确认新密码。
  • Change the password for root ? (Press y|Y for Yes, any other key for No) : Y #确认使用新密码。
  • New password: #再次输入新密码。
  • Re-enter new password: #再次确认新密码。
  • Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : Y #再次确认使用新密码。
  • Remove anonymous users? (Press y|Y for Yes, any other key for No) : Y #删除匿名用户
  • Disallow root login remotely? (Press y|Y for Yes, any other key for No) : Y #禁止使用root远程登录MYSQL
  • Remove test database and access to it? (Press y|Y for Yes, any other key for No) : Y #删除test库以及用户对test库的访问权限
  • Reload privilege tables now? (Press y|Y for Yes, any other key for No) : Y #重新加载授权表

安装PHP

  1. 更新YUM源:
Terminal window
1
sudo rpm -Uvh https://mirrors.aliyun.com/remi/enterprise/remi-release-7.rpm
  1. 启用PHP 8.0仓库
Terminal window
1
sudo yum-config-manager --enable remi-php80
  1. 安装PHP
Terminal window
1
sudo yum install -y php php-cli php-fpm php-common php-mysqlnd php-gd php-mbstring
  1. 启动并设置PHP-FPM开机自启:
Terminal window
1
sudo systemctl start php-fpm
2
sudo systemctl enable php-fpm

配置LNMP

  1. 配置Nginx以支持PHP:

编辑Nginx配置文件 /etc/nginx/conf.d/default.conf,添加以下内容:

1
server {
2
listen 80;
3
server_name your_domain_or_IP;
4
root /usr/share/nginx/html;
5
6
location / {
7
index index.php index.html index.htm;
8
}
9
10
location ~ \.php$ {
11
try_files $uri =404;
12
fastcgi_pass 127.0.0.1:9000;
13
fastcgi_index index.php;
14
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
15
include fastcgi_params;
16
}
17
}
  1. 测试并重启Nginx:
Terminal window
1
sudo nginx -t
2
sudo systemctl restart nginx
  1. 创建测试PHP文件:

在Nginx的根目录中创建一个info.php文件:

1
<?php
2
phpinfo();
3
?>
  1. 验证PHP配置:

在浏览器中访问 http://your_domain_or_IP/info.php,查看PHP信息页面。

总结

通过上述步骤,我们可以成功在阿里云服务器上安装和配置了LNMP环境。

美团外卖红包 饿了么红包 支付宝红包