CentOS LNMP配置教程

环境准备
- 操作系统:CentOS 7.x
- 软件版本:Nginx 1.16.1、MySQL 5.7.25、PHP 7.2.34
- 网络环境:公网IP或内网IP
安装LNMP环境
安装Nginx
# 安装依赖 yum install -y pcre pcre-devel zlib zlib-devel openssl openssl-devel # 安装Nginx yum install -y nginx
安装MySQL
# 安装依赖 yum install -y gcc gcc-c++ make automake autoconf libtool libxml2 libxml2-dev libjpeg libjpeg-dev libpng libpng-dev freetype freetype-dev libxml2 libxml2-dev openldap openldap-devel readline readline-devel curl curl-devel # 下载MySQL源码包 wget http://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm # 安装MySQL源码包 yum install -y mysql57-community-release-el7-11.noarch.rpm # 安装MySQL yum install -y mysql-community-server
安装PHP
# 安装依赖 yum install -y gcc gcc-c++ make automake autoconf libtool bison bison-devel libxml2 libxml2-dev libxslt libxslt-dev libcurl libcurl-devel openssl openssl-devel # 安装PHP yum install -y php php-mysqlnd php-gd php-xml php-mbstring php-zip php-fpm
配置Nginx
修改Nginx配置文件
# 进入Nginx配置文件目录 cd /etc/nginx # 备份原始配置文件 cp nginx.conf nginx.conf.bak # 修改配置文件 vi nginx.conf
添加以下内容到配置文件:
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
keepalive_timeout 65;
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}添加站点配置文件

# 创建站点目录 mkdir /var/www/html/myweb # 创建站点配置文件 vi /etc/nginx/conf.d/myweb.conf
添加以下内容到配置文件:
server {
listen 80;
server_name myweb.com;
location / {
root /var/www/html/myweb;
index index.html index.htm;
try_files $uri $uri/ =404;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}重启Nginx服务
systemctl restart nginx
配置PHP-FPM
修改PHP-FPM配置文件
# 进入PHP-FPM配置文件目录 cd /etc/php-fpm.d # 备份原始配置文件 cp www.conf www.conf.bak # 修改配置文件 vi www.conf
添加以下内容到配置文件:
[www] user = nginx group = nginx listen = /var/run/php-fpm.sock listen.owner = nginx listen.group = nginx pm = dynamic pm.max_children = 50 pm.start_servers = 10 pm.min_spare_servers = 5 pm.max_spare_servers = 35
重启PHP-FPM服务
systemctl restart php-fpm
配置MySQL
初始化MySQL
# 初始化MySQL mysqld --initialize
修改MySQL密码

# 进入MySQL安全模式 mysql_secure_installation # 输入root用户密码,回车 # 设置root密码 # 移除匿名用户 # 禁用root用户远程登录 # 删除test数据库 # 退出安全模式
创建数据库和用户
# 登录MySQL mysql -u root -p # 创建数据库 CREATE DATABASE myweb; # 创建用户 CREATE USER 'myuser'@'localhost' IDENTIFIED BY 'mypassword'; # 授权用户 GRANT ALL PRIVILEGES ON myweb.* TO 'myuser'@'localhost'; # 刷新权限 FLUSH PRIVILEGES; # 退出MySQL EXIT
相关问答FAQs
Q1:如何检查LNMP环境是否配置成功?
A1:可以使用以下命令检查Nginx、MySQL和PHP-FPM服务是否运行正常:
systemctl status nginx systemctl status mysql systemctl status php-fpm
Q2:如何访问LNMP环境下的网站?
A2:将浏览器地址栏输入你的域名或IP地址,然后按回车键即可访问网站,访问域名myweb.com,访问IP地址192.168.1.1。
图片来源于AI模型,如侵权请联系管理员。作者:酷小编,如若转载,请注明出处:https://www.kufanyun.com/ask/128287.html




