记录下在CentOS 7 安装 LNMP 环境(PHP7 + MySQL5.7 + Nginx1.10)过程笔记。
工具
VMware版本号 : 12.0.0
CentOS版本 : 7.0
一、修改 yum 源
[root@localhost ~]# rpm -Uvh https://dl.Fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
[root@localhost ~]# rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
[root@localhost ~]# rpm -Uvh http://dev.mysql.com/get/mysql57-community-release-el7-9.noarch.rpm
Webtatic:https://webtatic.comhttps://webtatic.com/
MySQL:https://dev.mysql.com/downloads/repo/yum/https://dev.mysql.com/downloads/repo/yum/
二、安装 Nginx、MySQL、PHP
[root@localhost ~]# yum -y install nginx
[root@localhost ~]# yum -y install mysql-community-server
[root@localhost ~]# yum -y install php70w-devel php70w.x86_64 php70w-cli.x86_64 php70w-common.x86_64 php70w-gd.x86_64 php70w-ldap.x86_64 php70w-mbstring.x86_64 php70w-mcrypt.x86_64 php70w-pdo.x86_64 php70w-mysqlnd php70w-fpm php70w-opcache php70w-pecl-redis php70w-pecl-mongo
其中,如果是laravel新版本,那么,有些特性需要PHP7.2级以上版本才能支持
所以,我们需要升级PHP7.0;因为我们之前安装了PHP7.0,所以,需要先卸载:
1.卸载旧版本
yum -y remove php*
2.修改云源
rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
3.安装
yum -y install php72w php72w-cli php72w-common php72w-devel php72w-embedded php72w-fpm php72w-gd php72w-mbstring php72w-mysqlnd php72w-opcache php72w-pdophp72w-xml
三、配置
1、配置 MySQL
MySQL 安装完成之后,在 /var/log/mysqld.log 文件中给 root 生成了一个默认密码
通过下面的方式找到root 默认密码,然后登录 MySQL 进行修改:
[root@localhost ~]# systemctl start mysqld # 启动 MySQL
[root@localhost ~]# grep 'temporary password' /var/log/mysqld.log # 查找默认密码2017-04-10T02:58:16.806931Z 1 [Note] A temporary password is generated for root@localhost: iacFXpWt-6gJ
登录 MySQL:
[root@localhost ~]# mysql -uroot -p'iacFXpWt-6gJ'
修改root 默认密码:
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyPass1!';
或者:
mysql> set password for 'root'@'localhost'=password('123abc');
注:
MySQL5.7 默认安装了密码安全检查插件(validate_password),默认密码检查策略要求密码必须包含:大小写字母、数字和特殊符号,并且长度不能少于8位。否则会提示 ERROR 1819 (HY000): Your password does not satisfy the current policy requirements 错误
详见 MySQL 官网密码策略详细说明:https://dev.mysql.com/doc/refman/5.7/en/validate-password-options-variables.htmlhttps://dev.mysql.com/doc/refman/5.7/en/validate-password-options-variables.html
配置默认编码为 utf8:
修改 /etc/my.cnf 配置文件,在 [mysqld] 下添加编码配置,配置完成后重启:
[root@localhost ~]# vim /etc/my.cnf[mysqld]character_set_server=utf8init_connect='SET NAMES utf8'[root@localhost ~]# systemctl restart mysqld # 重启 MySQL
设置开机启动:
[root@localhost ~]# systemctl enable mysqld
默认配置文件路径:
配置文件:/etc/my.cnf
日志文件:/var/log/mysqld.log
服务启动脚本:/usr/lib/systemd/system/mysqld.service
socket 文件:/var/run/mysqld/mysqld.pid
2、配置 Nginx
安装完成以后查看自己防火墙是否开启,如果已开启,我们需要修改防火墙配置,开启 Nginx 外网端口访问。
[root@localhost ~]# systemctl status firewalld
如果显示 active (running),则需要调整防火墙规则的配置。
修改 /etc/firewalld/zones/public.xml文件,在zone一节中增加
保存后重新加载 firewalld 服务:
[root@localhost ~]# vim /etc/firewalld/zones/public.xml
[root@localhost ~]# systemctl reload firewalld
修改 Nginx 配置:
[root@localhost ~]# vim /etc/nginx/nginx.conf
在 server {} 里添加:
location / {
#定义首页索引文件的名称
index index.php index.html index.htm;
}
# PHP 脚本请求全部转发到 FastCGI处理. 使用FastCGI默认配置.location ~ .php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
配置完成重启 Nginx:
[root@localhost ~]# systemctl start nginx # 启动 Nginx
注:本文只是简单配置 Nginx,具体更多配置请自行百度。
设置开机启动:
[root@localhost ~]# systemctl enable nginx
3、设置开机启动 php-fpm:
[root@localhost ~]# systemctl enable php-fpm
[root@localhost ~]# systemctl start php-fpm # 启动 php-fpm
四、测试
在 /usr/share/nginx/html 文件下创建php文件,输出 phpinfo 信息
浏览器访问 http://<内网IP地址>/phpinfo.php,如果看到 PHP信息,说明安装成功
五、补充
创建FTP账号
1.安装FTP
yum -y install vsftpd
2.新建用户,并指定主目录
useradd -d /home/test -m test (增加用户test,并制定test用户的主目录为/home/test)
3.设置新用户密码
passwd test (为test设置密码)
4.更改用户相应的权限设置
usermod -s /sbin/nologin test (限定用户test不能telnet,只能ftp)
usermod -s /sbin/bash test (用户test恢复正常)
usermod -d /test test (更改用户test的主目录为/test)
5.限制用户只能访问/home/test,不能访问其他路径
修改/etc/vsftpd/vsftpd.conf如下:
chroot_list_enable=YES //限制访问自身目录
# (default follows)
chroot_list_file=/etc/vsftpd/chroot_list
编辑 chroot_list文件,将受限制的用户添加进去,每个用户名一行
改完配置文件,不要忘记重启vsFTPd服务器
[root@linuxsir001 root]# service vsftpd restart