以下内容将详细介绍如何在Linux系统中搭建LNMP(Linux、Nginx、MySQL和PHP)环境,为部署Zabbix做好准备。
1. 安装Nginx
sudo apt-get update
sudo apt-get install nginx
2. 安装MySQL
sudo apt-get install mysql-server mysql-client
配置MySQL,请参考官方文档或相关教程进行安全设置与初始化。
3. 安装PHP及其扩展
sudo apt-get install php-fpm php-gd php-mbstring php-curl php-xml php-mysql php-zip php-json
4. 查找Nginx配置文件路径
运行以下命令定位Nginx的主配置文件位置:
ps -ef | grep nginx
/usr/sbin/nginx -t
示例输出如下:
root@HOSTNAME:~# /usr/sbin/nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
5. 配置Nginx支持PHP
编辑默认配置文件 `/etc/nginx/sites-enabled/default`,确保以下内容已正确添加到 `location ~ \.php$` 段落中:
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
6. 调整PHP配置
编辑 `/etc/php/7.0/fpm/php.ini` 文件,调整以下参数以优化性能:
max_execution_time = 600
max_input_time = 600
memory_limit = 256M
post_max_size = 32M
upload_max_filesize = 16M
date.timezone = Asia/Shanghai
7. 常见问题及解决方法
7.1 PHP无法解析
原因:未启用PHP-FPM服务。
解决:检查Nginx配置文件是否正确包含PHP-FPM的相关配置,并取消注释相关行。
7.2 Nginx返回403错误
原因:Nginx未允许访问PHP脚本。
解决:在Nginx配置文件中的 `index` 行添加 `index.php`。
8. 默认配置文件参考
8.1 Nginx主配置文件
以下是 `/etc/nginx/nginx.conf` 的部分内容:
user www-data;
worker_processes auto;
pid /run/nginx.pid;
events {
worker_connections 768;
}
http {
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
gzip on;
gzip_disable "msie6";
include /etc/nginx/mime.types;
default_type application/octet-stream;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
8.2 默认站点配置
以下是 `/etc/nginx/sites-enabled/default` 的部分内容:
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
index index.html index.htm index.nginx-debian.html index.php;
server_name _;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
通过以上步骤,您可以成功搭建LNMP环境并为Zabbix的安装做好准备。