Nginx的安裝
- 安裝快速HTTP服務器“的Nginx”並配置HTTP服務器
# install from EPEL
[root@linuxprobe~]# yum --enablerepo=epel -y install nginx # 基礎設置 [root@linuxprobe~]# vi /etc/nginx/nginx.conf # line 40: change hostname server_name linuxprobe.org; [root@linuxprobe ~]# systemctl start nginx 啟動nginx [root@linuxprobe ~]# systemctl enable nginx 開機自啟動nginx
[root@linuxprobe ~]# systemctl restart nginx 重啟nginx
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service. [root@linuxprobe ~]# cat /etc/hosts 127.0.0.1 localhost localhost.localdomain linuxprobe.org 10.1.1.56 vdevops.com # 開啟防火牆 [root@linuxprobe ~]# firewall-cmd --add-service=http --permanent
提示FirewallD is not running
https://jingyan.baidu.com/article/5552ef47f509bd518ffbc933.html
success [root@linuxprobe ~]# firewall-cmd --reload success
下面給大家上一個配置文件,作為理解,同時也配入我搭建的一台測試機中,給大家示例。
########### 每個指令必須有分號結束。#################
#user administrator administrators; #配置用戶或者組,默認為nobody nobody。
#worker_processes 2; #允許生成的進程數,默認為1
#pid /nginx/pid/nginx.pid; #指定nginx進程運行文件存放地址
error_log log/error.log debug; #制定日志路徑,級別。這個設置可以放入全局塊,http塊,server塊,級別以此為:debug|info|notice|warn|error|crit|alert|emerg
events {
accept_mutex on; #設置網路連接序列化,防止驚群現象發生,默認為on
multi_accept on; #設置一個進程是否同時接受多個網絡連接,默認為off
#use epoll; #事件驅動模型,select|poll|kqueue|epoll|resig|/dev/poll|eventport
worker_connections 1024; #最大連接數,默認為512
}
http {
include mime.types; #文件擴展名與文件類型映射表
default_type application/octet-stream; #默認文件類型,默認為text/plain
#access_log off; #取消服務日志
log_format myFormat '$remote_addr–$remote_user [$time_local] $request $status $body_bytes_sent $http_ referer $http_user_agent $http_x_forwarded_for'; #自定義格式
access_log log/access.log myFormat; #combined為日志格式的默認值
sendfile on; #允許sendfile方式傳輸文件,默認為off,可以在http塊,server塊,location塊。
sendfile_max_chunk 100k; #每個進程每次調用傳輸數量不能大於設定的值,默認為0,即不設上限。
keepalive_timeout 65; #連接超時時間,默認為75s,可以在http,server,location塊。
upstream mysvr {
server 127.0.0.1:7878;
server 192.168.10.121:3333 backup; #熱備
}
error_page 404 https://www.baidu.com; #錯誤頁
server {
keepalive_requests 120; #單連接請求上限次數。
listen 4545; #監聽端口
server_name 127.0.0.1; #監聽地址
location ~*^.+$ { #請求的url過濾,正則匹配,~為區分大小寫,~*為不區分大小寫。
#root path; #根目錄
#index vv.txt; #設置默認頁
proxy_pass http://mysvr; #請求轉向mysvr 定義的服務器列表
deny 127.0.0.1; #拒絕的ip
allow 172.18.5.54; #允許的ip
}
}
}
- 客戶端設置主機,從瀏覽器訪問linuxprobe.org
虛擬主機設置
- 配置nginx的
[root@linuxprobe ~]# vi /etc/nginx/conf.d/linuxcool.com.conf # create new server { listen 80; server_name linuxcool.com; location / { root /usr/share/nginx/linuxcool; index index.html index.htm; } } [root@linuxprobe ~]# mkdir /usr/share/nginx/linuxcool [root@linuxprobe w ~]# systemctl restart nginx
- 創建測試頁面
[root@linuxprobe ~]# vi /usr/share/nginx/
linuxcool
/index.html
<html>
<body>
<div style="width: 100%; font-size: 40px; font-weight: bold; text-align: center;"> Nginx LinuxCool Test Page 測試完成 </div>
</body>
</html>
Nginx && PHP-FPM
- 安裝PHP-FPM解析PHP頁面
[root@linuxprobe ~]# yum --enablerepo=epel -y install php php-mbstring php-pear php-fpm
- 配置配置PHP-FPM和Ngin
[root@linuxprobe ~]# vi /etc/php-fpm.d/www.conf # line 39: change user = nginx # line 41: change group = nginx [root@linuxprobe ~]# systemctl start php-fpm [root@linuxprobe ~]# systemctl enable php-fpm [root@linuxprobe ~]# vi /etc/nginx/nginx.conf # add into "server" section location ~ \.php$ {
root /usr/share/nginx/linuxcool;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/share/nginx/linuxcool$fastcgi_script_name;
include fastcgi_params;
}
[root@linuxprobe ~]# systemctl restart nginx
- 創建PHP測試頁
[root@www ~]# echo "<?php phpinfo() ?>" > /usr/share/nginx/
linuxcool
/info.php
- 端的客戶訪問https://IP地址/info.php
http://blog.csdn.net/wh211212/article/details/53018112
配置nginx視頻
http://v.youku.com/v_show/id_XMjgwOTU2MDM1Mg==.html
cd /etc/nginx/
mv nginx.conf nginx.conf.adc
cp nginx.conf.default nginx.conf
:set number
更改44行 root html;
root /www/data; #項目訪問根目錄
更改45行 index indhtml index.htm;
index indhtml index.htm index.php; #可訪問文件名
支持php
去除65-71行前面的#
65 # location ~ \.php$ {
66 # root html;
67 # fastcgi_pass 127.0.0.1:9000;
68 # fastcgi_index index.php;
69 # fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
70 # include fastcgi_params;
71 #}
修改66行 root /www/data;
修改69行 fastcgi_param SCRIPT_FILENAME /www/data$fastcgi_script_name;