阿里云(ECS) CentOS7.3搭建LNMP环境(Nginx 1.10.2 + MariaDB 5.5.52 + PHP 7.1.8)
一、安装 Nginx
1. 使用 yum 安装 Nginx:
# yum install -y nginx
2. 启动 Nginx:
# systemctl start nginx
3. 设置 Nginx 随系统自动启动:
# systemctl enable nginx
4. 查看 Nginx 状态:
# systemctl status nginx
成功安装后,会看到如下信息:
● nginx.service - The nginx HTTP and reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
Active: active (running) since Mon 2017-08-21 20:33:53 CST; 16s ago
5. 浏览器中访问 http://<公网IP地址>
一切顺利的话可以查看到 Nginx 欢迎页面。
二、安装MySQL(MariaDB)
1. 安装 MariaDB(MariaDB是MySQL的一个分支):
# yum install -y mariadb-server
2. 启动 MariaDB:
# systemctl start mariadb
3. 设置 MariaDB:
# mysql_secure_installation
Enter current password for root (enter for none):
初始root密码为空,直接回车
Set root password? [Y/n] 是否设置root密码,输入:y
New password:设置数据库密码
Re-enter new password:确认数据库新密码
Remove anonymous users? [Y/n]是否移除匿名用户,输入:y
Disallow root login remotely? [Y/n]是否禁止root用户远程登录,输入:y
Remove test database and access to it? [Y/n]是否删除测试用的数据库,输入:y
Reload privilege tables now? [Y/n]是否重新加载权限表,输入:y
4. 设置 MariaDB 随系统自动启动:
# systemctl enable mariadb
三、安装PHP7.1
1. 配置PHP源:
# 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
2. 安装扩展:
# yum install -y mod_php71w php71w-bcmath php71w-cli php71w-common php71w-devel php71w-fpm php71w-gd php71w-mbstring php71w-mcrypt php71w-mysql php71w-snmp php71w-xml php71w-process php71w-ldap net-snmp net-snmp-devel net-snmp-utils rrdtool
3. 查看是否安装成功:
# php -v
成功安装后,会看到如下信息:
PHP 7.1.8 (cli) (built: Aug 9 2017 19:19:49) ( NTS )Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2017 Zend Technologies`
4. 启动 php-fpm:
# systemctl start php-fpm
5. 设置 php-fpm 随系统自动启动:
# systemctl enable php-fpm
6. 查看 php-fpm 状态:
# service php-fpm status
成功安装后,会看到如下信息:
● php-fpm.service - The PHP FastCGI Process Manager
Loaded: loaded (/usr/lib/systemd/system/php-fpm.service; enabled; vendor preset: disabled)
Active: active (running) since Sat 2017-08-21 20:54:05 CST; 23s ago
四、配置 Nginx 站点
1. 创建网站根目录:
# mkdir -p /home/youwww
2. 建立测试文件:
# vim /home/youwww/phpinfo.php
3. 输入如下内容:
<?php phpinfo(); ?>
4. 保存并退出:
# :wq
5. 创建配置文件:
# vim /etc/nginx/conf.d/default.conf
6. 修改为:
server { listen 80; #监听80端口 # server_name www.youwebsite.com; # 绑定域名(域名解析成后去掉 # 注释) root /home/youwww; #设置站点根目录 location / { index index.html index.htm index.php; #设置首页文件,越前优先级越高 } location ~ \.php$ { root /home/youwww; #设置站点根目录 fastcgi_pass 127.0.0.1:9000; #默认使用9000端口和PHP通信 fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } }
7. 重启服务:
# systemctl reload nginx
8. 浏览器中访问 http://<公网IP地址>/phpinfo.php
一切正常的话此时就能显示出PHP的版本测试页
浏览器中访问 http://<公网IP地址>/phpMyAdmin/
使用之前设置的数据库用户名和密码登陆
备注1:
使用80端口时,打开PHP测试页时会显示错误提示 The page you are looking for is not found.
解决办法1:使用其他端口,如:8000、8080
解决办法2:修改 /etc/nginx/nginx.conf 文件,注释掉 listen 80 default_server; -> # listen 80 default_server;
备注2:
查看 Nginx 状态时,如果遇到错误提示 Failed to read PID from file /run/nginx.pid: Invalid argument
因为 nginx 启动需要一点点时间,而 systemd 在 nginx 完成启动前就去读取 pid file 造成读取 pid 失败。解决方法很简单,让 systemd 在执行 ExecStart 的指令后等待一点点时间即可。
如果你的 nginx 启动需要时间更长,可以把 sleep 时间改长一点 (参考资料:nginx Failed to read PID from file /run/nginx.pid: Invalid argument):
# mkdir -p /etc/systemd/system/nginx.service.d
# printf "[Service]\nExecStartPost=/bin/sleep 0.1\n" > /etc/systemd/system/nginx.service.d/override.conf
# systemctl daemon-reload
# systemctl restart nginx