centos下安装ngnix+php+mysql服务
一、nginx 安装
1.查看yum下nginx版本信息
1 | [root@localhost ~]# yum list | grep nginx |
2.手动添加nginx的yum仓库
1 | [root@localhost ~]# vi /etc/yum.repos.d/nginx.repo |
添加的内容为:
1 2 3 4 5 6 7 8 9 | [nginx] name=nginx repo baseurl=http: //nginx.org/packages/centos/$releasever/$basearch/ gpgcheck=0 enabled=1 |
3.编辑保存之后再查看nginx版本:
[root@localhost ~]# yum list | grep nginx
4.安装nginx服务:
1 | [root@localhost ~]# yum install -y nginx |
5.启动nginx服务:
[root@localhost ~]# service nginx start
注意:/etc/nginx/nginx.conf # Nginx配置文件位置
6.设置开机启动
[root@localhost ~]# chkconfig nginx on
7.nginx服务重启:
[root@localhost ~]# /etc/init.d/nginx restart
8.测试是否正常:
[root@localhost ~]# Curl http://127.0.0.1
9.删除默认的测试页面:
root@localhost ~]# rm -rf /usr/share/nginx/html/*
二、安装mysql
1.安装mysql
1 | [root@localhost ~]# yum install mysql mysql-server |
2.启动mysql
[root@localhost ~]# /etc/init.d/mysqld start
3.设置开机启动
[root@localhost ~]# chkconfig mysqld on
4.为root设置密码:
1 | [root@localhost ~]# mysql_secure_installation |
5.重启启动mysql服务:
1 2 3 | [root@localhost ~]# /etc/init.d/mysqld stop #停止 [root@localhost ~]# /etc/init.d/mysqld start #启动 [root@localhost ~]# service mysqld restart #重启 |
6.设置允许远程连接mysql
1 2 3 4 5 6 7 | [root@localhost ~]# mysql -u root -p Mysql>use mysql; Mysql>grant all privileges on *.* to 'root' @ '%' identified by '123456' with grant option; [root@localhost ~]# service mysqld restart #重启 |
三、安装PHP
1.安装PHP
1 | [root@localhost ~]# yum install php -y |
2.安装PHP组件,使PHP支持 MySQL、PHP支持FastCGI模式
1 | [root@localhost ~]# yum install php-mysql php-gd libjpeg* php-imap php-ldap php-odbc php-pear php-xml php-xmlrpc php-mbstring php-mcrypt php-bcmath php-mhash libmcrypt libmcrypt-devel php-fpm |
3.重启MySql
/etc/init.d/mysqld restart
4.重启nginx
1 | /etc/init.d/nginx restart |
5.启动php-fpm
1 | /etc/rc.d/init.d/php-fpm start |
6.设置开机启动php-fpm
1 | chkconfig php-fpm on |
四、配置nginx支持php
1.备份原有配置文件
cp /etc/nginx/nginx.conf /etc/nginx/nginx.confbak
2.编辑配置文件
1 2 3 4 5 | vim /etc/nginx/nginx.conf user nginx nginx; #修改nginx运行账号为:nginx组的nginx用户 :wq! #保存退出 |
3.备份原有默认配置文件
1 | cp /etc/nginx/conf.d/ default .conf /etc/nginx/conf.d/ default .confbak |
4.编辑默认配置
1 | vim /etc/nginx/conf.d/ default .conf |
修复内容为:
1 | index index.php index.html index.htm; #增加index.php |
修改为以下内容:
1 2 3 4 5 6 7 8 9 10 | # pass the PHPscripts to FastCGI server listening on 127.0.0.1:9000 # location ~ \.php$ { root html; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } #取消FastCGI server部分location的注释,并要注意fastcgi_param行的参数,改为$document_root$fastcgi_script_name,或者使用绝对路径 |
五、配置php
1.配置文件
1 2 3 4 5 6 7 | vim /etc/php.ini #disable_functions=passthru,exec,system,chroot,scandir,chgrp,chown,shell_exec #在386行 列出PHP可以禁用危险的函数 #expose_php = Off #在432行 禁止显示php版本的信息 #magic_quotes_gpc = On #在745行 打开magic_quotes_gpc来防止SQL注入 |
六、配置php-fpm
1.备份原有配置文件
1 | cp /etc/php-fpm.d/www.conf /etc/php-fpm.d/www.confbak |
2.编辑配置文件
vim /etc/php-fpm.d/www.conf
修改为以下:
user = nginx #修改用户为nginx
group = nginx #修改组为nginx
3.重启MySql
1 | /etc/init.d/mysqld restart |
4.重启nginx
1 | /etc/init.d/nginx restart |
5.重启php-fpm
1 | /etc/rc.d/init.d/php-fpm restart |
总结:到处已成功搭建起在centos下的ngnix+php+mysql应用!
分类:
安全运维
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构