ubuntu

Ubuntu下LNMP配置有哪些技巧

小樊
48
2025-06-23 04:17:51
栏目: 智能运维

在Ubuntu下配置LNMP(Linux, Nginx, MySQL/MariaDB, PHP)环境是一个相对简单的过程,但有一些技巧可以帮助你更高效地完成配置。以下是一些建议和步骤:

安装前的准备工作

  1. 更新系统

    • 确保系统是最新的:
      sudo apt update
      sudo apt upgrade -y
      
  2. 选择合适的版本

    • 根据需要选择合适的Linux发行版版本,如Ubuntu 20.04 LTS。

安装步骤

  1. 安装Nginx

    • 添加Nginx仓库(如果需要):
      sudo add-apt-repository ppa:nginx/nginx
      sudo apt update
      
    • 安装Nginx:
      sudo apt install nginx -y
      
    • 启动Nginx并设置开机自启动:
      sudo systemctl start nginx
      sudo systemctl enable nginx
      
  2. 安装MySQL/MariaDB

    • 安装MySQL或MariaDB(这里以MariaDB为例):
      sudo apt install mariadb-server -y
      
    • 启动MariaDB并设置开机自启动:
      sudo systemctl start mariadb
      sudo systemctl enable mariadb
      
    • 运行安全脚本以提高安全性:
      sudo mysql_secure_installation
      
  3. 安装PHP及其扩展

    • 安装PHP及其相关扩展:
      sudo apt install php-fpm php-mysql -y
      
    • 启动PHP-FPM并设置开机自启动:
      sudo systemctl start php7.4-fpm
      sudo systemctl enable php7.4-fpm
      
  4. 配置Nginx支持PHP

    • 编辑Nginx的默认站点配置文件:
      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;
      }
      
    • 保存并退出编辑器,然后测试Nginx配置:
      sudo nginx -t
      
    • 如果没有错误,重新加载Nginx以应用更改:
      sudo systemctl reload nginx
      

配置优化技巧

  1. 使用国内源

    • 安装前断网,使用国内源以加快安装速度:
      sudo apt update
      sudo apt install nginx mysql-server php-fpm php-mysql -y --allow-unauthenticated
      
  2. 启用Gzip压缩

    • 在Nginx配置中启用Gzip压缩以减少传输数据量:
      http {
          gzip on;
          gzip_vary on;
          gzip_proxied any;
          gzip_comp_level 6;
          gzip_buffers 16 8k;
          gzip_http_version 1.1;
          gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
      }
      
  3. 配置PHP-FPM

    • 编辑PHP-FPM配置文件以确保它使用正确的用户和组:
      sudo nano /etc/php/7.4/fpm/pool.d/www.conf
      
    • 修改usergroup行为www-data
      user = www-data
      group = www-data
      
    • 重启PHP-FPM服务:
      sudo systemctl restart php7.4-fpm
      
  4. 使用防火墙

    • 确保防火墙允许HTTP和HTTPS流量:
      sudo ufw allow 'Nginx Full'
      
  5. 配置虚拟主机(可选):

    • 创建并配置虚拟主机:
      sudo nano /etc/nginx/sites-available/your_domain.com
      
    • 添加以下内容,根据你的需求进行修改:
      server {
          listen 80;
          server_name your_domain.com www.your_domain.com;
          root /var/www/your_domain.com;
          index index.php index.html index.htm;
          location / {
              try_files $uri $uri/ /index.php?$query_string;
          }
          location ~ \.php$ {
              include snippets/fastcgi-php.conf;
              fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
              fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
              include fastcgi_params;
          }
      }
      
    • 创建符号链接以启用新的服务器块配置:
      sudo ln -s /etc/nginx/sites-available/your_domain.com /etc/nginx/sites-enabled/
      
    • 测试Nginx配置是否正确:
      sudo nginx -t
      
    • 重新加载Nginx以应用更改:
      sudo systemctl reload nginx
      

测试配置

  1. 测试Nginx

    • 在浏览器中访问你的服务器IP地址,如果看到Nginx的欢迎页面,则说明Nginx安装成功。
  2. 测试PHP

    • 在网站根目录下创建一个名为info.php的文件,内容如下:
      <?php
      phpinfo();
      ?>
      
    • 访问http://your_server_ip/info.php,如果看到PHP的信息页面,则说明PHP环境配置成功。

通过以上步骤和技巧,你可以在Ubuntu上成功配置一个高性能的LNMP环境。希望这些信息对你有所帮助。

0
看了该问题的人还看了