温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

centos中部署php项目的方法

发布时间:2020-07-01 11:36:41 来源:亿速云 阅读:230 作者:Leah 栏目:编程语言

这期内容当中小编将会给大家带来有关centos中部署php项目的方法,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

一、安装nginx(自动)

添加nginx源

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

安装nginx

yum install nginx

启动nginx服务

systemctl start nginx.service    //启动
systemctl enable nginx.service    //开机启动

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

二、安装mysql

wget http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm    //下载mysql源
rpm -ivh mysql-community-release-el7-5.noarch.rpm    //安装mysql源
yum install mysql-community-server    //安装mysql

启动mysql服务

systemctl start mysqld    //启动
systemctl enable mysqld    //开机启动
systemctl daemon-reload    //开机启动

三、修改mysql登录密码

grep 'temporary password' /var/log/mysqld.log    //查看临时生成的密码
mysql -uroot -p    //使用临时密码登录
> ALTER USER 'root'@'localhost' IDENTIFIED BY '新密码';    //修改密码

四、安装PHP及扩展

yum install php php-mysql php-fpm php-mbstring php-gd php-pear php-mhash php-eaccelerator  php-cli php-imap php-ldap php-odbc php-pear php-xml php-xmlrpc php-mssql php-snmp php-soap php-tidy php-common php-devel php-pecl-xdebug phpmyadmin php-mcrypt -y

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

cgi.fix_pathinfo=0

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

listen = /var/run/php-fpm/php-fpm.sock

启动php-fpm服务

systemctl start php-fpm    //启动
systemctl enable php-fpm.service    //开机启动

五、配置nginx站点

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

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

重启nginx服务

systemctl restart nginx

六、项目测试部署

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

<?php phpinfo()?>

浏览器访问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 访问外网

解决办法:

getsebool -a | grep httpd    //查看httpd状态
setsebool httpd_can_network_connect 1     //允许外访问
systemctl restart mysqld.service    //重启mysql服务

上述就是小编为大家分享的centos中部署php项目的方法了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI