在Ubuntu下配置LNMP(Linux, Nginx, MySQL/MariaDB, PHP)环境是一个相对简单的过程,但有一些技巧可以帮助你更高效地完成配置。以下是一些建议和步骤:
更新系统:
sudo apt update
sudo apt upgrade -y
选择合适的版本:
安装Nginx:
sudo add-apt-repository ppa:nginx/nginx
sudo apt update
sudo apt install nginx -y
sudo systemctl start nginx
sudo systemctl enable nginx
安装MySQL/MariaDB:
sudo apt install mariadb-server -y
sudo systemctl start mariadb
sudo systemctl enable mariadb
sudo mysql_secure_installation
安装PHP及其扩展:
sudo apt install php-fpm php-mysql -y
sudo systemctl start php7.4-fpm
sudo systemctl enable php7.4-fpm
配置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;
}
sudo nginx -t
sudo systemctl reload nginx
使用国内源:
sudo apt update
sudo apt install nginx mysql-server php-fpm php-mysql -y --allow-unauthenticated
启用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;
}
配置PHP-FPM:
sudo nano /etc/php/7.4/fpm/pool.d/www.conf
user和group行为www-data:user = www-data
group = www-data
sudo systemctl restart php7.4-fpm
使用防火墙:
sudo ufw allow 'Nginx Full'
配置虚拟主机(可选):
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/
sudo nginx -t
sudo systemctl reload nginx
测试Nginx:
测试PHP:
info.php的文件,内容如下:<?php
phpinfo();
?>
http://your_server_ip/info.php,如果看到PHP的信息页面,则说明PHP环境配置成功。通过以上步骤和技巧,你可以在Ubuntu上成功配置一个高性能的LNMP环境。希望这些信息对你有所帮助。