插件窝 干货文章 如何配置LNMP服务器

如何配置LNMP服务器

PHP Nginx systemctl install 563    来源:    2025-05-05

LNMP服务器配置指南

LNMP代表Linux、Nginx、MySQL和PHP,是一种常见的Web服务器架构。以下是配置LNMP服务器的详细步骤:

1. 系统准备

操作系统选择

推荐使用CentOS 7/8或Ubuntu 18.04/20.04 LTS

更新系统

# CentOS
yum update -y

# Ubuntu
apt update && apt upgrade -y

2. 安装Nginx

CentOS

yum install epel-release -y
yum install nginx -y
systemctl start nginx
systemctl enable nginx

Ubuntu

apt install nginx -y
systemctl start nginx
systemctl enable nginx

验证安装

访问服务器IP地址,应看到Nginx欢迎页面

3. 安装MySQL/MariaDB

CentOS

yum install mariadb-server mariadb -y
systemctl start mariadb
systemctl enable mariadb
mysql_secure_installation

Ubuntu

apt install mysql-server -y
systemctl start mysql
systemctl enable mysql
mysql_secure_installation

4. 安装PHP

CentOS 7

yum install -y http://rpms.remirepo.net/enterprise/remi-release-7.rpm
yum install -y yum-utils
yum-config-manager --enable remi-php74
yum install -y php php-fpm php-mysql php-gd php-mbstring php-xml php-curl

Ubuntu

apt install php-fpm php-mysql php-gd php-mbstring php-xml php-curl -y

配置PHP-FPM

编辑/etc/php-fpm.d/www.conf(CentOS)或/etc/php/7.x/fpm/pool.d/www.conf(Ubuntu):

user = nginx
group = nginx
listen = /run/php-fpm/php-fpm.sock
listen.owner = nginx
listen.group = nginx

重启PHP-FPM:

systemctl restart php-fpm

5. 配置Nginx支持PHP

编辑Nginx站点配置文件(通常在/etc/nginx/conf.d/default.conf/etc/nginx/sites-available/default):

server {
    listen       80;
    server_name  your_domain.com;
    root         /var/www/html;
    index        index.php index.html index.htm;

    location / {
        try_files $uri $uri/ =404;
    }

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_pass unix:/run/php-fpm/php-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

测试并重载Nginx配置:

nginx -t
systemctl reload nginx

6. 测试PHP

创建测试文件:

echo "<?php phpinfo(); ?>" > /var/www/html/info.php

访问http://your_server_ip/info.php,应看到PHP信息页面。

7. 安全配置

防火墙设置

# CentOS
firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=https
firewall-cmd --reload

# Ubuntu
ufw allow 'Nginx Full'

禁用危险的PHP函数

编辑php.ini文件:

disable_functions = exec,passthru,shell_exec,system,proc_open,popen,curl_exec,curl_multi_exec,parse_ini_file,show_source

定期更新

设置自动安全更新:

# CentOS
yum install yum-cron -y
systemctl enable yum-cron
systemctl start yum-cron

# Ubuntu
apt install unattended-upgrades -y
dpkg-reconfigure -plow unattended-upgrades

8. 可选组件

安装phpMyAdmin

# CentOS
yum install phpmyadmin -y

# Ubuntu
apt install phpmyadmin -y

安装Redis

# CentOS
yum install redis -y
systemctl start redis
systemctl enable redis

# Ubuntu
apt install redis-server -y

9. 性能优化

Nginx优化

编辑/etc/nginx/nginx.conf

worker_processes auto;
worker_connections 1024;
keepalive_timeout 65;
gzip on;

PHP-FPM优化

编辑/etc/php-fpm.d/www.conf

pm = dynamic
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 35

MySQL优化

编辑/etc/my.cnf/etc/mysql/my.cnf

innodb_buffer_pool_size = 1G
innodb_log_file_size = 256M
query_cache_size = 64M

完成以上步骤后,您的LNMP服务器就配置完成了,可以开始部署Web应用程序了。