阿里云服务器安装LNMP指南
- 699字
- 3分钟
- 2024-06-13
LNMP(Linux, Nginx, MySQL, PHP)是一个常见的Web服务器环境。本文将详细介绍如何在阿里云服务器上安装和配置LNMP环境。
环境准备
在开始安装之前,需要确保你的阿里云服务器已经配置好并可以访问网络。建议使用EPEL仓库来获取最新的软件包。
- 更新系统:
1sudo yum update -y
安装Nginx
- 安装Nginx:
1sudo yum install nginx -y
- 启动并设置Nginx开机自启:
1sudo systemctl start nginx2sudo systemctl enable nginx
- 验证Nginx安装:
打开浏览器,输入服务器IP地址,看到Nginx欢迎页面表示安装成功。
安装MySQL
- 安装MySQL依赖包:
1sudo yum install -y compat-openssl10
- 更新YUM源:
1sudo rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el7-11.noarch.rpm
- 下载软件包索引信息:
1sudo yum makecache
- 安装MySQL
1sudo yum -y install mysql-community-server
- 查看MySQL版本号
1mysql -V
- 启动并设置MySQL开机自启:
1sudo systemctl start mysqld2sudo systemctl enable mysqld3sudo systemctl daemon-reload
- 获取临时密码并登录:
1sudo grep 'temporary password' /var/log/mysqld.log
- 配置MySQL的安全性:
1sudo 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
- 更新YUM源:
1sudo rpm -Uvh https://mirrors.aliyun.com/remi/enterprise/remi-release-7.rpm
- 启用PHP 8.0仓库
1sudo yum-config-manager --enable remi-php80
- 安装PHP
1sudo yum install -y php php-cli php-fpm php-common php-mysqlnd php-gd php-mbstring
- 启动并设置PHP-FPM开机自启:
1sudo systemctl start php-fpm2sudo systemctl enable php-fpm
配置LNMP
- 配置Nginx以支持PHP:
编辑Nginx配置文件 /etc/nginx/conf.d/default.conf
,添加以下内容:
1server {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}
- 测试并重启Nginx:
1sudo nginx -t2sudo systemctl restart nginx
- 创建测试PHP文件:
在Nginx的根目录中创建一个info.php
文件:
1<?php2phpinfo();3?>
- 验证PHP配置:
在浏览器中访问 http://your_domain_or_IP/info.php
,查看PHP信息页面。
总结
通过上述步骤,我们可以成功在阿里云服务器上安装和配置了LNMP环境。


