安装Nginx需要依赖gzip ,rewrite ,ssl 个包,同时需要安装c++编译环境。
如果没有安装c++编译环境,还得安装,通过yum install gcc-c++完成安装。
三个依赖包的安装顺序为:openssl、zlib、pcre, 全部安装完成以后才能安装Nginx包。
1.gzip 模块需要 zlib 库 ( 下载: http://www.zlib.NET/ )
2.rewrite 模块需要 pcre 库 ( 下载: http://www.pcre.org/ )
3.ssl 功能需要 openssl 库 ( 下载: http://www.openssl.org/ )
安装依赖环境
c++编译环境
[root@localhost] yum -y install gcc-c++
openssl :
[root@localhost] tar zxvf openssl-fips-2.0.9.tar.gz
[root@localhost] cd openssl-fips-2.0.9
[root@localhost] ./config && make && make install
pcre:
[root@localhost] tar zxvf pcre-8.36.tar.gz
[root@localhost] cd pcre-8.36
[root@localhost] ./configure && make && make install
zlib:
[root@localhost]tar zxvf zlib-1.2.8.tar.gz
[root@localhost] cd zlib-1.2.8
[root@localhost] ./configure && make && make install
安装 Nginx
1.解压安装包
[root@bogon src]# tar zxvf nginx-1.6.2.tar.gz2.进入安装包目录
[root@bogon src]# cd nginx-1.6.23.编译安装
[root@bogon nginx-1.6.2]# ./configure --prefix=/usr/local/nginx --with-http_gzip_static_module --with-http_stub_status_module --with-http_ssl_module --with-openssl=/root/Nginx/openssl-1.0.1e --with-pcre=/root/Nginx/pcre-8.35 [root@bogon nginx-1.6.2]# make [root@bogon nginx-1.6.2]# make install注:--with-http_stub_status_module --with-http_ssl_module //如果不需要ssl可以不加
--with-openssl=/root/Nginx/openssl-1.0.1e //openssl库的源代码路径。版本需要与安装或者在使用中的一致下载地址:https://www.openssl.org/source/old/1.0.1/
--with-pcre=/root/Nginx/pcre-8.35 /指/定 PCRE 库的源代码的路径。
4.查看nginx版本
[root@bogon nginx-1.6.2]# /usr/local/webserver/nginx/sbin/nginx -v到此,nginx安装完成。
Nginx 配置
创建 Nginx 运行使用的用户 www:
[root@bogon conf]# /usr/sbin/groupadd www [root@bogon conf]# /usr/sbin/useradd -g www www配置nginx.conf ,将/usr/local/webserver/nginx/conf/nginx.conf替换为以下内容
#user nobody; user root; #设置启用用户为root 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 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 88; #设置端口 server_name localhost; root /root/www; #设置web根目录 #charset koi8-r; #access_log logs/host.access.log main; location / { 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$ { 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; # } #} }
检查配置文件ngnix.conf的正确性命令:
[root@bogon conf]# /usr/local/webserver/nginx/sbin/nginx -t
启动 Nginx
Nginx 启动命令如下:
[root@bogon conf]# /usr/local/webserver/nginx/sbin/nginx
访问站点
从浏览器访问我们配置的站点ip:
Nginx 其他命令
以下包含了 Nginx 常用的几个命令:
/usr/local/webserver/nginx/sbin/nginx -s reload # 平滑重启 重启前建议用-t检查配置文件是否正确 /usr/local/webserver/nginx/sbin/nginx -s reopen # 重启 Nginx /usr/local/webserver/nginx/sbin/nginx -s stop # 停止 Nginx