🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
## 安装Nginx 1.14.0 参考文章: [Centos6.5编译安装Nginx-1.6.2和简单配置](https://my.oschina.net/kcw/blog/318309) [RedHat 7 编译安装Nginx 1.12并配置WEB站点](http://blog.51cto.com/jiangjianlong/2105585) [CentOS 7.2.1511编译安装Nginx1.10.1+MySQL5.6.33+PHP5.6.26](https://www.osyunwei.com/archives/9749.html) ### 下载Nginx源码包 nginx下载站点:`http://nginx.org/en/download.html`,目前稳定版是`nginx-1.14.0`,我们下载源码包: ~~~ http://nginx.org/download/nginx-1.14.0.tar.gz ~~~ 下载并解包: ~~~ [root@localhost packages]# wget http://nginx.org/download/nginx-1.14.0.tar.gz [root@localhost packages]# tar zxf nginx-1.14.0.tar.gz [root@localhost packages]# cd nginx-1.14.0 ~~~ 添加用户和组 ~~~ [root@localhost nginx-1.14.0]# groupadd nginx [root@localhost nginx-1.14.0]# useradd -g nginx nginx ~~~ 依赖包安装: Nginx的依赖包有:PCRE,zlib,OpenSSL。这些包我们在安装PHP时已经安装过了,所以不需要再安装了。 创建目录: 创建编译参数中用到的目录`/var/tmp/nginx/client/`,否则安装完nginx后启动会报该目录不存在。 ~~~ [root@localhost nginx-1.14.0]# mkdir -p /var/tmp/nginx/client/ ~~~ 开始安装: ~~~ [root@localhost nginx-1.14.0]# ./configure --prefix=/usr/local/nginx --sbin-path=/usr/local/nginx/sbin/nginx --conf-path=/usr/local/nginx/conf/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/lock/nginx.lock --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module --with-http_gzip_static_module --http-client-body-temp-path=/var/tmp/nginx/client/ --http-proxy-temp-path=/var/tmp/nginx/proxy/ --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi --http-scgi-temp-path=/var/tmp/nginx/scgi --with-pcre [root@localhost nginx-1.14.0]# make [root@localhost nginx-1.14.0]# make install ~~~ 安装完成,执行`nginx -V`检查是否安装成功 ~~~ [root@localhost nginx-1.14.0]# /usr/local/nginx/sbin/nginx -V ~~~ 结果如下: :-: ![](https://box.kancloud.cn/e29420fa8e1588b6bac622ffeb9d5783_748x160.jpg) 检查Nginx环境及配置: ~~~ [root@localhost nginx-1.14.0]# /usr/local/nginx/sbin/nginx -t ~~~ 结果如下: :-: ![](https://box.kancloud.cn/e06269f7fc423fe5b7fa3a8af5c4e528_746x40.jpg) 配置nginx服务: 创建`/etc/init.d/nginx`文件 ~~~ [root@localhost nginx-1.14.0]# vim /etc/init.d/nginx ~~~ 文件内容如下: ~~~ #!/bin/sh # # nginx - this script starts and stops the nginx daemon # # chkconfig: - 85 15 # description: Nginx is an HTTP(S) server, HTTP(S) reverse \ # proxy and IMAP/POP3 proxy server # processname: nginx # config: /usr/local/nginx/conf # config: /etc/sysconfig/nginx # pidfile: /var/run/nginx.pid # Source function library. . /etc/rc.d/init.d/functions # Source networking configuration. . /etc/sysconfig/network # Check that networking is up. [ "$NETWORKING" = "no" ] && exit 0 nginx="/usr/local/nginx/sbin/nginx" prog=$(basename $nginx) NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf" [ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx lockfile=/var/lock/nginx.lock make_dirs() { # make required directories user=`nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -` options=`$nginx -V 2>&1 | grep 'configure arguments:'` for opt in $options; do if [ `echo $opt | grep '.*-temp-path'` ]; then value=`echo $opt | cut -d "=" -f 2` if [ ! -d "$value" ]; then # echo "creating" $value mkdir -p $value && chown -R $user $value fi fi done } start() { [ -x $nginx ] || exit 5 [ -f $NGINX_CONF_FILE ] || exit 6 make_dirs echo -n $"Starting $prog: " daemon $nginx -c $NGINX_CONF_FILE retval=$? echo [ $retval -eq 0 ] && touch $lockfile return $retval } stop() { echo -n $"Stopping $prog: " killproc $prog -QUIT retval=$? echo [ $retval -eq 0 ] && rm -f $lockfile return $retval } restart() { configtest || return $? stop sleep 1 start } reload() { configtest || return $? echo -n $"Reloading $prog: " killproc $nginx -HUP RETVAL=$? echo } force_reload() { restart } configtest() { $nginx -t -c $NGINX_CONF_FILE } rh_status() { status $prog } rh_status_q() { rh_status >/dev/null 2>&1 } case "$1" in start) rh_status_q && exit 0 $1 ;; stop) rh_status_q || exit 0 $1 ;; restart|configtest) $1 ;; reload) rh_status_q || exit 7 $1 ;; force-reload) force_reload ;; status) rh_status ;; condrestart|try-restart) rh_status_q || exit 0 ;; *) echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}" exit 2 esac ~~~ 创建日志目录: ~~~ [root@localhost nginx-1.14.0]# mkdir /usr/local/nginx/logs [root@localhost nginx-1.14.0]# chown -R www.www /usr/local/nginx/logs ~~~ 加入服务: ~~~ [root@localhost nginx-1.14.0]# chmod a+wrx /etc/init.d/nginx [root@localhost nginx-1.14.0]# chkconfig --add nginx [root@localhost nginx-1.14.0]# chkconfig nginx on ~~~ 启动服务: ~~~ [root@localhost nginx-1.14.0]# service nginx start [root@localhost nginx-1.14.0]# service nginx status ~~~ :-: ![](https://box.kancloud.cn/edb55dc6d8f36cd52131dc685d5734ff_746x209.jpg) 关闭防火墙: ~~~ [root@localhost nginx-1.14.0]# systemctl stop firewalld.service #停止firewall [root@localhost nginx-1.14.0]# systemctl disable firewalld.service #禁止firewall开机启动 ~~~ 关闭SELINUX: ~~~ [root@localhost nginx-1.14.0]# vi /etc/selinux/config #SELINUX=enforcing #注释掉 #SELINUXTYPE=targeted #注释掉 SELINUX=disabled #增加 :wq! #保存退出 setenforce 0 #使配置立即生效 ~~~ 安装iptables防火墙: ~~~ [root@localhost nginx-1.14.0]# yum install iptables-services #安装 [root@localhost nginx-1.14.0]# vi /etc/sysconfig/iptables #编辑防火墙配置文件 # sample configuration for iptables service # you can edit this manually or use system-config-firewall # please do not ask us to add additional ports/services to this default configuration *filter :INPUT ACCEPT [0:0] :FORWARD ACCEPT [0:0] :OUTPUT ACCEPT [0:0] -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT -A INPUT -p icmp -j ACCEPT -A INPUT -i lo -j ACCEPT -A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT -A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT -A INPUT -p tcp -m state --state NEW -m tcp --dport 3306 -j ACCEPT -A INPUT -j REJECT --reject-with icmp-host-prohibited -A FORWARD -j REJECT --reject-with icmp-host-prohibited COMMIT :wq! #保存退出 systemctl restart iptables.service #最后重启防火墙使配置生效 systemctl enable iptables.service #设置防火墙开机启动 /usr/libexec/iptables/iptables.init restart #重启防火墙 ~~~ 测试访问: 通过ip访问服务器 :-: ![](https://box.kancloud.cn/c258893f0633fb8149345fa8044cfc5e_561x218.jpg) Nginx安装完成。关闭虚拟机,拍个快照吧 配置虚拟主机 编辑`/usr/local/nginx/conf/nginx.conf`,在最后的花括号“}”上一行增加一行:`include vhost/*.conf;`,保存。 创建目录vhost: ~~~ [root@localhost nginx-1.14.0]# mkdir /usr/loca/nginx/conf/vhost ~~~ 在vhost目录下创建虚拟主机配置文件即可,扩展名为`.conf`,内容为: ~~~ server { listen 80; server_name www.abc.com; location / { root /data/www/abc/public; index index.php index.html index.htm; } } ~~~