以Aliyun体验机为例,从零搭建LNMPR环境(下)
source link: https://www.yulisay.com/d/lnmpr2.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
使用云服务器搭建 Web 运行环境,尤其是搭建常见的 LNMPR(Linux+Nginx+MySQL+PHP+Redis) 环境,对于开发人员是必备的职场基本技能之一。在这里,借着搭建我的“魚立说”个人网站的机会,整理了从零搭建 LNMPR 环境的详细过程,期间遇到的问题也一一进行了记录。
本主题使用到的服务器是 Aliyun 的 ECS 体验机,适用于在 CentOS 操作系统下搭建 LNMPR 运行环境,整个系列由以下两个文章部分组成:
我们已经编译安装好 LNMPR 服务,接下来对它们进行配置,从而让我们的 Web 项目运行起来。
配置 NMPR
下面依次对 Nginx、MySQL、PHP、Redis 进行了配置。
配置 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;
}
}
}
配置 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
配置 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
配置 Redis
找到 /usr/local/redis/bin/redis.conf 文件,调整这些配置项:
设置后台启动:daemonize yes
设置密码:requirepass password
注释掉这一行:bind 127.0.0.1
为 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
运行 NMPR
由于服务依赖,需要注意服务的运行顺序。
运行 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
运行 Nginx
Nginx 的相关命令:
启动 Nginx:nginx
关闭 Nginx:nginx -s stop
退出 Nginx:nginx -s quit
更新配置 Nginx:nginx -s reload
Nginx 启动成功后,在浏览器打开公网地址就可以看到 Nginx 欢迎页,这时会打印出 PHP 的版本信息:
图:Nginx 欢迎页
运行 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
运行 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 等。
可能出现的问题
CentOS 报错:FirewallD is not running
需要设置一下防火墙,开启远端访问功能,但是出于安全考虑最好打开防火墙。
外网无法连接 Redis
除了设置防火墙外,需要修改redis.conf 配置文件,注释掉 bind 127.0.0.1 这一行。
本文作者:于立。欢迎关注本站公众号:「于立为文」。
本文链接:https://www.yulisay.com/d/lnmpr2.html
版权声明:本网站所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 国际协议,转载或引用请署名作者并注明出处。
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK