温馨提示×

linux怎么搭建LNMP环境

小亿
120
2023-10-10 05:26:33
栏目: 智能运维

搭建LNMP环境是在Linux系统上部署Nginx、MySQL和PHP的组合,下面是搭建LNMP环境的步骤:

  1. 安装Nginx:
  • 使用包管理器安装Nginx,如在Ubuntu上运行命令 sudo apt-get install nginx,在CentOS上运行命令 sudo yum install nginx

  • 安装完成后,启动Nginx服务并设置开机自启动:sudo systemctl start nginxsudo systemctl enable nginx

  1. 安装MySQL:
  • 使用包管理器安装MySQL,如在Ubuntu上运行命令 sudo apt-get install mysql-server,在CentOS上运行命令 sudo yum install mysql-server

  • 安装过程中会提示设置MySQL的root密码,按照提示进行设置。

  • 安装完成后,启动MySQL服务并设置开机自启动:sudo systemctl start mysqlsudo systemctl enable mysql

  1. 安装PHP:
  • 使用包管理器安装PHP及相关扩展,如在Ubuntu上运行命令 sudo apt-get install php-fpm php-mysql,在CentOS上运行命令 sudo yum install php-fpm php-mysql

  • 安装完成后,编辑PHP配置文件 /etc/php/7.x/fpm/php.ini,将其中的 cgi.fix_pathinfo 设置为 0,保存文件。

  • 启动PHP-FPM服务并设置开机自启动:sudo systemctl start php-fpmsudo systemctl enable php-fpm

  1. 配置Nginx:
  • 编辑Nginx配置文件 /etc/nginx/nginx.conf,将其中的 server 部分替换为以下内容:
server {
listen 80;
server_name your_domain;
root /your/website/path;
index index.php index.html;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.x-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
  • your_domain 替换为你的域名或IP地址,将 /your/website/path 替换为你的网站根目录路径。

  • 保存文件后,重启Nginx服务:sudo systemctl restart nginx

  1. 测试LNMP环境:
  • 在网站根目录下创建一个简单的PHP文件 index.php,内容为 <?php phpinfo(); ?>

  • 在浏览器中访问 http://your_domain/index.php,如果能正常显示PHP信息页面,则表示LNMP环境搭建成功。

以上是一个简单的LNMP环境搭建过程,根据具体情况可能会有一些差异。可以根据自己的需求进行相应的配置和调整。

0