欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

CENTOS7 下配置PHP + MySQL + Nginx开发环境

程序员文章站 2024-02-20 08:31:04
...

一. MySQL安装与配置

1. 配置yum源

# 更新yum源

yum update         

# 下载mysql源安装包

wget http://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm

# 安装mysql源 

yum localinstall mysql57-community-release-el7-8.noarch.rpm

# 检查mysql源是否安装成功

yum repolist enabled | grep "mysql.*-community.*"

2. 安装MySQL

yum install mysql-community-server

3. 启动MySQL

# 启动MySQL服务

systemctl start mysqld

# 查看MySQL的启动状态

systemctl status mysqld

# 设置MySQL开机启动

systemctl enable mysqld
systemctl daemon-reload

4. 修改root默认密码

# 找到root默认密码

grep 'temporary password' /var/log/mysqld.log

# 进入mysql控制台, 输入上述查询到的默认密码

mysql -uroot -p

# 设置root管理员的密码

set password for 'root'@'localhost'=password('[email protected]'); 

5.添加远程登录用户
默认只允许root帐户在本地登录,如果要在其它机器上连接mysql,必须修改root允许远程连接,或者添加一个允许远程连接的帐户

# 添加远程帐户

GRANT ALL PRIVILEGES ON *.* TO 'yourname'@'%' IDENTIFIED BY '[email protected]' WITH GRANT OPTION;

6. 配置默认编码为utf8
修改配置文件 /etc/my.cnf,添加下面两行, utf8编码配置

character_set_server=utf8 
init_connect='SET NAMES utf8'


二. PHP环境配置

1. 安装 php 和 php-fpm

# 首先安装epel

yum -y install epel-release

# 安装php php-fpm

yum -y install php php-fpm

# 查看php版本

php -v

2. 安装php-mysql

yum install php-mysql

3. 设置php-fpm开机自动启动

systemctl enable php-fpm

4. 启动php-fpm

systemctl start php-fpm

 

三. Nginx安装与配置

1、添加源

  默认情况Centos7中无Nginx的源,最近发现Nginx官网提供了Centos的源地址。因此可以如下执行命令添加源:

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

 

2、安装Nginx

  通过yum search nginx看看是否已经添加源成功。如果成功则执行下列命令安装Nginx。

sudo yum install -y nginx

 

3、启动Nginx并设置开机自动运行

sudo systemctl start nginx.service
sudo systemctl enable nginx.service


# nginx 配置访问项目目录并支持 PHP 的 pathinfo 模式配置

 

修改 /etc/nginx/conf.d/default.conf
 server {
        #侦听80端口
        listen       80;
        index index.php index.html index.htm;
        #设定本虚拟主机的访问日志
        server_name  localhost;
        root /var/www/blog/public;
        #rewrite_log on;
        #access_log  logs/hc-admin.access.log  main;
        #error_log  logs/hc-admin.access.log  main;
    location ~ .*\.(gif|jpg|jpeg|bmp|png|ico|txt|js|css|ttf|woff|woff2)$
    {
        root /var/www/blog/public;
    }
    location / {  
        index  index.htm index.html index.php;  
 
        if (!-e $request_filename) {  
           rewrite  ^/(.*)$  /index.php/$1  last;  
           break;  
        }  
    }  
    location ~ \.php/?.*$ {  
        root        /var/www/blog/public;  
        fastcgi_pass   127.0.0.1:9000;  
        fastcgi_index  index.php;  
        #加载Nginx默认"服务器环境变量"配置  
        #include        fastcgi.conf;  
        include fastcgi_params; 
       
        set $fastcgi_script_name2 $fastcgi_script_name;  
        if ($fastcgi_script_name ~ "^(.+\.php)(/.+)$") {  
            set $fastcgi_script_name2 $1;  
            set $path_info $2;  
        }  
        fastcgi_param   PATH_INFO $path_info;  
        fastcgi_param   SCRIPT_FILENAME   $document_root$fastcgi_script_name2;  
        fastcgi_param   SCRIPT_NAME   $fastcgi_script_name2;  
    }
    } 


配置ssl

先把监听端口换掉 并打开ssl 然后添加ssl证书  地址按照自己的位置修改
  listen       443;
  ssl                  on;
  #证书地址
  ssl_certificate      /etc/nginx/server.crt;
  ssl_certificate_key  /etc/nginx/server.key;
  ssl_session_timeout  5m;


常用指令

mysql
systemctl start mysqld # 启动
systemctl stop mysqld # 停止
systemctl restart mysqld # 重启

php-fpm
systemctl start php-fpm # 启动
systemctl stop php-fpm # 停止
systemctl restart php-fpm # 重启

nginx
sudo fuser -k 80/tcp # 杀死80端口
/usr/local/nginx/sbin/nginx # 开启
/usr/local/nginx/sbin/nginx -s stop # 停止
/usr/local/nginx/sbin/nginx -s reopen # 重启
/usr/local/nginx/sbin/nginx -s reload # 重新载入配置文件

 

其他问题

1. 关闭SELINUX(SELINUX是一个安全子系统,它能控制程序只能访问特定文件。如果不关闭,你可能访问文件受限):

vi /etc/selinux/config 
#SELINUX=enforcing        # 注释掉 
#SELINUXTYPE=targeted     # 注释掉 
SELINUX=disabled          # 增加 
:wq!                      # 保存退出 
shutdown -r now           # 重启系统

2. thinkphp 提示错误目录 [ ./Runtime/ ] 不可写!

chmod 777 -R /var/www/xxx项目目录/Application/Runtime

转载于:https://my.oschina.net/u/3853452/blog/1933108