首先安裝nginx安裝環境
nginx是C語言開發,建議在linux上運行,本教程使用Centos6.5作為安裝環境。
- gcc
安裝nginx需要先將官網下載的源碼進行編譯,編譯依賴gcc環境,如果沒有gcc環境,需要安裝gcc:
yum install gcc-c++
- PCRE
PCRE(Perl Compatible Regular Expressions)是一個Perl庫,包括 perl 兼容的正則表達式庫。nginx的http模塊使用pcre來解析正則表達式,所以需要在linux上安裝pcre庫。
yum install -y pcre pcre-devel
注:pcre-devel是使用pcre開發的一個二次開發庫。nginx也需要此庫。
- zlib
zlib庫提供了很多種壓縮和解壓縮的方式,nginx使用zlib對http包的內容進行gzip,所以需要在linux上安裝zlib庫。
yum install -y zlib zlib-devel
- openssl
OpenSSL 是一個強大的安全套接字層密碼庫,囊括主要的密碼算法、常用的密鑰和證書封裝管理功能及SSL協議,並提供豐富的應用程序供測試或其它目的使用。
nginx不僅支持http協議,還支持https(即在ssl協議上傳輸http),所以需要在linux安裝openssl庫。
yum install -y openssl openssl-devel
然后安裝nginx
到Nginx官網下載tar.gz格式的安裝包,這里下載的是nginx-1.10.3版本,環境使用centos的虛擬機
1、將安裝包上傳,解壓,命令tar -zxvf nginx-1.10.3.tar.gz
2、自定義創建一個文件夾作為Nginx安裝目錄,這里在home下創建nginx文件夾;
3、在解壓的文件夾(nginx-1.10.3)下執行./configure --prefix=/home/nginx 命令。
意思即配置安裝環境,將會把Nginx安裝到/home/nginx下;
4、編譯:在解壓的文件夾下先后執行make 和 make install 命令
執行make
然后執行make install
5、Nginx默認使用端口是80,這里直接先把Nginx端口改為8088,
vi /home/nginx/conf/nginx.conf,修改server的端口,並配置一個圖片服務器
user root; worker_processes 1; error_log logs/error.log debug; #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 888; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location ~ .*\.(gif|jpg|jpeg|png)$ { expires 24h; root /home/www/FTP/; access_log /home/nginx/logs/images.log; proxy_store on; proxy_store_access user:rw group:rw all:rw; proxy_temp_path /home/www/FTP/; proxy_redirect off; proxy_set_header Host 127.0.0.1; client_max_body_size 10m; client_body_buffer_size 1280k; proxy_connect_timeout 900; proxy_send_timeout 900; proxy_read_timeout 900; proxy_buffer_size 40k; proxy_buffers 40 320k; proxy_busy_buffers_size 640k; proxy_temp_file_write_size 640k; } location / { root html; index index.html index.htm; } #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; #} } # 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; # } #} }
server下listen的端口改為8088,並增加一個location配置,用於訪問圖片文件,這一串配置有#號在前面的注釋掉的都可以刪掉,免得看起來又長又亂。
6、啟動命令: /home/nginx/sbin/nginx -c /home/nginx/conf/nginx.conf
打開防火牆對應端口供訪問,8088,當然也可以直接關了防火牆。
若修改了nginx.conf配置,則需要重啟才生效,命令:/home/nginx/sbin/nginx -s reload