修改hosts文件php
cd /private/etc vim hosts
顯示nginx
## # Host Database # # localhost is used to configure the loopback interface # when the system is booting. Do not change this entry. ## 127.0.0.1 localhost 255.255.255.255 broadcasthost ::1 localhost 127.0.0.1 www.test.com 127.0.0.1 tpshop.test.com
進入apacheapache
cd /etc/apache2/
修改httpd.conf,沒有權限vim
sudo chmod 777 httpd.conf
把包含vhost那兩行前的#號去掉,讓extra下的擴展文件生效
給予權限
找到
Require all denied修改成
Require all granted
有兩處須要修改瀏覽器
配置域名
進入extra文件夾,編輯httpd-vhosts.conf服務器
cd extra
vi httpd-vhosts.conf
在最後處添加本身的域名網絡
<VirtualHost *:80> DocumentRoot "/Users/wxx/PhpstormProjects/test" ServerName www.test.com ErrorLog "/private/var/log/apache2/www.test.com-error_log" CustomLog "/private/var/log/apache2/www.test.com-access_log" common </VirtualHost>
進入 Nginx 的安裝目錄,我本地的安裝目錄是 /usr/local/etc/nginx/ ,打開該目錄下的 nginx.conf 文件。併發
對 nginx.conf 文件進行配置:app
# 在配置文件的頂級main部分,表明worker角色的工做進程的個數 worker_processes 1; # 錯誤日誌 error_log /usr/local/var/log/nginx/error.log warn; # 進程文件 pid /usr/local/var/run/nginx.pid; events { # 寫在events部分。每個worker進程能併發處理(發起)的最大鏈接數 worker_connections 256; } http { # 文件擴展名與文件類型映射表 include mime.types; # 設定默認文件類型 default_type application/octet-stream; # 爲Nginx服務器設置詳細的日誌格式 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 記錄了哪些用戶,哪些頁面以及用戶瀏覽器、ip和其餘的訪問信息 # access log 路徑 access_log /usr/local/var/log/nginx/access.log main; # 若是port_in_redirect爲off時,那麼始終按照默認的80端口;若是該指令打開,那麼將會返回當前正在監聽的端口。 port_in_redirect off; # 開啓高效文件傳輸模式,sendfile指令指定nginx是否調用sendfile函數來輸出文件,對於普通應用設爲 on,若是用來進行下載等應用磁盤IO重負載應用,可設置爲off,以平衡磁盤與網絡I/O處理速度,下降系統的負載。注意:若是圖片顯示不正常把這個改爲off。 sendfile on; # 配置信息文件夾,如 Nginx 安裝目錄不一樣請自行修改 include /usr/local/etc/nginx/conf.d/*.conf; }
根據上面的配置能夠看出,咱們把對 server 的配置統一放置在 conf.d 文件夾中。函數
首先先在 Nginx 安裝目錄下建立conf.d文件夾:
cd /usr/local/etc/nginx/ sudo mkdir conf.d
在文件夾 conf.d 建立默認配置文件 default.conf:
cd conf.d sudo vim default.conf
對 default.conf 進行以下配置:
# 虛擬主機配置 server { # 監聽端口 listen 80; # 域名設定,能夠有多個 server_name localhost; root /Users/www/test/; # 該項要修改成你準備存放相關網頁的路徑 location / { # 定義路徑下默認訪問的文件名 index index.php; # 打開目錄瀏覽功能,能夠列出整個目錄 autoindex on; } #proxy the php scripts to php-fpm location ~ \.php$ { # fastcgi配置 include /usr/local/etc/nginx/fastcgi.conf; # 指定是否傳遞4xx和5xx錯誤信息到客戶端 fastcgi_intercept_errors on; # 指定FastCGI服務器監聽端口與地址,能夠是本機或者其它 fastcgi_pass 127.0.0.1:9000; } }
測試nginx配置是否成功
Nginx默認監聽的端口是8080,在上述配置中我把它改爲監聽80端口。
配置的虛擬主機指向 /Users/www/test/ 目錄,我在這個目錄下新建了一個 index.php 文件,用來測試 Nginx 是否已配置成功。
index.php 文件內容以下:
test more index.php <?php echo "hello Nginx"; ?>
使用以下命令重啓 Nginx:
sudo nginx -s reload