安装Nginx(Ubuntu Server)
1.首先安装nginx依赖插件:
sudo apt-get install openssl libssl-dev
sudo apt-get install libpcre3 libpcre3-dev
sudo apt-get install zlib1g-dev
sudo apt-get install build-essential
2.下载nginx安装包并解压:
tar
3.编译安装:
进入解压目录:cd nginx-x.x.x
依次执行以下命令:
./configure \
--prefix=/usr/local/nginx \
--pid-path=/usr/local/nginx/nginx.pid \
--lock-path=/usr/local/nginx/nginx.lock \
--error-log-path=/usr/local/nginx/logs/error.log \
--http-log-path=/usr/local/nginx/logs/access.log \
--with-http_gzip_static_module \
--http-client-body-temp-path=/usr/local/nginx/temp/client \
--http-proxy-temp-path=/usr/local/nginx/temp/proxy \
--http-fastcgi-temp-path=/usr/local/nginx/temp/fastcgi \
--http-uwsgi-temp-path=/usr/local/nginx/temp/uwsgi \
--http-scgi-temp-path=/usr/local/nginx/temp/scgi
make
make install
4.Nginx配置
#user nobody; #定义Nginx运行的用户和用户组
worker_processes 2; #nginx进程数
error_log /usr/local/nginx/logs/error.log debug;
pid /usr/local/nginx/logs/nginx.pid;
worker_rlimit_nofile 1024; #nginx进程打开最多文件描述符数目,理论值应该是最多打开文件数(系统的值ulimit -n)与nginx进程数相除,但是nginx分配请求并不均匀,所以建议与ulimit -n的值保持一致
events {
use epoll; #事件模型
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 /usr/local/nginx/logs/access.log main;
server_names_hash_bucket_size 128; #服务器名字的hash表大小
client_header_buffer_size 1024k; #上传文件大小限制
sendfile on; #开启高效文件传输模式,如果图片显示不正常把这个改成off
tcp_nopush on; #防止网络阻塞
tcp_nodelay on; #防止网络阻塞
keepalive_timeout 120; #长连接超时时间,单位是秒
#FastCGI相关参数是为了改善网站的性能:减少资源占用,提高访问速度
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模块设置
gzip on; #开启gzip压缩输出
gzip_min_length 1k; #最小压缩文件大小
gzip_buffers 4 16k; #压缩缓冲区
gzip_http_version 1.1; #压缩版本(默认1.1,前端如果是squid2.5请使用1.0)
gzip_comp_level 2; #压缩等级
gzip_types text/plain application/x-javascript text/css application/xml; #压缩类型,默认就已经包含text/html,所以下面就不用再写了
server {
listen 80;
server_name localhost; #域名可以有多个,用空格隔开
charset utf-8;
access_log logs/host.access.log main;
location / {
root /usr/local/nginx/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 /usr/local/nginx/html;
}
}
}
5.启动/停止:
进入/usr/local/nginx目录执行:
启动:sudo ./sbin/nginx