以Aliyun体验机为例,从零搭建LNMPR环境(下)

使用云服务器搭建 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 的版本信息:

Nginx 欢迎页

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:

远程访问 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:

远程访问 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 这一行。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值