centos安装nginx
1,下载nginx
http://nginx.org/download/nginx-1.11.12.tar.gz
http://nginx.org/en/download.html
2,安装Nginx所依赖的包:
gcc -v //查看系统是否安装gcc
rpm -qa | grep pcre 或者 pcre-config –version //是否安装pcre
rpm -qa | grep zlib //是否安装zlib
openssl version 或者 openssl version -a //是否安装 (最低要求1.0.2)
wget https://www.openssl.org/source/openssl-1.1.0e.tar.gz tar zxf openssl-1.1.0e.tar.gz
(一般情况下系统都会安装的)
yum -y install pcre*
yum -y install openssl*
yum -y install zlib*
3,切换到下载好的压缩包所在目录
例如:tar -zxvf nginx-1.11.12.tar.gz
4,查看openssl安装目录
一般在目录:
/usr/local/ssl
/usr/local/openssl
/usr/local/lib64 (我的在)
如果你是用rpm安装的
可以用命令
rpm -ql openssl
5,进入到解压好的nginx目录里面
关闭防火墙:service iptables stop
执行:
./configure --prefix=/etc/nginx --conf-path=/etc/nginx/nginx.conf --with-openssl=/usr/lib64/openssl --with-http_stub_status_module --with-debug --with-http_ssl_module --with-http_realip_module --with-http_gzip_static_module --with-http_v2_module
别人的安装参数:
--with-cc-opt='-g -O2 -fPIE -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2' --with-ld-opt='-Wl,-Bsymbolic-functions -fPIE -pie -Wl,-z,relro -Wl,-z,now' --prefix=/usr/share/nginx --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --lock-path=/var/lock/nginx.lock --pid-path=/run/nginx.pid --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --with-debug --with-pcre-jit --with-ipv6 --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_auth_request_module --with-http_addition_module --with-http_dav_module --with-http_geoip_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_image_filter_module --with-http_v2_module --with-http_sub_module --with-http_xslt_module --with-stream --with-stream_ssl_module --with-mail --with-mail_ssl_module --with-threads
6,接着编译
make && make install
7,启动nginx
cd /usr/local/nginx/sbin
/usr/local/nginx/sbin/nginx -V //查看版本和查看已安装模块
执行./nginx
启动:./nginx
停止:./nginx -s stop
重启:./nginx -s reopen
执行./nginx -h 可以看到命令的帮助信息
配置文件:
/nginx/nginx/conf/nginx.conf
8,加入系统托管,利用chkconfig控制
//执行文件名为:nginx
(注意路径)
#!/bin/bash
# chkconfig: - 30 21
# description: http service.
# Source Function Library
. /etc/init.d/functions
# Nginx Settings
NGINX_SBIN="/usr/local/nginx/sbin/nginx"
NGINX_CONF="/usr/local/nginx/conf/nginx.conf"
NGINX_PID="/usr/local/nginx/logs/nginx.pid"
RETVAL=0
prog="Nginx"
start() {
echo -n $"Starting $prog: "
mkdir -p /dev/shm/nginx_temp
daemon $NGINX_SBIN -c $NGINX_CONF
RETVAL=$?
echo
return $RETVAL
}
stop() {
echo -n $"Stopping $prog: "
killproc -p $NGINX_PID $NGINX_SBIN -TERM
rm -rf /dev/shm/nginx_temp
RETVAL=$?
echo
return $RETVAL
}
reload(){
echo -n $"Reloading $prog: "
killproc -p $NGINX_PID $NGINX_SBIN -HUP
RETVAL=$?
echo
return $RETVAL
}
restart(){
stop
start
}
configtest(){
$NGINX_SBIN -c $NGINX_CONF -t
return 0
}
case "$1" in
start)
start
;;
stop)
stop
;;
reload)
reload
;;
restart)
restart
;;
configtest)
configtest
;;
*)
echo $"Usage: $0 {start|stop|reload|restart|configtest}"
RETVAL=1
esac
exit $RETVAL
执行以下步骤:
cp nginx /etc/rc.d/init.d/
chmod 755 /etc/init.d/nginx
chkconfig –level 2345 nginx on
chkconfig –list nginx //2345都是on表示设置成功
下载: nginx
nginx.conf //一些常用配置
user root;
#user nobody;
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 {
proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=one:8m max_size=3000m inactive=600m;
proxy_temp_path /var/tmp;
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
gzip on;
gzip_comp_level 6;
gzip_vary on;
gzip_min_length 1000;
gzip_proxied any;
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
gzip_buffers 16 8k;
ssl_certificate /root/www/Nginx/1_www.easynode.cn_bundle.crt;
ssl_certificate_key /root/www/Nginx/2_www.easynode.cn.key;
ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:kEDH+AESGCM:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA256:DHE-RSA-AES256-SHA:!aNULL:!eNULL:!EXPORT:!DSS:!DES:!RC4:!3DES:!MD5:!PSK;
ssl_prefer_server_ciphers on;
upstream silly_face_society_upstream {
server 127.0.0.1:3000;
#server 127.0.0.1:8887;
keepalive 64;
}
server {
listen 80;
listen 443 ssl;
server_name www.easynode.cn;
#error_page 502 /errors/502.html;
if ($scheme = http) {
return 301 https://$server_name$request_uri;
}
#location ~ ^/(images/|img/|javascript/|js/|css/|stylesheets/|flash/|media/|static/|robots.txt|humans.txt|favicon.ico) {
#root /root/www/easynode/public;
#access_log off;
#expires max;
#}
location ~ .*\.(woff|eot|svg|ttf|favicon.ico|gif|jpg|jpeg|png|bmp|swf)$
{
access_log off;
log_not_found off;
expires max;
root /root/www/easynode/public; #重点
}
location ~ .*\.(js|css)?$
{
access_log off;
log_not_found off;
expires max;
root /root/www/easynode/public; #重点
}
#location /errors {
# internal;
# alias /usr/local/silly_face_society/node/public/errors;
#}
location / {
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_set_header Connection "";
proxy_http_version 1.1;
proxy_cache one;
proxy_cache_key sfs$request_uri$scheme;
proxy_pass http://silly_face_society_upstream;
}
}
}
9,为nginx添加额外模块
(1)./nginx -V //获取到当前的参数
例如: --prefix=/etc/nginx --conf-path=/etc/nginx/nginx.conf --with-openssl=/root/openssl-1.1.0e --with-http_stub_status_module
(2)例如安装下载nginx-rtmp-module模块,去下载该模块解压获取到该模块的目录。
/root/nginx-rtmp-module-master //解压后的路径
(3)在nginx根目录下看是否有configure文件,有可以进行安装,追加模块安装
./configure --prefix=/etc/nginx --conf-path=/etc/nginx/nginx.conf --with-openssl=/root/openssl-1.1.0e --with-http_stub_status_module --add-module=/root/nginx-rtmp-module-master
make //在接着执行,千万不要执行make install
(4)到sbin目录下备份一下原来的nginx文件,例如改成nginx_bak
(5)到objs目录下把nginx复制到sbin目录中即可,之后到sbin目录执行./nginx -V看看是否追加了安装的模块