linux nginx+mysql+php lnmp_如何部署LNMP环境(Linux+Nginx+MySQL+PHP)?

本文详细指导如何在CentOS 7上安装和配置Nginx,创建并部署静态和动态网站,同时集成PHP-FPM并设置HTTPS。涉及步骤包括安装相关软件、配置http和https服务、重启服务及防火墙设置,最后演示了PHP测试和数据库初始化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1 安装nginx

1.1 安装nginx相关包

yum install -y http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm

yum install -y nginx

1.2 创建项目目录

mkdir -p /var/www/www.cmdschool.org

1.3 配置http和https服务

http config:

cp /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/www.cmdschool.org_80.conf

vim /etc/nginx/conf.d/www.cmdschool.org_80.conf

保留以下内容:

server {

listen 80;

server_name www.cmdschool.org;

location / {

root /var/www/www.cmdschool.org;

index index.html index.htm index.php;

}

error_page 500 502 503 504 /50x.html;

location = /50x.html {

root /usr/share/nginx/html;

}

}

https config:

cp /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/www.cmdschool.org_443.conf

vim /etc/nginx/conf.d/www.cmdschool.org_443.conf

确认存在如下内容:

server {

listen 443;

server_name www.cmdschool.org;

ssl on;

ssl_certificate 1_www.cmdschool.org_bundle.crt;

ssl_certificate_key 2_www.cmdschool.org.key;

ssl_session_timeout 5m;

ssl_protocols TLSv1 TLSv1.1 TLSv1.2;

ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;

ssl_prefer_server_ciphers on;

location / {

root /var/www/www.cmdschool.org;

index index.html index.htm index.php;

}

error_page 500 502 503 504 /50x.html;

location = /50x.html {

root /usr/share/nginx/html;

}

}

添加静态测试页面:

mkdir -p /var/www/www.cmdschool.org

echo www.cmdschool.org > /var/www/www.cmdschool.org/index.html

1.4 配置nginx服务

systemctl enable nginx

systemctl restart nginx

1.5 配置防火墙

firewall-cmd --permanent --add-service http

firewall-cmd --permanent --add-service https

firewall-cmd --reload

firewall-cmd --list-all

1.6 关闭selinux

sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config

setenforce 0

2 集成PHP fastCGI

2.1 安装基础软件包

yum install -y php-fpm php

2.2 启动并配置服务自启动

systemctl enable php-fpm.service

systemctl start php-fpm.service

2.3 配置集成php fastCGI

vim /etc/nginx/conf.d/www.cmdschool.org_80.conf

vim /etc/nginx/conf.d/www.cmdschool.org_443.conf

增加如下代码

server {

[...]

location ~ \.php$ {

root /var/www/www.cmdschool.org;

fastcgi_pass 127.0.0.1:9000;

fastcgi_index index.php;

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

include /etc/nginx/fastcgi_params;

}

}

重启nginx服务

systemctl restart nginx

2.4 测试php fastCGI

添加调试代码

echo '<?php phpinfo(); ?>' > /var/www/www.cmdschool.org/index.php

访问以下链接测试fastCGI

http://www.cmdschool.org/index.php

https://www.cmdschool.org/index.php

2.5 如下日志可以协助你排错

tail -f /var/log/nginx/error.log

tail -f /var/log/php-fpm/error.log

2.6 安装辅助工具包

yum install -y unzip

3 配置MariaDB数据库

3.1 安装MariaDB相关包

yum install -y mariadb-server mariadb-devel mariadb

3.2 启动MariaDB服务并配置自启动

systemctl start mariadb.service

systemctl enable mariadb.service

3.3 初始化MariaDB

mysql_secure_installation

向导如下:

[...]

Set root password? [Y/n] y

New password:

Re-enter new password:

[...]

Remove anonymous users? [Y/n] y

[...]

Disallow root login remotely? [Y/n] y

[...]

Remove test database and access to it? [Y/n] y

[...]

Reload privilege tables now? [Y/n] y

[...]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值