Linux基础服务

作业一:nginx服务
1、二进制安装nginx包

[root@bogon ~]# systemctl disable firewalld  #关闭Firewalld自启动
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
Removed symlink /etc/systemd/system/basic.target.wants/firewalld.service.
[root@bogon ~]# systemctl stop firewalld  #关闭firewalld
[root@bogon ~]# yum install nginx -y  #安装nginx服务 

2、作为web服务修改配置文件

[root@bogon ~]# egrep -v "^$|^#|#"  /etc/nginx/nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
include /usr/share/nginx/modules/*.conf;
events {
    worker_connections 1024;
}
http {
    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  /var/log/nginx/access.log  main;
    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;
    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;
        root         /usr/share/nginx/html;
        include /etc/nginx/default.d/*.conf;
        location / {
                root /data/www/html;
                index index.html;
        }
        error_page 404 /404.html;
            location = /40x.html {
        }
        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }
}

[root@bogon ~]# mkdir -p /data/www/html
[root@bogon ~]# echo "welcome oldgirl" > /data/www/html/index.html

3、让配置生效,验证配置

[root@bogon ~]# /usr/sbin/nginx -t   #检查配置是否正确
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@bogon ~]# systemctl start nginx  #启动nginx服务

[root@bogon ~]# systemctl status nginx #查看nginx启动状态
● nginx.service - The nginx HTTP and reverse proxy server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
   Active: active (running) since Mon 2017-03-20 15:23:29 CST; 44s ago
  Process: 3072 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
  Process: 3069 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
  Process: 3068 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)
 Main PID: 3075 (nginx)
   CGroup: /system.slice/nginx.service
           ├─3075 nginx: master process /usr/sbin/nginx
           └─3076 nginx: worker process

Mar 20 15:23:29 bogon systemd[1]: Starting The nginx HTTP and reverse proxy server...
Mar 20 15:23:29 bogon nginx[3069]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
Mar 20 15:23:29 bogon nginx[3069]: nginx: configuration file /etc/nginx/nginx.conf test is successful
Mar 20 15:23:29 bogon systemd[1]: Failed to read PID from file /run/nginx.pid: Invalid argument
Mar 20 15:23:29 bogon systemd[1]: Started The nginx HTTP and reverse proxy server.

#使用浏览器验证是否可以正常访问web

作业二:nfs服务

拓扑图:

1、二进制安装nfs

[root@nfs-server ~]# yum install rpcbind nfs-utils -y #NFS服务端和客户端都装。

2、作为共享存储挂载在三台web的网站根目录下

[root@nfs-server ~]# mkdir /share  #创建挂载目录
[root@nfs-server ~]# cat /etc/exports  #配置NFS SERVER
/share 192.168.152.0/24(rw,sync,fsid=0)

# 启动开机自启:
[root@nfs-server ~]# systemctl enable nfs-server.service
Created symlink from /etc/systemd/system/multi-user.target.wants/nfs-server.service to /usr/lib/systemd/system/nfs-server.service.
[root@nfs-server ~]# systemctl enable rpcbind.service

# 启动服务(必须先启动rpcbind服务):
[root@nfs-server ~]# systemctl start rpcbind.service
[root@nfs-server ~]# systemctl start nfs-server.service

# 确定NFS服务器启动成功:
[root@nfs-server ~]# rpcinfo 
   program version netid     address                service    owner
    100000    4    tcp6      ::.0.111               portmapper superuser
    100000    3    tcp6      ::.0.111               portmapper superuser
    100000    4    udp6      ::.0.111               portmapper superuser
    100000    3    udp6      ::.0.111               portmapper superuser
    100000    4    tcp       0.0.0.0.0.111          portmapper superuser
    100000    3    tcp       0.0.0.0.0.111          portmapper superuser
    100000    2    tcp       0.0.0.0.0.111          portmapper superuser
    100000    4    udp       0.0.0.0.0.111          portmapper superuser
    100000    3    udp       0.0.0.0.0.111          portmapper superuser
    100000    2    udp       0.0.0.0.0.111          portmapper superuser
    100000    4    local     /var/run/rpcbind.sock  portmapper superuser
    100000    3    local     /var/run/rpcbind.sock  portmapper superuser
    100005    1    udp       0.0.0.0.78.80          mountd     superuser
    100005    1    tcp       0.0.0.0.78.80          mountd     superuser
    100005    1    udp6      ::.78.80               mountd     superuser
    100005    1    tcp6      ::.78.80               mountd     superuser
    100005    2    udp       0.0.0.0.78.80          mountd     superuser
    100005    2    tcp       0.0.0.0.78.80          mountd     superuser
    100024    1    udp       0.0.0.0.129.33         status     29
    100005    2    udp6      ::.78.80               mountd     superuser
    100024    1    tcp       0.0.0.0.171.119        status     29
    100024    1    udp6      ::.206.85              status     29
    100005    2    tcp6      ::.78.80               mountd     superuser
    100005    3    udp       0.0.0.0.78.80          mountd     superuser
    100024    1    tcp6      ::.207.20              status     29
    100005    3    tcp       0.0.0.0.78.80          mountd     superuser
    100005    3    udp6      ::.78.80               mountd     superuser
    100005    3    tcp6      ::.78.80               mountd     superuser
    100003    3    tcp       0.0.0.0.8.1            nfs        superuser
    100003    4    tcp       0.0.0.0.8.1            nfs        superuser
    100227    3    tcp       0.0.0.0.8.1            nfs_acl    superuser
    100003    3    udp       0.0.0.0.8.1            nfs        superuser
    100003    4    udp       0.0.0.0.8.1            nfs        superuser
    100227    3    udp       0.0.0.0.8.1            nfs_acl    superuser
    100003    3    tcp6      ::.8.1                 nfs        superuser
    100003    4    tcp6      ::.8.1                 nfs        superuser
    100227    3    tcp6      ::.8.1                 nfs_acl    superuser
    100003    3    udp6      ::.8.1                 nfs        superuser
    100003    4    udp6      ::.8.1                 nfs        superuser
    100227    3    udp6      ::.8.1                 nfs_acl    superuser
    100021    1    udp       0.0.0.0.220.240        nlockmgr   superuser
    100021    3    udp       0.0.0.0.220.240        nlockmgr   superuser
    100021    4    udp       0.0.0.0.220.240        nlockmgr   superuser
    100021    1    tcp       0.0.0.0.156.92         nlockmgr   superuser
    100021    3    tcp       0.0.0.0.156.92         nlockmgr   superuser
    100021    4    tcp       0.0.0.0.156.92         nlockmgr   superuser
    100021    1    udp6      ::.211.174             nlockmgr   superuser
    100021    3    udp6      ::.211.174             nlockmgr   superuser
    100021    4    udp6      ::.211.174             nlockmgr   superuser
    100021    1    tcp6      ::.171.11              nlockmgr   superuser
    100021    3    tcp6      ::.171.11              nlockmgr   superuser
    100021    4    tcp6      ::.171.11              nlockmgr   superuser
[root@nfs-server ~]# 
[root@nfs-server ~]# exportfs 
/share          192.168.152.0/24

# 默认使用showmount -e 查看自己共享的服务,需要DNS能解析,不然容易报错。
# 此处添加HOSTS本地解析。
[root@nfs-server ~]# echo "192.168.152.134     nfs-server" >> /etc/hosts
[root@nfs-server ~]# showmount -e  
Export list for nfs-server:
/share 192.168.152.0/24

# 客户端挂载:
[root@web01 ~]# mount -t nfs 192.168.152.134:/share /data/www/html/
[root@web02 ~]# mount -t nfs 192.168.152.134:/share /data/www/html/
[root@web03 ~]# mount -t nfs 192.168.152.134:/share /data/www/html/

[root@nfs-server ~]# showmount -a  #显示已经与客户端连接上的目录信息
All mount points on nfs-server:
192.168.152.135:/share
192.168.152.136:/share
192.168.152.137:/share

3、实现在任意一台web上修改的结果,其余两台都可以看到

# others用户添加w权限
[root@nfs-server ~]# chmod -R o+w /share/
[root@nfs-server ~]# ll -d /share/
drwxr-xrwx 2 root root 23 Mar 20 16:38 /share/

# 测试结果
[root@web01 ~]# echo "welcome luchuangao" > /data/www/html/test.html
[root@web02 ~]# cat /data/www/html/test.html 
welcome luchuangao
[root@web03 ~]# cat /data/www/html/test.html                       
welcome luchuangao

作业三:nginx反向代理三台web

 参考链接:http://nginx.org/en/docs/http/load_balancing.html

1、实现基于轮询的方式调度三台web,并验证结果

[root@nfs-server ~]# !egrep
egrep -v "^$|^#|#"  /etc/nginx/nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
include /usr/share/nginx/modules/*.conf;
events {
    worker_connections 1024;
}
http {
    upstream luchuangao {
        server 192.168.152.135:80;
        server 192.168.152.136:80;
        server 192.168.152.137:80;
    }
    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  /var/log/nginx/access.log  main;
    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;
    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;
        root         /usr/share/nginx/html;
        include /etc/nginx/default.d/*.conf;
        location / {
                proxy_pass http://luchuangao;
        }
        error_page 404 /404.html;
            location = /40x.html {
        }
        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }
}


[root@nfs-server ~]# /usr/sbin/nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@nfs-server ~]# systemctl reload nginx
[root@web01 ~]# echo "web01" > /data/www/html/index.html 
[root@web02 ~]# echo "web02" > /data/www/html/index.html  
[root@web03 ~]# echo "web03" > /data/www/html/index.html  

# 浏览器进行验证

2、实现基于权重的方式调度三台web,并验证结果

[root@nfs-server ~]# /usr/sbin/nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@nfs-server ~]# /usr/sbin/nginx -s reload

[root@nfs-server ~]# egrep -v "#|^$|^#" /etc/nginx/nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
include /usr/share/nginx/modules/*.conf;
events {
    worker_connections 1024;
}
http {
    upstream luchuangao {
        server 192.168.152.135:80 weight=3;
        server 192.168.152.136:80;
        server 192.168.152.137:80;
    }
    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  /var/log/nginx/access.log  main;
    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;
    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;
        root         /usr/share/nginx/html;
        include /etc/nginx/default.d/*.conf;
        location / {
                proxy_pass http://luchuangao;
        }
        error_page 404 /404.html;
            location = /40x.html {
        }
        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }
}

3、实现基于hash的方式调用三台web,并验证结果

[root@nfs-server ~]# /usr/sbin/nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@nfs-server ~]# /usr/sbin/nginx -s reload

[root@nfs-server ~]# egrep -v "#|^$|^#" /etc/nginx/nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
include /usr/share/nginx/modules/*.conf;
events {
    worker_connections 1024;
}
http {
    upstream luchuangao {
        ip_hash;
        server 192.168.152.135:80;
        server 192.168.152.136:80;
        server 192.168.152.137:80;
    }
    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  /var/log/nginx/access.log  main;
    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;
    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;
        root         /usr/share/nginx/html;
        include /etc/nginx/default.d/*.conf;
        location / {
                proxy_pass http://luchuangao;
        }
        error_page 404 /404.html;
            location = /40x.html {
        }
        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }
}

作业四:nginx反向代理+三台web+nfs共享存储实现集群配置

# 在各个web服务器上挂载nfs

[root@web01 ~]# mount -t nfs 192.168.152.134:/share /data/www/html/
[root@web02 ~]# mount -t nfs 192.168.152.134:/share /data/www/html/
[root@web03 ~]# mount -t nfs 192.168.152.134:/share /data/www/html/
[root@web01 ~]# echo "nfs ok" > /data/www/html/index.html 

# 浏览器访问进行验证

作业五:源码安装nginx,并按照作业一描述的那样去测试使用

#编译安装nginx

[root@nfs-server ~]# yum install gcc-* glibc-* openssl openssl-devel pcre pcre-devel zlib zlib-devel -y
[root@nfs-server ~]# wget -q http://nginx.org/download/nginx-1.10.3.tar.gz
[root@nfs-server ~]# ls nginx-1.10.3.tar.gz 
nginx-1.10.3.tar.gz
[root@nfs-server ~]# tar -xf nginx-1.10.3.tar.gz
[root@nfs-server ~]# cd nginx-1.10.3/
[root@nfs-server nginx-1.10.3]# ./configure --sbin-path=/usr/local/nginx/nginx --conf-path=/usr/local/nginx/nginx.conf --pid-path=/usr/local/nginx/nginx.pid --with-http_ssl_module
[root@nfs-server nginx-1.10.3]# make && make install

# 配置nginx.conf

[root@nfs-server ~]# cat /usr/local/nginx/nginx.conf|egrep -v "^$|^#|#"
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  localhost;
        location / {
            root   /data/www/html;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

# 验证

[root@nfs-server ~]# /usr/local/nginx/nginx -t
nginx: the configuration file /usr/local/nginx/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/nginx.conf test is successful
[root@nfs-server ~]# /usr/local/nginx/nginx  #启动nginx服务
[root@nfs-server ~]# echo 'goodbye oldboy' > /data/www/html/index.html 

#使用浏览器进行验证

参考链接:http://www.cnblogs.com/linhaifeng/articles/6045600.html#_label19

原文地址:https://www.cnblogs.com/luchuangao/p/6589170.html