yum搭建lnmp环境Linux Centos 7.2+Nginx+PHP7+MySQL5.7

4月 17, 2016

基础环境配置

检查是否有添加了 FedoraProject 的 epel 源,如果系统默认没有加入这个源

1
2
3
4
//检查
yum repolist
//安装
yum install epel-release

更换CentOS源

1
2
3
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
yum makecache //清除yum缓存
yum update //更新

安装Nginx

1
vim /etc/yum.repos.d/nginx.repo

添加一下内容,官网说明

1
2
3
4
5
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1

安装

1
2
3
4
5
yum update
yum install nginx
nginx -v //查看版本
systemctl start nginx
systemctl enable nginx//开机自启动

配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
vim /etc/nginx/conf.d/default.conf
location / {
root /data/www/public;
index index.php index.html index.htm;
try_files $uri $uri/ /index.php?$query_string;
}

location ~ \.php$ {
root /data/www/public;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}

//注意将fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;里的/scripts改为路径地址,如
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

安装PHP7

更新php7源

1
rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm

将[remi]段中的enabled=0改为enabled=1

1
vim /etc/yum.repos.d/remi.repo

将[remi-php70]段中的enabled=0改为enabled=1

1
vim /etc/yum.repos.d/remi-php70.repo

查看PHP版本

1
yum list php

yum安装

1
yum install php php-fpm php-cli php-pdo php-mysql php-gd php-bcmath php-xml php-mbstring php-mcrypt php-devel

php的php.ini配制一般在/etc/php.ini

1
2
3
4
5
6
7
8
9
10
11
vim /etc/php.ini

date.timezone = Asia/Shanghai
upload_max_filesize = 20M
post_max_size = 20M
display_errors = Off // 生产环境半掉就好了

# 使HTTP Header中不显示PHP信息把
expose_php = On
修改为
expose_php = Off
1
2
3
4
vim  /etc/php-fpm.d/www.conf
修改为
user = nginx
group = nginx

启动

1
2
service php-fpm restart
chkconfig php-fpm on //开机自启

安装MySQL5.7

可以在https://dev.mysql.com/downloads/repo/yum/ 查看MySQL Yum Repository路径

1
2
3
4
5
6
7
8
rpm -Uvh http://dev.mysql.com/get/mysql57-community-release-el7-9.noarch.rpm
打开 mysql-community.repo 看关于mysql的内空,确定mysql57的enable为1
vim /etc/yum.repos.d/mysql-community.repo

yum makecache
yum update

yum install mysql-community-server

启动

1
2
service mysqld start
chkconfig mysqld on //开机自启

启动后,查看安装后自动生成的密码

1
2
grep "password" /var/log/mysqld.log
grep 'temporary password' /var/log/mysqld.log //或者查看临时密码

修改密码并初始化

1
mysql_secure_installation

允许远程访问

1
2
3
4
5
6
7
mysql -uroot -p
use mysql
update user set Host='%' where User ='root' limit 1;
flush privileges;

set global validate_password_policy=0; //设置安全权限级别,否则普通密码难修改
ALTER USER 'root'@'localhost' IDENTIFIED BY 'root'; //修改密码

安装扩展

yum install -y lrzsz composer git libuuid-devel gcc gcc-c++ cmake

更换composer为中国镜像,不能用root帐号,创建linshi帐号

1
2
3
4
useradd linshi
su linshi //切换
composer config -g repo.packagist composer https://packagist.phpcomposer.com
userdel linshi //删除帐号

安装uuid

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
yum install libuuid-devel
wget http://pecl.php.net/get/uuid-1.0.4.tgz
tar -zxvf uuid-1.0.4.tgz
cd uuid-1.0.4

phpize
//如果出现Can't find PHP headers in /usr/include/php The php-devel package is required,则
yum install php-devel
./configure

make && make install

将上边的添加到php.ini的Dynamic Extensions
vim /etc/php.ini
extension=uuid.so

安装redis

1
2
3
4
5
yum install redis
redis-server &

service redis start
chkconfig redis on
1
2
3
4
mkdir wwwroot
//文件夹权限
chmod 777 -R storage
chmod 777 -R vendor

路径

  • 站点配置文件 /etc/nginx/conf.d/*.conf
  • php配置文件 /etc/php.ini
  • MySQL配置文件 /etc/my.cnf

参考资料

CentOS7 下使用 YUM 手工搭建 LNMP 环境记录
entOS6.5 下升级 PHP7、MySQL5.7

https://www.ssllabs.com/ssltest/analyze.html?d=www.wugenglong.com