Linux-011 centos7搭建Nginx服务器 并 修改Nginx服务器访问根目录

来源:互联网 发布:网络引论 编辑:程序博客网 时间:2024/06/04 18:10


一、 nginx安装环境


       yum install -y gcc-c++   pcre pcre-devel zlib zlib-devel  openssl openssl-devel


该命令等效于下面4句:

1、  gcc 安装

        安装nginx需要先将官网下载的源码进行编译,编译依赖gcc环境,如果没有gcc环境,需要安装gcc:

         yum install gcc-c++

2、  PCRE pcre-devel 安装

       PCRE(PerlCompatible Regular Expressions)是一个Perl库,包括 perl 兼容的正则表达式库。nginx的http模块使用pcre来解析正则表达式,所以需要在linux上安装pcre库。

        yum install -y pcre pcre-devel

3、  zlib 安装

        zlib库提供了很多种压缩和解压缩的方式,nginx使用zlib对http包的内容进行gzip,所以需要在linux上安装zlib库。

        yum  install -y zlib zlib-devel

4、OpenSSL 安装

      OpenSSL是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及SSL协议,并提供丰富的应用程序供测试或其它目的使用。nginx不仅支持http协议,还支持https(即在ssl协议上传输http),所以需要在linux安装openssl库。

       yum  install -y openssl openssl-devel


二、下载nginx


1.直接下载.tar.gz安装包,地址:https://nginx.org/en/download.html


2.使用wget命令下载(推荐)。

wget -c https://nginx.org/download/nginx-1.10.1.tar.gz


三、安装

1、解压

依然是直接命令:

tar -zxvf nginx-1.10.1.tar.gzcd nginx-1.10.1

2、配置

其实在 nginx-1.10.1 版本中你就不需要去配置相关东西,默认就可以了。当然,如果你要自己配置目录也是可以的。1.使用默认配置

./configure

2.自定义配置(不推荐)

./configure \--prefix=/usr/local/nginx \--conf-path=/usr/local/nginx/conf/nginx.conf \--pid-path=/usr/local/nginx/conf/nginx.pid \--lock-path=/var/lock/nginx.lock \--error-log-path=/var/log/nginx/error.log \--http-log-path=/var/log/nginx/access.log \--with-http_gzip_static_module \--http-client-body-temp-path=/var/temp/nginx/client \--http-proxy-temp-path=/var/temp/nginx/proxy \--http-fastcgi-temp-path=/var/temp/nginx/fastcgi \--http-uwsgi-temp-path=/var/temp/nginx/uwsgi \--http-scgi-temp-path=/var/temp/nginx/scgi

注:将临时文件目录指定为/var/temp/nginx,需要在/var下创建temp及nginx目录

执行:./configure ,结果:

[root@localhost nginx-1.12.2]# ./configure
checking for OS
 + Linux 3.10.0-327.el7.x86_64 x86_64
.......

Configuration summary
  + using system PCRE library
  + OpenSSL library is not used
  + using system zlib library

  nginx path prefix: "/usr/local/nginx"
  nginx binary file: "/usr/local/nginx/sbin/nginx"
  nginx modules path: "/usr/local/nginx/modules"
  nginx configuration prefix: "/usr/local/nginx/conf"
  nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
  nginx pid file: "/usr/local/nginx/logs/nginx.pid"
  nginx error log file: "/usr/local/nginx/logs/error.log"
  nginx http access log file: "/usr/local/nginx/logs/access.log"
  nginx http client request body temporary files: "client_body_temp"
  nginx http proxy temporary files: "proxy_temp"
  nginx http fastcgi temporary files: "fastcgi_temp"
  nginx http uwsgi temporary files: "uwsgi_temp"
  nginx http scgi temporary files: "scgi_temp"

3、编译安装

makemake install

查找安装路径:

whereis nginx

nginx-whereis.png


四、开放80端口、开放http服务,重启防火墙

  1. firewall-cmd --zone=public --add-port=80/tcp --permanent 
  2. firewall-cmd --permanent --zone=public --add-service=http
  3. firewall-cmd --reload
  4. firewall-cmd --list-all #查看开放服务、端口中是否有http服务和80端口。

