利用Ubuntu LNMP搭建网站的步骤如下:
首先,确保你的Ubuntu系统是最新的。打开终端并运行以下命令:
sudo apt update
sudo apt upgrade -y
安装Nginx服务器:
sudo apt install nginx
安装完成后,启动Nginx服务并设置开机自启动:
sudo systemctl start nginx
sudo systemctl enable nginx
安装MySQL数据库服务器:
sudo apt install mysql-server
在安装过程中,系统会提示你设置MySQL的root用户密码,设置好密码后,启动MySQL服务:
sudo systemctl start mysql
sudo systemctl enable mysql
安装PHP以及相关的扩展:
sudo apt install php-fpm php-mysql
编辑Nginx的默认配置文件以支持PHP。使用你喜欢的文本编辑器打开配置文件:
sudo nano /etc/nginx/sites-available/default
在 server
块中添加以下内容:
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
}
这里的 /var/run/php/php7.4-fpm.sock
路径可能会因PHP版本不同而有所变化,请根据实际情况进行调整。保存并关闭文件后,重新加载Nginx配置:
sudo systemctl reload nginx
在 /var/www/html
目录下创建一个名为 info.php
的文件,内容如下:
<?php
phpinfo();
?>
保存并关闭文件。然后在浏览器中访问服务器的IP地址或域名,加上 /info.php
路径,例如:http://your_server_ip/info.php
。如果能够正常显示PHP信息,则说明LNMP环境安装成功。
至此,你已经成功在Ubuntu系统上安装了LNMP环境,并进行了基本的安全设置。现在你可以开始部署各种基于LNMP的网站和应用了。