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

使用云服务器搭建 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 文件,并做如下配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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 文件,并做如下配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
[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 文件,并做如下配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
[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 添加开放端口

1
2
3
4
5
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:

1
2
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 这一行。

相关文章

友情提示:请勿采取不友好的评论或进行过激的争吵,禁止任何淫秽、反动、暴力、赌博、恐吓或违法的内容。对此,本站将保留删除评论的权利。
0 条评论
😂暂无评论,快来抢沙发吧~