1、创建nignx用户
/usr/sbin/groupadd -f nginx
/usr/sbin/useradd -g nginx nginx
2、安装依赖
yum install gcc gcc-c++ -y #默认有的话无须安装
yum install -y pcre-devel openssl-devel #依赖(支持Nginx服务访问,以https方式访问)
3、下载软件包&编译安装
wget -q http://nginx.org/download/nginx-1.12.2.tar.gz #下载软件包 tar xf nginx-1.12.2.tar.gz cd nginx-1.12.2 ./configure --prefix=/usr/local/nginx-1.12.2 --user=nginx --group=nginx --with-http_stub_status_module --with-http_ssl_module #根据你的需求添加编译时要带的模块 make && make install
4、软链接启动
ln -s /usr/local/nginx-1.12.2 /usr/local/nginx # 这条ln命令的意义十分深远重大。这是生产环境的经验。 # 将Nginx安装路径通过软链接方式更改为/usr/local/nginx/,方便人员使用。 # 安装时指定版本号路径是为了便于查看分区当前使用的版本,也方便以后升级。 /usr/local/nginx/sbin/nginx #启动nginx 若报错说明没有创建nginx用户或者是 ln -s /usr/local/nginx-1.12.2/sbin/nginx /usr/local/sbin/ #做条软连接直接用nginx启动 在nginx.conf中 把user nobody的注释去掉既可 netstat -lntup|grep 80 查看是否有80端口
5、查看nginx的编译参数
#/usr/local/nginx/sbin/nginx -V nginx version: nginx/1.12.2 built by gcc 4.4.7 20120313 (Red Hat 4.4.7-18) (GCC) built with OpenSSL 1.0.1e-fips 11 Feb 2013 TLS SNI support enabled configure arguments: --prefix=/usr/local/nginx-1.12.2 --user=nginx --group=nginx --with-http_stub_status_module --with-http_ssl_module
6、检查语法&平滑重启
nginx -t #检查语法是否正常
nginx -s reload #平滑重启
7、精简nginx配置文件
egrep -v "#|^$" nginx.conf.default >nginx.conf #精简化/最小化默认nginx.conf配置文件信息
8、配置nginx负载均衡
# vim conf/nginx.conf worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; upstream wang.com{ #服务器集群的名字 server 10.10.16.223:8080 weight=1; server 10.10.16.252:8080 weight=2; } server { listen 80; server_name localhost; location / { proxy_pass http://wang.com; #以上面对应 proxy_redirect default; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }