使用云服务器搭建 Web 运行环境,尤其是搭建常见的 LNMPR(Linux+Nginx+MySQL+PHP+Redis) 环境,对于开发人员是必备的职场基本技能之一。在这里,借着搭建我的“魚立说”个人网站的机会,整理了从零搭建 LNMPR 环境的详细过程,期间遇到的问题也一一进行了记录。

本文来源:魚立说。本文链接:https://www.yulisay.com/d/lnmpr2.html,支持微信浏览器打开。

更多精彩文章,请移步 魚立说个人网站 翻看。欢迎欣赏,吐槽不足之处。


本主题使用到的服务器是 Aliyun 的 ECS 体验机,适用于在 CentOS 操作系统下搭建 LNMPR 运行环境,整个系列由以下两个文章部分组成:

2. 配置运行(带配置+运行)

在《编译安装 LNMPR》中,我们已经编译安装好 LNMPR 服务,接下来对它们进行配置,从而让我们的 Web 项目运行起来。

2.1 配置 NMPR

下面依次对 Nginx、MySQL、PHP、Redis 进行了配置。

2.1.1 配置 Nginx

找到 /usr/local/nginx/conf/nginx.conf 文件,并做如下配置:

user www admin;
worker_processes auto; pid /data/run/nginx.pid; events {
worker_connections 1024;
} http {
include mime.types;
default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"'; access_log /data/log/nginx/access.log main;
error_log /data/log/nginx/error.log warn; sendfile on;
tcp_nopush on;
client_max_body_size 100M; keepalive_timeout 60; server {
listen 80;
server_name localhost; access_log /data/log/php/test.access.log main;
error_log /data/log/php/test.error.log warn; root /data/project/www;
index index.php index.html index.htm; location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
}

2.1.2 配置 MySQL

找到 /etc/my.cnf 文件,并做如下配置:

[mysqld]
port=3306
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock default_authentication_plugin = mysql_native_password symbolic-links=0 log-error=/data/log/mysqld.log
pid-file=/data/run/mysqld/mysqld.pid character-set-client-handshake = FALSE
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci
init_connect='SET NAMES utf8mb4' default-time-zone='+8:00' slow_query_log=ON
long_query_time=3
slow-query-log-file=/data/log/mysql/slow.log [mysqld_safe]
log-error=/data/log/mysql/error.log [mysql]
default-character-set=utf8mb4 [client]
port=3306
default-character-set=utf8mb4
socket=/var/lib/mysql/mysql.sock

2.1.3 配置 PHP

找到 /usr/local/php/etc/php-fpm.d/www.conf 文件,并做如下配置:

[www]
user = www
group = admin listen = 127.0.0.1:9000 pm = dynamic
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3 request_slowlog_timeout = 3
slowlog = /data/log/php/fpm.slow.log

还需要配置 /usr/local/php/etc/php.ini:

[PHP]
engine = On zend.enable_gc = On max_execution_time = 30
max_input_time = 60 memory_limit = 256M error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT display_errors = On
display_startup_errors = On log_errors = On
log_errors_max_len = 1024
html_errors = On
error_log = /data/log/php/php.error.log default_mimetype = "text/html"
default_charset = "UTF-8" [Date]
date.timezone = "Asia/Shanghai" [opcache]
opcache.enable=1
opcache.enable_cli=1
opcache.memory_consumption=128
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=4000
opcache.revalidate_freq=60
opcache.file_cache=/tmp
zend_extension=/usr/local/php/lib/php/extensions/no-debug-non-zts-20190902/opcache.so

2.1.4 配置 Redis

找到 /usr/local/redis/bin/redis.conf 文件,调整这些配置项:

设置后台启动:daemonize yes

设置密码:requirepass password

注释掉这一行:bind 127.0.0.1

2.1.5 为 firewalld 添加开放端口

systemctl start firewalld && \
firewall-cmd --zone=public --add-port=80/tcp --permanent && \
firewall-cmd --zone=public --add-port=3306/tcp --permanent && \
firewall-cmd --zone=public --add-port=6379/tcp --permanent && \
firewall-cmd --reload

2.2 运行 NMPR

由于服务依赖,需要注意服务的运行顺序。

2.2.1 运行 PHP

PHP 的相关命令:

启动 PHP-FPM:/etc/init.d/php-fpm start

重启 PHP-FPM:/etc/init.d/php-fpm restart

停止 PHP-FPM:/etc/init.d/php-fpm stop

2.2.2 运行 Nginx

Nginx 的相关命令:

启动 Nginx:nginx

关闭 Nginx:nginx -s stop

退出 Nginx:nginx -s quit

更新配置 Nginx:nginx -s reload

Nginx 启动成功后,在浏览器打开公网地址就可以看到 Nginx 欢迎页,这时会打印出 PHP 的版本信息:

2.2.3 运行 MySQL

MySQL 的相关命令:

启动 MySQL:systemctl start mysqld

停止 MySQL:systemctl stop mysqld

设置 MySQL 开机自启:systemctl enable mysqld

查看 MySQL 状态:systemctl status mysqld

首次登录 MySQL 需要在日志文件中找出临时密码:grep 'temporary password' /data/log/mysqld.log

然后使用 root 账号登陆,输入上面找到的临时密码:mysql -uroot -p

我们首先修改下用户密码:ALTER USER 'root'@'localhost' IDENTIFIED BY 'password';

并开启远程访问,这里的 Your_IP 需要替换成你本地的外网 IP:

grant all privileges on *.* to 'root'@'Your_IP' identified by 'password' with grant option;
flush privileges;

这时可以在 Your_IP 发起连接:mysql -hYour_IP -uroot -p'password' -P3306,我们便可以在远程访问 ECS 上的 MySQL:

2.2.4 运行 Redis

启动 Redis:redis-server /usr/local/redis/bin/redis.conf

停止 Redis:redis-cli -h 127.0.0.1 -p 6379 -a password shutdown

在远程访问 ECS 上的 Redis:

到这里,我们的 LNMPR 基础环境就算搭建完成了。当然,后续的正式 ECS 还有更多的工作要做,比如配置 SSL、额外的扩展等,还要结合自己的代码进行具体的部署,比如部署代码、crontab 等。

2.3 可能出现的问题

2.3.1 CentOS 报错:FirewallD is not running

需要设置一下防火墙,开启远端访问功能,但是出于安全考虑最好打开防火墙。

2.3.2 外网无法连接 Redis

除了设置防火墙外,需要修改redis.conf 配置文件,注释掉 bind 127.0.0.1 这一行。

最新文章

  1. VIew-CoordinatorLayout 笔记
  2. wav文件格式分析(一)
  3. 用NPOI操作EXCEL关于HSSFClientAnchor(dx1,dy1,dx2,dy2,col1,row1,col2,row2)的参数
  4. PS之火焰铁锈字
  5. java开发经验分享(三)
  6. Java getResourceAsStream返回为空的问题
  7. CSS中选择器优先级顺序实战讲解
  8. java自动化测试-http请求get
  9. DevOps之内容分发网络CDN
  10. LeetCode专题-Python实现之第28题: Implement strStr()
  11. Entity Framework入门教程(16)---Enum
  12. Linux由浅入深学习 (转)
  13. 进度条的制作-python
  14. oracle中job定时任务96
  15. Android API之android.content.BroadcastReceiver
  16. Webservice超时问题
  17. P3938 斐波那契
  18. linux下一个启动和监测多个进程的shell脚本程序
  19. grail开发环境的搭建
  20. hdu1312 Red and Black

热门文章

  1. taro best practice
  2. react hooks & component will unmount & useEffect & clear up
  3. py python-pptx 创建ppt
  4. .NET 6 Preview 1 发布
  5. 为什么ElasticSearch比MySQL更适合全文索引
  6. vue中v-model的学习
  7. linux文件权限的查看和修改(转)
  8. 基于SaaS平台的iHRM项目的前端项目介绍
  9. 使用Docker快速搭建Nginx+PHP-FPM+MySQL+phpMyAdmin环境
  10. nacos服务注册与发现之客户端