LNMP服务器搭建
Linux(CentOs)
- 安装nginx
yum -y install nginx
- 安装mysql mysql_install.sh
#!/bin/bash
COMMON_RPM='mysql-community-common.rpm'
LIBS_RPM='mysql-community-libs.rpm'
CLIENT_RPM='mysql-community-client.rpm'
SERVER_RPM='mysql-community-server.rpm'
# 腾讯云服务器,使用内网流量不占用公网流量
# DOMAIN_NAME='mirrors.tencentyun.com'
# 外网域名
DOMAIN_NAME='mirrors.cloud.tencent.com'
# 下载镜像文件
wget -O "${COMMON_RPM}" "https://${DOMAIN_NAME}/mysql/yum/mysql-5.7-community-el7-x86_64/mysql-community-common-5.7.32-1.el7.x86_64.rpm" \
&& wget -O "${LIBS_RPM}" "https://${DOMAIN_NAME}/mysql/yum/mysql-5.7-community-el7-x86_64/mysql-community-libs-5.7.32-1.el7.x86_64.rpm" \
&& wget -O "${CLIENT_RPM}" "https://${DOMAIN_NAME}/mysql/yum/mysql-5.7-community-el7-x86_64/mysql-community-client-5.7.32-1.el7.x86_64.rpm" \
&& wget -O "${SERVER_RPM}" "https://${DOMAIN_NAME}/mysql/yum/mysql-5.7-community-el7-x86_64/mysql-community-server-5.7.32-1.el7.x86_64.rpm" \
&& yum -y remove mariadb* \
&& yum -y remove mysql* \
&& yum -y install libaio numactl \
&& rpm -ivh "${COMMON_RPM}" "${LIBS_RPM}" "${CLIENT_RPM}" "${SERVER_RPM}"
- 安装php7.2 php72w_install.sh
#!/bin/bash
rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm \
&& rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm \
&& yum -y install php72w \
&& yum -y install php72w-cli \
php72w-common \
php72w-devel \
php72w-bcmath \
php72w-fpm \
php72w-gd \
php72w-imap \
php72w-ldap \
php72w-mbstring \
php72w-mysqlnd \
php72w-odbc \
php72w-opcache \
php72w-pdo \
php72w-pear \
php72w-process \
php72w-xmlrpc \
&& php -v \
&& sed -i 's/;date.timezone =/date.timezone = PRC/' /etc/php.ini \
&& curl -sS https://getcomposer.org/installer | php \
&& mv composer.phar /usr/local/bin/composer
Nginx
- 默认配置
# nginx.conf 文件
...
# 日志打印请求响应时间
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" '
'$request_time $upstream_response_time';
...
...
开启gzip
gzip on;
gzip_min_length 5k;
gzip_buffers 4 16k;
#gzip_http_version 1.0;
gzip_comp_level 3;
gzip_types text/plain application/javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
gzip_vary on;
include /etc/nginx/conf.d/*.conf; //引入域名配置文件
...
-
域名证书
-
域名配置
# *.conf
...
端口
listen 80;
域名
server_name domain;
#根目录
root /home/wwwroot;
#默认主文件
index index.html index.htm index.php;
#php请求重定向
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass 127.0.0.1:9000; //转发
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
...
...
#开启ssl配置快
ssl_certificate cert/3047424_groupstreet.gzqidong.cn.pem;
ssl_certificate_key cert/3047424_groupstreet.gzqidong.cn.key;
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
...
-
测试配置
nginx -t
-
启动
systemctl start nginx
-
开机自启
systemctl enable nginx
Mysql
- 配置
# /etc/my.cof
...
[mysqld]
# 确保文件夹权限
datadir=/var/lib/mysql
# 尽量修改socket文件
# 修改后请与 [client] 和 php配置文件 同步
# [client] socket=文件地址
# php.ini
# mysqli.default_socket="文件地址"
# pdo_mysql.default_socket="文件地址"
socket=/var/lib/mysql/mysql.sock
# 最大连接数
max_connections = 1000
# 考虑高并发场景下的容错,建议加大。
max_connect_errors = 1000
# 密码永不过期 (5.7.9版本前默认是1年)
default_password_lifetime=0
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
log-error=/var/log/mysql/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
innodb_buffer_pool_size = 1024M
# 开启binlog日志
server-id=1
log_bin=master-bin
#设置日志格式
binlog_format = mixed
#设置日志路径,注意路经需要mysql用户有权限写
log-bin = /var/log/mysql/mysql-bin.log
#设置binlog清理时间
expire_logs_days = 7
#binlog每个日志文件大小
max_binlog_size = 100m
#binlog缓存大小
binlog_cache_size = 4m
#最大binlog缓存大小
max_binlog_cache_size = 512m
# 开启慢查询
slow_query_log = 1
# 开启慢查询时间,此处为1秒,达到此值才记录数据
long_query_time = 2
# 检索行数达到此数值,才记录慢查询日志中
min_examined_row_limit = 10000
# mysql 5.6.5新增,用来表示每分钟允许记录到slow log的且未使用索引的SQL语句次数,默认值为0,不限制。
log_throttle_queries_not_using_indexes = 0
# 慢查询日志文件地址
slow_query_log_file = /var/log/mysql/mysql-slow.log
# 开启记录没有使用索引查询语句
log-queries-not-using-indexes = 0
...
- 获取初始密码
grep password /var/log/mysql/mysqld.log
- 修改密码
# 注意密码策略
set password=password('新密码');
-
启动
systemctl start mysqld
-
开机自启
systemctl enable mysqld
PHP
- 配置
# /etc/php.ini
...
# 隐藏php版本信息
expose_php = Off
# 最大写入时间
max_input_time = 60
# 内存最大限制
memory_limit = 128M
# Default Value: E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED
error_reporting = E_ALL & ~E_NOTICE
# 错误显示( 线上关闭错误显示, 开始错误日志 )
display_errors = Off
# 错误日志
log_errors = On
# 日志文件
error_log = php_errors.log
# 最大post提交
post_max_size = 8M
# 上传文件最大限制 ( 与post_max_size同时使用 )
upload_max_filesize = 2M
# 上传最大文件数量
max_file_uploads = 20
# 默认请求超时时间
default_socket_timeout = 60
...
- php-fpm配置
# /etc/php-fpm.conf
...
process_control_timeout = 20
...
# /etc/php-fpm.d/www.conf
...
# 慢日志文件地址
slowlog = /var/log/php-fpm/$pool.slow.log
# 慢日志时间
request_slowlog_timeout = 2
# 默认请求超时时间
request_terminate_timeout = 60
# session文件夹目录
php_value[session.save_path] = /var/lib/php/session
...
-
测试配置
php-fpm -t
-
启动
systemctl start php-fpm
-
开机自启
systemctl enable php-fpm
其他
- 常用工具
yum -y install screen htop ftp lftp unzip lrzsz
经年痴心妄想,一朝走火入魔。