安裝 Nginx
# rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm# yum install nginx設定
Nginx 相關檔案位置:
所有設定檔:/etc/nginx/
主要設定檔:/etc/nginx/nginx.conf
預設設定檔:/etc/nginx/conf.d/default.conf
程序設定檔:/usr/sbin/nginx
log 檔:/var/log/nginx/
主要設定檔
Nginx 服務的主要設定檔
# 啟用 Nginx 的 Linux 帳戶user nginx;# 啟用的執行緒數量(建議搭配 CPU 核心數 * 2)worker_processes 1;# log 檔位置error_log /var/log/nginx/error.log warn;pid /var/run/nginx.pid;events { # 允許同一時間連線的數量 worker_connections 1024;}http { include /etc/nginx/mime.types; default_type application/octet-stream; # 預設 log 記錄的格式 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 檔位置 access_log /var/log/nginx/access.log main; sendfile on; #tcp_nopush on; keepalive_timeout 65; # 是否啟用 gaip 壓縮(預設為註解,不啟用) #gzip on; # 載入該路徑的所有設定檔,通常就是每個虛擬主機的配置 include /etc/nginx/conf.d/*.conf; # 設定可上傳最大檔案容量(依需求設定) client_max_body_size 5m;}虛擬主機設定檔
Nginx 預設的主機設定檔
server { # 這個主機的 Port listen 80; # 這個主機的名稱 server_name localhost; # 設定預設編碼,但通常都是由網頁 <meta> 來定義,因此預設註解 #charset koi8-r; # 針對這個主機的 log 檔位置 #access_log /var/log/nginx/log/host.access.log main; # html 檔 location / { # 網站的根目錄位置 root /usr/share/nginx/html; # 使用「瀏覽器」瀏覽根目錄時,未指定檔名時預設使用的檔案 index index.html index.htm; } # 發生 404 指定導向哪個網頁 #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # # php 檔 #location ~ \.php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #}}服務設置
啟用服務並開機自動啟用
# systemctl start nginx
# systemctl enable nginx
防火牆
設定 firewall 允許 http(80 Port)、https(443 Port)封包通行
# firewall-cmd --permanent --zone=public --add-service=http# firewall-cmd --permanent --zone=public --add-service=https# firewall-cmd --reload檢查 firewall 的設定
# firewall-cmd --zone=public --list-allpublic (default, active) interfaces: eth0 eth1 sources: services: dhcpv6-client http https ssh ports: masquerade: no forward-ports: icmp-blocks: rich rules:使用遞迴來讓所有的目錄加入 SELinux 的 httpd_sys_content_t 文件類型。
chcon -R -t httpd_sys_rw_content_t /home/web/html
參考來源:https://smalljacky.com/linux/centos/centos7-nginx-mariadb-php-phpmyadmin/