1、一鍵安裝四個依賴javascript
yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel
2、建立一個安裝目錄,並下載nginx安裝包
cd /usr/local
mkdir nginx #建立目錄名爲nginx的文件夾
cd nginx
wget http://nginx.org/download/nginx-1.13.7.tar.gz #下載nginx安裝包
3、安裝nginx
cd /usr/local/nginx #進入nginx目錄
tar zxvf nginx-1.13.7.tar.gz -C /usr/local/nginx #解壓
cd /usr/local/nginx/nginx-1.13.7
./configure --prefix=/usr/local/nginx --conf-path=/usr/local/nginx/nginx.conf #執行命令
若是執行下面的命令時報: -bash: ./configure: Permission denied 錯誤,則執行:bash ./configure
若是執行下面的命令時報: 找不到logs文件夾 錯誤,則在安裝目錄中建立 logs文件夾
make #執行make命令
make install #執行make install命令
4、檢查及啓動nginx
/usr/local/nginx/sbin/nginx -t #檢查一下修改過的配置文件是否正確
cd /usr/local/nginx/sbin #進入安裝目錄php
./nginx #啓動nginxcss
若是啓動沒有報錯,則啓動成功。在瀏覽中輸入服務器ip,若是能訪問到nginx首頁,則安裝成功。有時可能還要關閉防火牆才能訪問。html
5、若是修改配置文件後要重啓nginx服務java
/usr/local/nginx/sbin/nginx -s reload #重啓nginxnginx
附:nginx 優化後的配置文件web
#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 102400;#number的值不能大於操做系統能打開的最大的文件句柄數 accept_mutex on;#優化同一時刻只有一個請求而避免多個睡眠進程被喚醒的設置,on爲防止被同時喚醒,默認爲off multi_accept on; #打開同時接受多個新網絡鏈接請求的功能 use epoll; #使用epoll事件驅動,由於epoll的性能相比其餘事件驅動要好不少 } http { server_tokens off; #隱藏ngxin版本號 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; #gzip_static on; gzip_disable "msie6"; #不使用gzip IE6 gzip_min_length 10k; #gzip壓縮最小文件大小,超出進行壓縮(自行調節) gzip_buffers 4 16k; #buffer 不用修改 gzip_comp_level 3; #壓縮級別:1-10,數字越大壓縮的越好,時間也越長 gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png; #壓縮文件類型 gzip_vary off; #跟Squid等緩存服務有關,on的話會在Header裏增長 "Vary: Accept-Encoding" server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { add_header Cache-Control "no-cache, no-store"; root /data/xyweb/;# index index.html index.htm; expires -1; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} } server { listen 8089;# server_name localhost; location / { root /data/xyweb/machine; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } server { listen 8088;# server_name localhost; location / { root /data/xyweb/rzweb; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } server { listen 8086;# server_name localhost; location / { root /data/xyFiles; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } server { listen 80;# server_name pay.xynetweb.com; location / { proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://195.6.71.888:8090/service-pay/pay/; } } server { listen 80;# server_name paytest.xynetweb.com; location / { proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://195.6.4.448:6017/service-pay/pay/; } location /api/pay/jft/ { proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://195.6.4.448:6017/service-pay-wx/pay/api/jft/; } } # another virtual host using mix of IP-, name-, and port-based configuration # #server { # listen 8000; # listen somename:8080; # server_name somename alias another.alias; # location / { # root html; # index index.html index.htm; # } #} # HTTPS server # #server { # listen 443 ssl; # server_name localhost; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # } #} }