LNMP代表Linux、Nginx、MySQL和PHP,是一种常见的Web服务器架构。以下是配置LNMP服务器的详细步骤:
推荐使用CentOS 7/8或Ubuntu 18.04/20.04 LTS
# CentOS
yum update -y
# Ubuntu
apt update && apt upgrade -y
yum install epel-release -y
yum install nginx -y
systemctl start nginx
systemctl enable nginx
apt install nginx -y
systemctl start nginx
systemctl enable nginx
访问服务器IP地址,应看到Nginx欢迎页面
yum install mariadb-server mariadb -y
systemctl start mariadb
systemctl enable mariadb
mysql_secure_installation
apt install mysql-server -y
systemctl start mysql
systemctl enable mysql
mysql_secure_installation
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
apt install php-fpm php-mysql php-gd php-mbstring php-xml php-curl -y
编辑/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
编辑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
创建测试文件:
echo "<?php phpinfo(); ?>" > /var/www/html/info.php
访问http://your_server_ip/info.php
,应看到PHP信息页面。
# CentOS
firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=https
firewall-cmd --reload
# Ubuntu
ufw allow 'Nginx Full'
编辑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
# CentOS
yum install phpmyadmin -y
# Ubuntu
apt install phpmyadmin -y
# CentOS
yum install redis -y
systemctl start redis
systemctl enable redis
# Ubuntu
apt install redis-server -y
编辑/etc/nginx/nginx.conf
:
worker_processes auto;
worker_connections 1024;
keepalive_timeout 65;
gzip on;
编辑/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
编辑/etc/my.cnf
或/etc/mysql/my.cnf
:
innodb_buffer_pool_size = 1G
innodb_log_file_size = 256M
query_cache_size = 64M
完成以上步骤后,您的LNMP服务器就配置完成了,可以开始部署Web应用程序了。