Centos7配置PHP + MySQL + Nginx

一. 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('PassWord123@'); 

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

# 添加远程帐户

GRANT ALL PRIVILEGES ON *.* TO 'yourname'@'%' IDENTIFIED BY 'YourPassword@123' 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安装与配置


# 下载安装包

wget http://nginx.org/download/nginx-1.10.0.tar.gz

# 解压Nginx的tar包,并进入解压好的目录

tar -zxvf nginx-1.10.0.tar.gz
cd nginx-1.10.0/

# 安装zlib和pcre库

yum -y install zlib zlib-devel
yum -y install pcre pcre-devel

# 配置、编译并安装

./configure--with-http_ssl_module
make
make install

# 启动nginx

/usr/local/nginx/sbin/nginx

访问服务器后如下图显示说明Nginx运正常。


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

server {
        listen       80;
        server_name  xxx.xxxx.com; # 你的域名
	location / {
	    root  /var/www/xxx项目目录/;
	    index index.php;
	}
       	location ~ ^(.+\.php)(.*)$ {
	    root           /var/www/xxx项目目录/;
	    fastcgi_pass   127.0.0.1:9000;
	    fastcgi_index  index.php;
	    fastcgi_split_path_info  ^(.+\.php)(.*)$;
	    fastcgi_param PATH_INFO $fastcgi_path_info;
	    if (!-e $document_root$fastcgi_script_name) {
	        return 404;
	    }
	    fastcgi_param  SCRIPT_FILENAME$document_root$fastcgi_script_name;
	    include        fastcgi_params;
	}
    }


常用指令

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