內網服務器centos7配置nginx


在學校里搞了個內網,鏡像源用的是學校的。只好手動搞一下了。

ssh連接服務器后。

1. 首先安裝所需的各種依賴。

安裝gcc,nginx是用c寫的,編譯的時候需要gcc環境
yum install gcc-c++
安裝pcre,一個正則庫,nginx需要用到
yum install -y pcre pcre-devel
檢查pcre安裝
pcre-config --version
安裝zlib,nginx對包zip壓縮時需要
yum install -y zlib zlib-devel
安裝openssl,是一個密碼庫,ssl需要
yum install -y openssl openssl-devel

2. 安裝nginx

下載nginx
wget -c https://nginx.org/download/nginx-1.12.2.tar.gz
由於我是用本地下載的,因為服務器連不了外網,所以scp遠程傳過去
scp 本地文件路徑 服務器用戶名@服務器ip:存到服務器的文件路徑
解壓
tar -zxvf nginx-1.12.2.tar.gz
cd nginx-1.12.2
使用默認配置
./configure
編譯
make
安裝,安裝后的路徑在/usr/local/nginx
make install
查看安裝版本,有則安裝成功
/usr/local/nginx/sbin/nginx -v

3. nginx配置

創建www用戶運行nginx
創建www用戶組
/usr/sbin/groupadd www
www用戶組中創建www用戶
/usr/sbin/useradd -g www www
配置nginx.conf
sudo vim /usr/local/nginx/conf/nginx.conf
刪除原有的,輸入以下

user www www;
worker_processes 2; #設置值和CPU核心數一致
error_log /usr/local/nginx/logs/nginx_error.log crit; #日志位置和日志級別
pid /usr/local/nginx/nginx.pid;
#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 65535;
events
{
  use epoll;
  worker_connections 65535;
}
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';
  
#charset gb2312;
     
  server_names_hash_bucket_size 128;
  client_header_buffer_size 32k;
  large_client_header_buffers 4 32k;
  client_max_body_size 8m;
     
  sendfile on;
  tcp_nopush on;
  keepalive_timeout 60;
  tcp_nodelay on;
  fastcgi_connect_timeout 300;
  fastcgi_send_timeout 300;
  fastcgi_read_timeout 300;
  fastcgi_buffer_size 64k;
  fastcgi_buffers 4 64k;
  fastcgi_busy_buffers_size 128k;
  fastcgi_temp_file_write_size 128k;
  gzip on; 
  gzip_min_length 1k;
  gzip_buffers 4 16k;
  gzip_http_version 1.0;
  gzip_comp_level 2;
  gzip_types text/plain application/x-javascript text/css application/xml;
  gzip_vary on;
 
  #limit_zone crawler $binary_remote_addr 10m;
 #下面是server虛擬主機的配置
 server
  {
    listen 80;#監聽端口
    server_name localhost;#域名
    index index.html index.htm index.php;
    root /usr/local/nginx/html;#站點目錄
      location ~ .*\.(php|php5)?$
    {
      #fastcgi_pass unix:/tmp/php-cgi.sock;
      fastcgi_pass 127.0.0.1:9000;
      fastcgi_index index.php;
      include fastcgi.conf;
    }
    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|ico)$
    {
      expires 30d;
  # access_log off;
    }
    location ~ .*\.(js|css)?$
    {
      expires 15d;
   # access_log off;
    }
    access_log off;
  }

}

檢查一下配置是否成功
/usr/local/webserver/nginx/sbin/nginx -t

3 防火牆設置

sudo firewall-cmd --permanent --zone=public --add-service=http
sudo firewall-cmd --permanent --zone=public --add-service=https
sudo firewall-cmd --reload

4 開啟nginx服務

查看服務器公網地址
ip addr show eth0 | grep inet | awk '{ print $2; }' | sed 's/\/.*$//'
啟動nginx
/usr/local/nginx/sbin/nginx./nginxsudo systemctl start nginx.service
現在可以到瀏覽器輸入ip地址看是否成功了,如果有Welcome to nginx!界面,那么就成功了
設置開機自啟
sudo systemctl enable nginx.service

5 nginx 配置目錄信息

網站文件存放默認目錄
/usr/share/nginx/html
網站默認站點配置
/etc/nginx/conf.d/default.conf
自定義Nginx站點配置文件存放目錄
/etc/nginx/conf.d/
Nginx全局配置
/etc/nginx/nginx.conf
Nginx啟動
nginx -c nginx.conf

6 nginx 其他常用命令

關閉nginx
./nginx -s stop/usr/local/nginx/sbin/nginx -s stop
重載配置
/usr/local/nginx/sbin/nginx -s reload./nginx -s reload

參考1
參考2
參考3


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM