環境為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