Linux 典型應用之WebServer 安裝和配置

 Apache的基本操做

 安裝php

yum install httpd

啓動html

service httpd start 

在瀏覽器中輸入如下Ip 發現沒法訪問linux

http://192.168.1.109/

輸入如下命令進行網絡統計nginx

 netstat -anpl | grep 'http'

 發現此時80端口和httpd都已經啓動了,這時候只須要把防火牆關閉便可centos

 service firewalld stop

再次刷新頁面,啓動成功  瀏覽器

中止  服務器

service httpd stop

Apache的虛擬主機配置及僞靜態操做 

虛擬主機通常在配置多個域名的時候纔會使用,好比一個服務器上部署多個網站,保證每一個域名訪問的內容和源代碼是不同的。網絡

 進入httpd目錄負載均衡

cd /etc/httpd/

ls

打開conf/httpd.confide

cd conf 

vi httpd.conf
  • DocumentRoot "/var/www/html"  表示文件根目錄的路徑(代碼目錄都是放在這裏的),配置虛擬主機的時候咱們能夠本身指定代碼目錄
  • port 80 指的是監聽的端口是80

配置虛擬主機 

<VirtualHost *:80>
        ServerName www.zhangbiao.com
        DocumentRoot /data/www
</VirtualHost>

 

建立/data/www 文件 ,由於把跟文件的目錄指定到了/data/www

sudo mkdir -p  /data/www

  

在data/www 目錄下建立index.html 文件

cd /data/www

vi index.html  

輸入如下內容

<h1> hello world </h1>

重啓httpd服務

service httpd restart

 

配置 hosts  linux hosts 路徑  /etc/hosts   windos hosts路徑  C:\Windows\System32\drivers\etc

192.168.1.109 www.zhangbiao.com

在瀏覽器輸入

www.zhangbiao.com

 

 發現頁面顯示的並非咱們輸入的 hello world

查看log 日誌

cd /etc/httpd/logs/
ls -al

查看訪問日誌

tail -f access_log

 

查看錯誤日誌

tail -f error_log

在瀏覽器訪問

www.zhangbiao.com

錯誤日誌中出現如下提示信息

vi  /etc/httpd/conf/httpd.conf

在上面配置虛擬主機的代碼段添加如下信息

<VirtualHost *:80>
        ServerName www.zhangbiao.com
        DocumentRoot /data/www
        <Directory "/data/www">
                Options Indexes FollowSymLinks
                AllowOverride None
                Require all granted
        </Directory>
</VirtualHost>

再次刷新頁面

 

須要注意的是若是不能訪問請進行如下檢查 

DocumentRoot 和 Directory 中的字符串必須相等

data , www 必須是可執行的權限 755

selinux的問題 ,你能夠把你的目錄進行一下selinux權限設置 

sudo setenforce 0

這條命令只是臨時的,若是想要永久有效能夠修改其配置文件

vi /etc/selinux/config 

把 SELINUX=disabled

配置多個虛擬主機

只須要照着上面配置一個虛擬主機進行如下配置便可,在/etc/httpd/conf/httpd.cof

<VirtualHost *:80>
        ServerName www.zhangbiao.com
        DocumentRoot /data/www
        <Directory "/data/www">
                Options Indexes FollowSymLinks
                AllowOverride None
                Require all granted
        </Directory>
</VirtualHost>

<VirtualHost *:80>
        ServerName www.zhangbiao1.com
        DocumentRoot /data/www1
        <Directory "/data/www1">
                Options Indexes FollowSymLinks
                AllowOverride None
                Require all granted
        </Directory>
</VirtualHost>

在建立 /data/www1目錄再起www1下建立index.html便可

Apach僞靜態

僞靜態是基於 rewrite 實現的

在 /etc/httpd/conf/httpd.cof 添加如下配置 

LoadModule rewrite_module modules/rewrite.so

 

<VirtualHost *:80>
        ServerName www.zhangbiao.com
        DocumentRoot /data/www
        <Directory "/data/www">
                Options Indexes FollowSymLinks
                AllowOverride None
                Require all granted
        <IfModule mod_rewrite.c>
                RewriteEngine On
                RewriteRule ^(.*).htmp$ index.html
        </IfModule>
        </Directory>
</VirtualHost>

 

重啓服務

service httpd restart

訪問已 htmp結尾的都會轉到index.html文件

 http://www.zhangbiao.com/1.htmp

 

 Nginx的基本操做

安裝

yum install nginx
發現沒法安裝成功

須要安裝 Nginx 源

sudo rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm

安裝該rpm後,咱們就能在/etc/yum.repos.d/ 目錄中看到一個名爲nginx.repo 的文件

再次安裝

yum install nginx

啓動

service nginx start

訪問

http://192.168.1.109/

 

中止

service nginx stop

重載,通常都有reload無負載的遷移,由於要用restart從新啓動的時候,須要關閉程序在程序啓動,須要必定的時間

service nginx reload

nginx 的配置文件存放在 /etc/nginx 下面 

 nginx 配置虛擬主機

打開 配置文件 nginx,conf

