超详解的LNMP搭建并优化

环境为Centos7 nginx1.14 mysql5.7 php7
一,安装Nginx (yum装,快速)
yum install nginx
二,优化nginx (方便后期工作,如果纯为测试的话,不用)
1, cat nginx.conf

user www;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

include /usr/lib/nginx/modules/*.conf;

events {
worker_connections 2048;
}

http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" "$http_host" ';

log_format json '{"@timestamp":"$time_iso8601",'
'"clientip":"$http_x_forwarded_for",'
'"remote_user":"$remote_user",'
'"request":"$request",'
'"method":"$request_method",'
'"status":"$status",'
'"code":"$status",'
'"size":"$body_bytes_sent",'
'"referer":"$http_referer",'
'"agent":"$http_user_agent",'
'"host":"$http_host",'
'"remote_addr":"$remote_addr",'
'"responsetime":"$request_time",'
'"upstreamtime":"$upstream_response_time",'
'"url":"$uri",'
'"request_filename":"$request_filename",'
'"index":"$fastcgi_script_name"}';

access_log /var/log/nginx/access.log json;

sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;

include /etc/nginx/mime.types;
default_type application/octet-stream;

include /etc/nginx/conf.d/*.conf;


}

2, cat conf.d/optimize.conf (把优化参数写到这里)

server_tokens off; #并不会让nginx执行的速度更快,关闭它可隐藏错误页面中的nginx版本号
charset utf-8,gbk; #字符
#sendfile on;
#tcp_nopush on; #在一个数据包里发送所有头文件,而不一个接一个的发送
#tcp_nodelay on; #不缓存数据,而是一段一段的发送
#keepalive_timeout 65; #给客户端分配keep-alive链接超时时间,服务器将在这个超时时间过后关闭链接,将它设置低些可以让ngnix持续工作的时间更长

autoindex off; #开启或者关闭列出目录中文件的功能
autoindex_exact_size off; #默认为 on,以 bytes 为单位显示文件大小;切换为 off 后,以可读的方式显示文件大小,单位为 KB、MB 或者 GB
autoindex_localtime on; #默认为 off,以 GMT 时间作为显示的文件时间;切换为 on 后,以服务器的文件时间作为显示的文件时间

large_client_header_buffers 8 12k;
client_max_body_size 2000m; #文件限制大小
client_header_buffer_size 128k;
client_body_buffer_size 256k;
client_body_timeout 1200s; #请求体的超时时间
client_header_timeout 1200s; #请求头的超时时间
send_timeout 1200s; #指定客户端的响应超时时间,如果在这段时间内,客户端没有读取任何数据,nginx就会关闭连接。
reset_timedout_connection on; #关闭不响应的客户端连接。这将会释放那个客户端所占有的内存空间

fastcgi_buffer_size 256k;
fastcgi_buffers 16 256k;
fastcgi_busy_buffers_size 512k;
fastcgi_temp_file_write_size 512k;
fastcgi_connect_timeout 900s; #链接
fastcgi_read_timeout 1800s; #读取;是指fastcgi进程向nginx进程发送response的整个过程的超时时间
fastcgi_send_timeout 1800s; #发请求;是指nginx进程向fastcgi进程发送request的整个过程的超时时间
fastcgi_intercept_errors off;

open_file_cache max=100000 inactive=20s; # 打开缓存的同时也指定了缓存最大数目,以及缓存的时间
open_file_cache_valid 300s; # 在open_file_cache中指定检测正确信息的间隔时间
open_file_cache_min_uses 2; #open_file_cache中指令参数不活动时间期间里最小的文件数
open_file_cache_errors on;

proxy_connect_timeout 600; #说明该指令设置与upstream server的连接超时时间,有必要记住,这个超时不能超过75秒
proxy_read_timeout 600; #说明该指令设置与代理服务器的读超时时间。它决定了nginx会等待多长时间来获得请求的响应。这个时间不是获得整个response的时间,而是两次reading操作的时间
proxy_send_timeout 600;
proxy_buffer_size 64k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;

gzip on; #告诉nginx采用gzip压缩的形式发送数据,这将会减少我们发送的数据量
gzip_comp_level 7; #压缩级别,1-9,数字越大压缩的越好,时间也越长
gzip_min_length 1024; #不压缩临界值,大于1024的才压缩
gzip_buffers 4 16k; #用于压缩缓存
gzip_types text/plain application/x-javascript text/css text/javascript text/xml image/x-icon image/bmp; #压缩级别,1-9,数字越大压缩的越好,时间也越长
gzip_vary on;
gzip_static on;
gzip_proxied any; # 压缩所有的请求
gzip_disable "MSIE [1-6].";

cat conf.d/default.conf

server {
listen 80;
root /var/www/html;
index index.html index.htm index.php;
server_name _;
error_page 500 502 503 504 /50x.html;

location = /50x.html {
root /var/lib/nginx/html;
}

location ~ .php$ {
root /usr/share/nginx/html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
此处的php设置可根据情况而定

启动nginx
systcemctl start nginx

开机自启
systcemctl enable nginx

三 、安装MySql(5.7)

centos7默认不支持mysql,默认支持的是mariadb,mariadb是mysql一个开源分支。

1、卸载mariadb,否则安装mysql会出现冲突

执行命令
rpm -qa | grep mariadb 列出所有被安装的mariadb rpm 包;

执行命令
rpm -e --nodeps 包名称(比如:rpm -e --nodeps mariadb-libs-5.5.44-2.el7.centos.x86_64) 逐个将所有列出的mariadb rpm 包给卸载掉

2、添加官方的yum源:
以centos7安装mysql5.6为例:
vim /etc/yum.repos.d/mysql-community.repo
[mysql56-community]

name=MySQL 5.6 Community Server

baseurl=http://repo.mysql.com/yum/mysql-5.6-community/el/7/$basearch/

enabled=1

gpgcheck=0

gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql

centos7安装mysql5.7:baseurl=http://repo.mysql.com/yum/mysql-5.7-community/el/7/$basearch/

centos6安装mysql5.6:baseurl=http://repo.mysql.com/yum/mysql-5.6-community/el/6/$basearch/

centos6安装mysql5.7:baseurl=http://repo.mysql.com/yum/mysql-5.7-community/el/6/$basearch/

3、安装MySql
yum install mysql-community-server

4、启动
systcemctl start mysql

6,配置密码:
mysqladmin -u root -p password "12345566"
初始密码为空,直接按回车即可
注意:mysql5.7的初始密码是随机生成的,放在了 /var/log/mysqld.log
使用命令 grep ‘temporary password’ /var/log/mysqld.log 读出来即可。

7、配置权限
mysql> grant all on *.* to 'root'@'%' identified by '12345566';

四、安装php7

1、检查当前安装的PHP包
yum list installed | grep php
如果有,就删除 yum remove

2、安装
#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
如果想删除,重新装:
rpm -qa | grep webstatic
rpm -e 上面搜索到的包即可

#yum install php70w.x86_64 php70w-cli.x86_64 php70w-common.x86_64 php70w-gd.x86_64 php70w-ldap.x86_64 php70w-mbstring.x86_64 php70w-mcrypt.x86_64 php70w-mysql.x86_64 php70w-pdo.x86_64
如果想安装更多的扩展,继续加其它的

安装php-fpm
yum install php70w-fpm php70w-opcache

3、设置php-fpm启动用户
因为要和nginx通信,所以要和nginx的启动用户一样,
user=nginx
group=nginx


4、启动php-fpm
systcemctl start php-fpm
设置开机自启:
#systemctl enable php-fpm
#systemctl daemon-reload

原文地址:https://www.cnblogs.com/luoyan01/p/9734145.html