centos7怎么部署php项目

藏色散人
发布: 2021-10-19 10:25:36
原创
4348人浏览过
centos7部署php项目的方法:1、通过yum install安装nginx和mysql;2、修改mysql登录密码;3、安装PHP及扩展;4、配置nginx站点;5、进行项目测试部署即可。

centos7怎么部署php项目

本文操作环境:centos7系统、PHP7.1版、DELL G3电脑

centos7怎么部署php项目?

CentOS 7部署PHP项目的方法:

立即学习PHP免费学习笔记(深入)”;

目录

一、安装nginx(自动)

二、安装mysql

三、修改mysql登录密码

四、安装PHP及扩展

五、配置nginx站点

六、项目测试部署

写在前面:本文编辑服务器文件使用的是editplus工具

一、安装nginx(自动)
添加nginx源

<p>rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm<br/></p>
登录后复制

安装nginx

<p>yum install nginx<br/></p>
登录后复制

启动nginx服务

<p>systemctl start nginx.service    //启动<br/> systemctl enable nginx.service    //开机启动<br/></p>
登录后复制

测试访问,如果可以看到nginx欢迎界面则说明安装成功且能正常访问

 二、安装mysql

<p>wget http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm    //下载mysql源<br/> rpm -ivh mysql-community-release-el7-5.noarch.rpm    //安装mysql源<br/> yum install mysql-community-server    //安装mysql<br/></p>
登录后复制

启动mysql服务

<p>systemctl start mysqld    //启动<br/> systemctl enable mysqld    //开机启动<br/> systemctl daemon-reload    //开机启动<br/></p>
登录后复制

三、修改mysql登录密码

<p>grep 'temporary password' /var/log/mysqld.log    //查看临时生成的密码<br/> mysql -uroot -p    //使用临时密码登录<br/> > ALTER USER 'root'@'localhost' IDENTIFIED BY '新密码';    //修改密码<br/></p>
                    <div class="aritcle_card">
                        <a class="aritcle_card_img" href="/ai/%E7%AC%94%E7%9B%AE%E9%B1%BC%E8%8B%B1%E6%96%87%E8%AE%BA%E6%96%87%E5%86%99%E4%BD%9C%E5%99%A8">
                            <img src="https://img.php.cn/upload/ai_manual/000/000/000/175680141089375.png" alt="笔目鱼英文论文写作器">
                        </a>
                        <div class="aritcle_card_info">
                            <a href="/ai/%E7%AC%94%E7%9B%AE%E9%B1%BC%E8%8B%B1%E6%96%87%E8%AE%BA%E6%96%87%E5%86%99%E4%BD%9C%E5%99%A8">笔目鱼英文论文写作器</a>
                            <p>写高质量英文论文,就用笔目鱼</p>
                            <div class="">
                                <img src="/static/images/card_xiazai.png" alt="笔目鱼英文论文写作器">
                                <span>87</span>
                            </div>
                        </div>
                        <a href="/ai/%E7%AC%94%E7%9B%AE%E9%B1%BC%E8%8B%B1%E6%96%87%E8%AE%BA%E6%96%87%E5%86%99%E4%BD%9C%E5%99%A8" class="aritcle_card_btn">
                            <span>查看详情</span>
                            <img src="/static/images/cardxiayige-3.png" alt="笔目鱼英文论文写作器">
                        </a>
                    </div>
                
登录后复制

四、安装PHP及扩展

<p>yum install php php-mysql php-fpm php-mbstring php-gd php-pear <br/>php-mhash php-eaccelerator  php-cli php-imap php-ldap php-odbc php-pear <br/>php-xml php-xmlrpc php-mssql php-snmp php-soap php-tidy php-common php-devel <br/>php-pecl-xdebug phpmyadmin php-mcrypt -y<br/></p>
登录后复制

编辑/etc/php.ini文件,修改参数

<p>cgi.fix_pathinfo=0<br/></p>
登录后复制

编辑/etc/php-fpm.d/www.conf文件,修改参数

<p>listen = /var/run/php-fpm/php-fpm.sock<br/></p>
登录后复制

启动php-fpm服务

<p>systemctl start php-fpm    //启动<br/> systemctl enable php-fpm.service    //开机启动<br/></p>
登录后复制

五、配置nginx站点

修改/etc/nginx/conf.d/default.conf文件,添加如下参数

<p>server {<br/>     listen       80;<br/>     server_name  www.sange.com;    #需要修改客户端hosts文件<br/>  <br/>     root   /opt/data;    #PHP项目根路径<br/>     index index.php index.html index.htm;<br/>  <br/>     location / {<br/>         try_files $uri $uri/ =404;<br/>     }<br/>     error_page 404 /404.html;<br/>     error_page 500 502 503 504 /50x.html;<br/>     location = /50x.html {<br/>         root /usr/share/nginx/html;<br/>     }<br/>  <br/>     location ~ \.php$ {<br/>         try_files $uri =404;<br/>         fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;<br/>         fastcgi_index index.php;<br/>         fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;<br/>         include fastcgi_params;<br/>     }<br/> }<br/></p>
登录后复制

重启nginx服务

<p>systemctl restart nginx<br/></p>
登录后复制

六、项目测试部署

新建/opt/data/info.php文件,打开文件编辑,添加

<p><?php phpinfo()?><br/></p>
登录后复制

浏览器访问www.sange.com,可以看到php各种配置信息则说明配置成功,如

 当然,这只是为了测试一下环境而新建的一个简单php文件,当真正部署项目的时候,需要修改项目数据库配置文件中用户名跟密码,导入数据库操作。在这种情况下,如果需要客户端登录数据库,服务器的mysql需要设置允许远程登录功能,授予用户访问权限。当浏览器访问需要连接数据库时,默认情况下会遇到一个错误提示,那就是SQLSTATE[HY000] [2003] Can't connect to MySQL server on '127.0.0.1' (13)。

问题:SQLSTATE[HY000] [2003] Can't connect to MySQL server on '127.0.0.1' (13)

原因:SELinux 不让 httpd 访问外网

解决办法:

<p>getsebool -a | grep httpd    //查看httpd状态<br/> setsebool httpd_can_network_connect 1     //允许外访问<br/> systemctl restart mysqld.service    //重启mysql服务<br/></p>
登录后复制

推荐学习:《PHP视频教程》   

以上就是centos7怎么部署php项目的详细内容,更多请关注php中文网其它相关文章!

PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号