1. 程式人生 > >linux部署nginx

linux部署nginx

1、安裝依賴包

# yum install gcc gcc-c++ make automake autoconf libtool pcre pcre-devel zlib zlib-devel openssl openssl-devel kernel-devel  popt-devel libnl libnl-devel libnfnetlink-devel wget 

2、建立下載路徑

# mkdir /home/setup #建立setup路徑(下載檔案放到這裡)已建立的跳下一步
# cd /home/setup #切換到setup路徑下
# wget http://nginx.org/download/nginx-1.13.0.tar.gz #下載

3、安裝nginx

# tar -zxvf nginx-1.13.0.tar.gz #解壓
#cd nginx-1.13.0 #切換路徑
#useradd www -M -s /sbin/nologin #新增www使用者,其中-M引數表示不新增使用者目錄,-s引數表示指定shell型別
 # vi auto/cc/gcc  #將這句註釋掉 取消Debug編譯模式 大概在179行
----------------編輯
#CFLAGS="$CFLAGS -g"
----儲存退出

4、編譯nginx(根據專案需要進行編譯對應擴充套件)

# ./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_ssl_module --with-http_flv_module --with-http_mp4_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-mail
#make && make install #安裝 #echo "/usr/local/nginx/sbin/nginx" >> /etc/rc.local #配置開機自啟 #mkdir /home/nginx #mkdir /home/nginx/conf #nginx配置檔案路徑 #chmod -R a+x /home/nginx/conf #新增執行許可權 注:新增加指令碼均需要給與執行許可權 如:web.conf chmod -R a+x /home/nginx/conf/web.conf

5、nginx.conf配置

############################################# nginx.conf
user www www; worker_processes 4; #cpu核數1/2 error_log /var/log/ngerror.log; #錯誤日誌 worker_rlimit_nofile 65536; events { worker_connections 4096; } http { include mime.types; default_type application/octet-stream; charset utf-8; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; client_max_body_size 1000M; #上傳檔案大小 fastcgi_connect_timeout 3000; fastcgi_send_timeout 3000; fastcgi_read_timeout 3000; fastcgi_buffer_size 256k; fastcgi_buffers 8 256k; fastcgi_busy_buffers_size 256k; fastcgi_temp_file_write_size 256k; fastcgi_intercept_errors on; #proxy_buffer_size 128k; #快取 #proxy_buffers 32 32k; #快取 # proxy_busy_buffers_size 128k; #快取 #proxy_temp_file_write_size 128k; #快取 #proxy_cache_path /var/www/cache levels=1:2 keys_zone=mycache:20m max_size=2048m inactive=60m; #快取 #proxy_temp_path /var/www/cache/tmp; #快取 gzip on; gzip_min_length 1k; gzip_buffers 4 16k; gzip_http_version 1.1; gzip_comp_level 9; gzip_types text/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php; gzip_vary on; ## includes vhosts include /usr/local/nginx/*.conf; include /home/nginx/conf/*.conf; } ##############################################

6、nginx啟動

啟動nginx
#/usr/local/nginx/sbin/nginx
重啟nginx 
#/usr/local/nginx/sbin/nginx -s reload
停止nginx
#/usr/local/nginx/sbin/nginx -s stop

7、測試網站
1) 配置web.conf

#cd /home/nginx/conf #切換路徑
#vi web.conf # 配置基於thinkphp的網站
#################################################編輯
server {
listen 80;  #埠80
server_name all; #域名或者IP
root /home/web; #網站路徑
# access_log /home/nginx/logs/access.log; #錯誤日誌路徑
# error_log /home/nginx/logs/error.log; #錯誤日誌路徑
index index.html index.htm;   #首頁配置

location ~ .*\.(gif|jpg|jpeg|png|bmp)$ {
expires 100d;
}
location ~ .*\.(js|css)?$ {
expires 30d;
}
error_page 500 502 503 504 /50x.html;
}
--------------------------------儲存退出

2)建立網站

#mkdir /home/web
#cd /home/web
#vi index.html
--------------編輯
我是測試網站!!!
-----------儲存

重啟nginx如下
#/usr/local/nginx/sbin/nginx -s reload

3)訪問測試

#curl http://ip
#我是測試網站!!!

OK 成功