CentOS 7 安装 LNMP 环境概述
前言
本教程详细讲解如何在 CentOS 7 环境下利用 YUM 包管理器搭建 LNMP 架构,具体组件包括 Linux 操作系统、Nginx Web 服务器、PHP 7.1 解析引擎以及 MySQL 5.7 数据库。文中将逐步演示各项服务的安装与配置过程,内容详实清晰,旨在为读者提供一份可直接参考的操作指南。
具体实施步骤如下:
YUM 包管理器的基本使用
yum update
通过 YUM 安装 Nginx
添加 Nginx 官方最新软件源
yum localinstall http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
yum repolist enabled | grep "nginx*"
执行 Nginx 安装命令
yum -y install nginx
启动 Nginx 服务
service nginx start
配置 Nginx 服务开机自动启动
systemctl enable nginx.service
验证开机自启动配置是否生效
systemctl list-dependencies | grep nginx
通过浏览器访问公网 IP 以确认安装结果
http://00.00.00.00/
通过 YUM 安装 MySQL 5.7
添加 MySQL 官方社区源
yum -y localinstall http://dev.mysql.com/get/mysql57-community-release-el7-7.noarch.rpm
yum repolist enabled | grep "mysql.*-community.*"
执行 MySQL 服务端安装
yum -y install mysql-community-server install mysql-community-devel
启动 MySQL 守护进程
service mysqld start
检查 MySQL 服务运行状态是否正常
service mysqld status 或者 ps -ef | grep mysql
配置 mysqld 服务开机自启动
systemctl enable mysqld.service
验证 mysqld 开机自启动设置是否成功
systemctl list-dependencies | grep mysqld
MySQL 5.7 版本增强了安全机制,YUM 安装后系统会自动生成临时随机密码,需手动修改 root 密码
查找并查看 MySQL 生成的临时随机密码
grep 'temporary password' /var/log/mysqld.log
使用获取到的随机密码登录终端进行初始认证
mysql -u root -p 更改密码(mysql文档规定,密码必须包括大小写字母数字加特殊符号>8位)
ALTER USER 'root'@'localhost' IDENTIFIED BY 'Yourpassword';
退出客户端后,使用新设置的密码重新登录,以确认密码修改有效
exit;
mysql -u root -p
安装 PHP 7.1
配置 PHP 所需的第三方软件源
rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
检查软件源是否已正确导入
yum repolist enabled | grep "webtatic*"
安装 PHP 相关扩展源
yum -y install php71w php71w-fpm
yum -y install php71w-mbstring php71w-common php71w-gd php71w-mcrypt
yum -y install php71w-mysql php71w-xml php71w-cli php71w-devel
yum -y install php71w-pecl-memcached php71w-pecl-redis php71w-opcache
验证 PHP 7.1.x 版本及其扩展模块是否安装完毕
综合验证 PHP 环境是否安装成功
php -v
逐项验证所需 PHP 扩展模块是否可用
php -m
配置 PHP-FPM 并检测其运行状态
启动 PHP-FPM 服务
service php-fpm star
检查 PHP-FPM 服务启动状态
service php-fpm status
设置 PHP-FPM 开机自启动
systemctl enable php-fpm.service
检查开机自启动配置是否生效
systemctl list-dependencies | grep php-fpm
ps -ef | grep php-fpm
Nginx 配置文件示例如下:
server{
listen 80;
server_name youserver;
index index.html index.php;
root /home/public;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
index index.html index.htm index.php;
try_files $uri $uri/ /index.php?$query_string;
}
error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ .php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ .php$ {
root /home/public;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /home/public$fastcgi_script_name;
include fastcgi_params;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /.ht {
deny all;
}
}
总结
以上即为本文的全部内容,希望该教程对您的学习或工作有所帮助。如有任何疑问,欢迎留言交流,感谢支持。