我一哥們,常以麒麟之才自稱,最近告訴我掉了很多頭發,說在搭建nginx服務器的時候被難倒了。Nginx是一款高性能的http 服務器,小編今天帶你怎樣安裝nginx,以及怎樣配置,最后說一下常見的問題。
(一)nginx需要環境配置
g++環境
yum install gcc-c++
PERE,一個Perl庫,包括 perl 兼容的正則表達式庫,nginx的http模塊使用pcre來解析正則表達式:
yum install -y pcre pcre-devel
zlib,zlib庫提供了很多種壓縮和解壓縮的方式,nginx使用zlib對http包的內容進行gzip
yum install -y zlib zlib-devel
openssl,一個強大的安全套接字層密碼庫,囊括主要的密碼算法、常用的密鑰和證書封裝管理功能及SSL協議
yum install -y openssl openssl-devel
(二)nginx安裝
官網下載地址,http://nginx.org/en/download.html,小編安裝的nginx-1.8.1
wget http://nginx.org/download/nginx-1.8.1.tar.gz
tar -zxvf nginx-1.8.1.tar.gz
mkdir -p /var/temp/nginx #必須創建一個臨時目錄,要不會報錯
cd nginx-1.8.1
./configure --prefix=/usr/local/nginx/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 --with-http_stub_status_module --with-http_ssl_module
make;make install
(三)配置
小編這里給出了自己的親測可用的配置文件,vim /usr/local/nginx/nginx/conf/nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name 10.168.1.120; #nginx服務器ip,可以ifconfig查看
location / {
root /geneonto/packages/; #需要共享的文件路徑
autoindex on;
autoindex_exact_size on;
autoindex_localtime on;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
配置完成后需要指定配置文件并且重啟nginx服務:
cd /usr/local/nginx/nginx
./nginx -c /usr/local/nginx/nginx/conf/nginx.conf
./nginx -s reload
systemctl stop firewalld #一定要關閉防火墻,要不訪問不了
重啟并關閉防火墻后,可以上同一個局域網查看并下載文件,地址:http://10.168.1.120
(四)常見問題
1、一定要關閉防火墻
2、重啟電腦后需要重新創建臨時目錄
mkdir -p /var/temp/nginx
3、若出現一下報錯,說明端口有被nginx占用,需要殺了重新啟動
./nginx -c /usr/local/nginx/nginx/conf/nginx.conf #出現一下報錯
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use) #說明80端口被占用,需要殺了重新啟動nginx
lsof -i :80 #查看占用的程序,結果如下:
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
nginx 4454 root 6u IPv4 16789 0t0 TCP *:http (LISTEN)
nginx 5391 nobody 6u IPv4 16789 0t0 TCP *:http (LISTEN)
kill -9 4454 5391 #殺了這兩個進程
重新執行一下命令
./nginx -c /usr/local/nginx/nginx/conf/nginx.conf
./nginx -s reload