vi  /etc/nginx/nginx.conf  

能夠看到會默認加載/etc/nginx/conf.d/ 下面已conf結尾的文件 

進入 /etc/nginx/conf.d/ 下面

cd  /etc/nginx/conf.d/ 

ls

 查看 default.conf  文件

cat default.conf 

 

查看  /usr/share/nginx/html 下的文件

ll /usr/share/nginx/html

 

把default.conf 複製一份到zhang.conf

cp default.conf zhang.conf

內容以下

server {
    listen       80;
    server_name  www.zhangbiao.com;
    root   /data/www;
    index  index.html index.htm;

}

訪問

http://www.zhangbiao.com/

nginx配置多個端口和域名訪問

在zhangbiao.conf 中配置多監聽一個端口和servername以下

server {
    listen       80;
    listen       9999;
    server_name  www.zhangbiao.com www.zhangtao.com;
    root   /data/www;
    index  index.html index.htm;

}

 

配置hosts

 

重啓nginx

service nginx restart

訪問新增的端口

http://www.zhangbiao.com:9999

 訪問新填的域名

http://www.zhangtao.com/

 

 Nginx 僞靜態的實現

修改zhang.conf 增長location配置

server {
    listen       80;
    listen       9999;
    server_name  www.zhangbiao.com www.zhangtao.com;
    root   /data/www;
    index  index.html index.htm;
    location / {
	rewrite ^(.*)\.htmp$ /index.html;
    }

}

  

重啓Nginx

service nginx restart

訪問

http://www.zhangtao.com/1.htmp

 

Nginx 日誌格式化

打開  /etc/nginx/nginx.conf

vi /etc/nginx/nginx.conf  

 

 添加自定義的log_format 和修改access_log 關鍵字

    log_format  zhang  '$remote_addr - "$http_user_agent"';

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

 

重寫加載nginx

service nginx reload  

 打開日誌文件存儲的路徑

tail -f /var/log/nginx/access.log

 訪問

http://www.zhangbiao.com/

能夠看到日誌信息以下

 

 咱們能夠把不一樣的虛擬主機存儲各自的 log 文件 ,在 /etc/nginx/conf.d/zhang.conf ,添加如下日誌配置

access_log  /var/log/nginx/access_zhang.log zhang;

 

重啓 Nginx 服務

service nginx reload

訪問

http://www.zhangbiao.com/

這時在 /var/log/nginx 下多了 access_zhang.log 文件

 

更多的nginx log_formt 配置能夠百度檢索

nginx log_formt

Nginx 反向代理 和 負載均衡 

反向代理

訪問  www.zhangbiao.com  進行反向代理的個人博客頁面

 進入 /etc/nginx/conf.d 目錄

編輯 zhang.conf 

upstream imooc_hosts{
    server 118.89.106.129:80;
}



server {
    listen       80;
    listen       9999;
    server_name  www.zhangbiao.com www.zhangtao.com;
    root   /data/www;
    index  index.html index.htm;
    access_log  /var/log/nginx/access_zhang.log zhang;

    location / {
        #rewrite ^(.*)\.htmp$ /index.html;
        proxy_set_header Host www.54php.cn;
        proxy_pass http://118.89.106.129;
    }

}

重啓

service nginx reload

訪問

http://www.zhangbiao.com/

 

 負載均衡

主要做用是減小網站的壓力,進行分流

訪問同一個url 第一次是server1 返回的結果,第二次是server2返回的結果 

進入 /etc/nginx/conf.d 目錄

編輯 zhang.conf  加入如下配置

upstream zhang_hosts{
    server 118.89.106.129:80;
    server 192.168.1.109;
}



server {
    listen       80;
    listen       9999;
    server_name  www.zhangbiao.com www.zhangtao.com;
    root   /data/www;
    index  index.html index.htm;
    access_log  /var/log/nginx/access_zhang.log zhang;

    location / {
        #rewrite ^(.*)\.htmp$ /index.html;
        proxy_set_header Host www.54php.cn;
        proxy_pass http://zhang_hosts;
    }

}

 

擴展 配置weight 權重,訪問5次server1 才切換到server2

upstream zhang_hosts{
    server 118.89.106.129:80 weight=5;
    server 192.168.1.109 weight=1;
}

 

重啓

service nginx reload

訪問

www.zhangbiao.com

 

 再次刷新

 

 Nginx 調試

 有時咱們對Nginx 配置出錯的時候,咱們能夠逐行的進行調試找到錯誤出現的位置

Nginx 能夠經過 return 關鍵字 返回請求的域名這個能夠本身設置 ,返回成功即以上的代碼沒問題

在 /etc/nginx/conf.d/zhang.conf

添加如下代碼

    add_header Content-Type "text/plain;charset=utf-8";
    return 200 "$http_host";

 

訪問

http://www.zhangbiao.com/

返回  

 

訪問

http://www.zhangtao.com/

 

 

 

反向代理轉向不一樣的機器,nginx 報 502 的錯誤

能夠嘗試輸入如下命令

setsebool -P httpd_can_network_connect 1
相關文章
相關標籤/搜索