二十五岁时我们都一样愚蠢、多愁善感,喜欢故弄玄虚,可如果不那样的话,五十岁时也就不会如此明智。
标题:Linux ( CentOS ) Nginx 安装配置
Nginx —— Ngine X,是一款自由的、开源的、高性能HTTP服务器和反向代理服务器;也是一个IMAP、POP3、SMTP代理服务器
也就是说Nginx本身就可以托管网站(类似于 Apache 一样),进行 Http 服务处理,也可以作为反向代理服务器使用
Nginx 解决了服务器的C10K(就是在一秒之内连接客户端的数目为10k即1万)问题。它的设计不像传统的服务器那样使用线程处理请求,而是一个更加高级的机制—事件驱动机制,是一种异步事件驱动结构
Nginx 安装
系统平台:CentOS release 7.x (Final) 64位
一、安装编译工具及库文件
[root@localhost ~]# yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel二、首先要安装 PCRE
PCRE 可以让 Nginx 支持 Rewrite 功能
1、下载 PCRE 安装包: http://downloads.sourceforge.net/project/pcre/pcre/8.35/pcre-8.35.tar.gz
[root@localhost ~]# wget http://downloads.sourceforge.net/project/pcre/pcre/8.35/pcre-8.35.tar.gz2、解压安装包:
[root@localhost ~]# tar zxvf pcre-8.35.tar.gz3、进入安装包目录
[root@localhost ~]# cd pcre-8.354、编译安装
[root@localhost pcre-8.35]# ./configure [root@localhost pcre-8.35]# make && make install5、查看 pcre 版本
[root@localhost pcre-8.35]# pcre-config --version 8.35安装 Nginx
1、下载 Nginx : http://nginx.org/download/nginx-1.6.2.tar.gz
[root@localhost ~]# wget http://nginx.org/download/nginx-1.6.2.tar.gz2、解压安装包
[root@localhost ~]# tar zxvf nginx-1.6.2.tar.gz3、进入安装包目录
[root@localhost ~]# cd nginx-1.6.24、编译安装
[root@localhost nginx-1.6.2]# ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre=/usr/local/src/pcre-8.35 [root@localhost nginx-1.6.2]# make [root@localhost nginx-1.6.2]# make install5、查看 nginx 版本
[root@localhost nginx-1.6.2]# /usr/local/nginx/sbin/nginx -v输出如下
[root@localhost nginx-1.6.2]# /usr/local/nginx/sbin/nginx -v Nginx version: nginx/1.6.2到此,nginx 安装完成
Nginx 配置
创建 Nginx 运行使用的用户 www 和 组 www:
[root@localhost ~]# groupadd www [root@localhost ~]# useradd -g www www配置 nginx.conf,将 /usr/local/nginx/conf/nginx.conf 替换为以下内容
[root@localhost conf]# cat /usr/local/nginx/conf/nginx.conf user www www; worker_processes 1; #可以设置值和 CPU 核心数一致,本机比较破,所以就一个了 error_log /usr/local/nginx/logs/nginx_error.log crit; #日志位置和日志级别 pid /usr/local/nginx/nginx.pid; #Specifies the value for maximum file descriptors that can be opened by this process. worker_rlimit_nofile 65535; events { use epoll; worker_connections 65535; } 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'; #charset gb2312; server_names_hash_bucket_size 128; client_header_buffer_size 32k; large_client_header_buffers 4 32k; client_max_body_size 8m; sendfile on; tcp_nopush on; keepalive_timeout 60; tcp_nodelay on; 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 on; gzip_min_length 1k; gzip_buffers 4 16k; gzip_http_version 1.0; gzip_comp_level 2; gzip_types text/plain application/x-javascript text/css application/xml; gzip_vary on; #limit_zone crawler $binary_remote_addr 10m; #下面是server虚拟主机的配置 server { listen 80; #监听端口 server_name localhost; #域名 index index.htm index.html index.php; root /usr/local/nginx/html; # 站点目录 location ~ .*\.(php|php5)?$ { #fastcgi_pass unix:/tmp/php-cgi.sock; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; include fastcgi.conf; } location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|ico)$ { expires 30d; access_log off; } location ~ .*\.(js|css)?$ { expires 15d; access_log off; } access_log off; } }检查配置文件 ngnix.conf 的正确性命令:
[root@localhost ~]# /usr/local/nginx/sbin/nginx -t输出如下
[root@localhost ~]# /usr/local/nginx/sbin/nginx -t the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok configuration file /usr/local/nginx/conf/nginx.conf test is successful启动 Nginx
Nginx 启动命令如下
[root@localhost conf]# /usr/local/nginx/sbin/nginx我们用 ps 命令看看 nginx 启动情况
[root@localhost ~]# ps aux | grep nginx root 21493 0.0 0.2 103276 1024 pts/0 S+ 17:21 0:00 grep nginx www 22148 0.0 5.4 83420 27328 ? S Sep07 3:01 nginx: worker process root 23774 0.0 0.8 61424 4288 ? Ss Jul05 0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf访问站点
从浏览器访问我们配置的站点 http://localhost
Nginx 其它常用命令
- 重新载入配置
[root@localhost ~]# /usr/local/nginx/sbin/nginx -s reload
- 重启 Nginx
[root@localhost ~]# /usr/local/nginx/sbin/nginx -s reopen
- 停止 Nginx
[root@localhost ~]# /usr/local/nginx/sbin/nginx -s stop