使用雲服務器搭建 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 這一行。