1. 程式人生 > >Linux CentOS7安裝nginx圖片伺服器

Linux CentOS7安裝nginx圖片伺服器

文章目錄

安裝nginx依賴包

安裝gcc

yum -y install gcc-c++

安裝PCRE正則表示式解析

yum install -y pcre pcre-devel

安裝zlib解壓縮

yum install -y zlib zlib-devel

安裝openssl

yum install -y openssl openssl-devel

nginx安裝步驟

把nginx的壓縮包上傳到linux系統

在root使用者下,在secureCRT介面下按Alt+p進入sftp,把檔案拖到視窗,檔案就傳到了~目錄下

解壓

  • z表示格式
  • x解壓
  • v進度
  • f指定檔案
tar -zxvf nginx-1.8.0.tar.gz

新建臨時快取資料夾

/var下建立tempnginx目錄,即·/var/temp/nginx。

進入nginx-1.8.0執行configure

./configure \
--prefix=/usr/local/nginx \
--pid-path=/var/run/nginx/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

make並且make install

進入nginx-1.8.0,執行make和make install命令

[[email protected] nginx-1.8.0]# make
[[email protected] nginx-1.8.0]# make install

啟動

在/usr/local/nginx/sbin資料夾下:

[[email protected] sbin]# ./nginx

檢視程序

ps aux|grep nginx

應該有兩個nginx程序masterworker

在瀏覽器輸入伺服器域名192.168.52.129訪問nginx

無法訪問歡迎頁面【防火牆問題】

檢視防火牆埠配置檔案:

vim /etc/sysconfig/iptables

如果訪問ip地址無法顯示nginx的歡迎頁面,大概是防火牆的原因,設定防火牆80埠開啟:

[[email protected] ]# /sbin/iptables -I INPUT -p tcp --dport 80 -j ACCEPT
[[email protected] ]# cd /etc/init.d/iptables save
[[email protected] ]# service iptables save
[[email protected] ]# service iptables restart

如果service iptables save命令執行失敗報出:

The service command supports only basic LSB actions (start, stop, restart, try-restart, reload, force-reload, status). For other actions, please try to use systemctl.

解決方法:
systemctl stop firewalld 關閉防火牆
yum install iptables-services 安裝或更新服務
再使用systemctl enable iptables 啟動iptables
最後 systemctl start iptables 開啟iptables

再執行防火牆配置

關閉nginx

[[email protected] sbin]# ./nginx -s stop

配置檔案nginx.conf

在檔案/usr/local/nginx/conf/nginx.conf的末尾 “}”前新增如下server後儲存:

  • 可以根據埠號區分:如80和81
  • 可以通過域名區分:修改host檔案,test.taotao.com,並新增主頁html-test/index.html
    server {
        listen       80;
        server_name  test.taotao.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html-test;
            index  index.html index.htm;
        }
    }

修改主機host檔案

推薦使用switchost軟體,在主機host檔案中新增如下對映:

192.168.52.129 test.taotao.com

複製index主頁

複製nginx下的html資料夾為html-test

cp -r html html-test

編輯html-test裡面的index.html檔案,隨便修改幾個內容,以示區別。

vi html-test/index.html

在sbin目錄下執行./nginx -s reload啟動nginx並更新配置

執行./nginx -s reload找不到nginx.pid

如果出現如下錯誤:

nginx: [error] open() "/var/run/nginx/nginx.pid" failed (2: No such file or directory)

在sbin資料夾下執行如下命令,指定配置檔案路徑:

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

瀏覽器輸入test.taotao.com訪問html-test下的index.html頁面