[root@localhost nginx-1.12.2]# firewall-cmd --list-all
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: enp0s3
  sources:
  services: dhcpv6-client ssh ftp http #已开放ssh、ftp、http服务
  ports: 21/tcp 80/tcp #已开放21、80端口
  protocols:
  masquerade: no
  forward-ports:
  source-ports:
  icmp-blocks:
  rich rules:



五、更改nginx访问根目录

安装完nginx服务器后发现nginx的根目录在 安装目录的/html/下(/usr/local/nginx/html/),但是对于部署文件来说,在该目录下是不太习惯的,我就尝试着更改nginx访问的根目录

1、更改nginx配置文件   vi  /usr/local/nginx/conf/nginx.conf

#user  nobody;worker_processes  1;#error_log  logs/error.log;#error_log  logs/error.log  notice;#error_log  logs/error.log  info;#pid        logs/nginx.pid;events {    worker_connections  1024;}http {    include       mime.types;    default_type  application/octet-stream;    #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  logs/access.log  main;    sendfile        on;    #tcp_nopush     on;    #keepalive_timeout  0;    keepalive_timeout  65;    #gzip  on;    server {        listen       80;        server_name  localhost;                #charset koi8-r;        #access_log  logs/host.access.log  main;        location / {                  
            root  /home/ftpuser/wwwRoot/; #修改http://locakhost:80/映射地址,此时对应本机访问路径为 :/home/ftpuser/wwwRoot/
index  index.html index.htm index.jpg;#添加一张图片,测试用。
        }

        #error_page  404              /404.html;
error_page 404 /404.html;
 location = /50x.html {
           root  /home/ftpuser/wwwRoot/; #修改http://locakhost:80/映射地址,此时对应本机访问路径为 :/home/ftpuser/wwwRoot/
        }

 。。。。。。

 }

#  :wq!  #保存并退出


2、nginx根目录权限设置:

 为了方便学习,我通常这样设置权限,命令:

         chmod  -R 755  /home/ftpuser/wwwRoot/

如果为保证安全,可这样设置:

        1)设置新的访问根目录权限为所有人可执行(不然就没权限访问根目录了,一访问就是403错误!)

                chmod   711  /home/ftpuser/wwwRoot/

        2) 设置目录下文件为所有人可读权限(不然就没法读文件了,一读就是403错误!)

                chmod  -R 744  /home/ftpuser/wwwRoot/*


3、重启nginx务器

     service nginx restart

4、测试

拷贝一直张jpg格式图片到:/home/ftpuser/wwwRoot/目录下,改名为:index.jpg 。

chmod  744  /home/ftpuser/wwwRoot/index.jpg #设置所有人可读。

访问http://localhost/,图片即在网页中显示出来。

设置完成后此时访问http://localhost/XXX   即为/home/www/XXX


六、使用nginx

1 启动nginx

ln -s /usr/local/nginx/sbin/nginx  /bin/nginx

从此以后直接在命令行输入:

nginx



/usr/local/nginx/sbin/nginx



cd /usr/local/nginx/sbin/

./nginx  


查询nginx进程:

ps aux|grep nginx


注意:执行./nginx启动nginx,这里可以-c指定加载的nginx配置文件,如下:

./nginx -c /usr/local/nginx/conf/nginx.conf

如果不指定-c,nginx在启动时默认加载conf/nginx.conf文件。

 

2 停止nginx

方式1,快速停止:

cd /usr/local/nginx/sbin

./nginx -s stop

此方式相当于先查出nginx进程id再使用kill命令强制杀掉进程。

 

方式2,完整停止(建议使用):

cd /usr/local/nginx/sbin

./nginx -s quit

此方式停止步骤是待nginx进程处理任务完毕进行停止。

 

 

3 重启nginx

方式1,先停止再启动(建议使用):

对nginx进行重启相当于先停止nginx再启动nginx,即先执行停止命令再执行启动命令。

如下:

./nginx -s quit

./nginx

 

方式2,重新加载配置文件启动更改配置后使用

当nginx的配置文件nginx.conf修改后,要想让配置生效需要重启nginx,使用-s reload不用先停止nginx再启动nginx即可将配置信息在nginx中生效,如下:

./nginx -s reload