各位读者大家好,这里是Galaxy,基于最近对于Nginx的学习,我写下了这些内容,希望内容可以帮助到大家!
安装必备软件
[root@localhost~]# yum -y gcc gcc-g++ wget
如果你的虚拟机是全新安装的或者有什么原因导致无法安装或者是yum进程被占用,解决方法如下:
[root@localhost~]# rm -f /var/run/yum.pid
然后重新下载既可
下载nginx(也可以从官网获取)这里使用wget下载
[root@localhost~]# wget http://nginx.org/download/nginx-1.10.1.tar.gz
更新时间(可有可无)
[root@localhost~]# yum -y install ntpdate
[root@localhost~]# ntpdate cn.pool.ntp.org
[root@localhost~]# date
安装
[root@localhost~]# tar -zxvf nginx-1.10.1.tar.gz
[root@localhost~]# cd nginx-1.10.1
[root@localhost nginx-1.10.1]# ls
ls之后可以查看到nginx目录之下有auto目录、configure文件、html目录、src目录、conf目录
安装依赖
[root@localhost~]# yum -y install pcre-devel openssl-devel
编译安装
[root@localhost~]# cd nginx-1.10.1
[root@localhost nginx-1.10.1]# ./configure \
--prefix=/usr/local/nginx \
--with-http_ssl_module
[root@localhost nginx-1.10.1]# make && make install
启动nginx
[root@localhost~]# cd /usr/local/nginx/sbin
[root@localhost sbin]# ./nginx
此时nginx已经启动,在浏览器中输入虚拟机的ip地址既可查看(需要注意的是我们要打开80端口)
[root@localhost~]# iptables -I INPUT -p tcp --dport 80 -j ACCEPT
查看防火墙状态
[root@localhost~]# service iptables status
保存到防火墙规则中
[root@localhost~]# service iptables save
重启iptables服务
[root@localhost~]# service iptables restart
我们需要注意一下,如果你无法使用iptables请先安装,代码如下:
[root@localhost~]# yum -y install iptables
[root@localhost~]# yum -y install iptables-services
至此80端口打开,输入ip地址可以查看Nginx是否安装成功
如果成功则会看到Welcome to nginx!的页面
查看进程
[root@localhost~]# ps aux | grep nginx
当看到主进程和工作进程的时候,说明Nginx已经启动
停止Nginx服务
[root@localhost sbin]# ./nginx -s stop
#或者是
[root@localhost sbin]# ./nginx -s quit
添加到环境变量
[root@localhost~]# echo $PATH
/usr/locah/sbin:/usr/local/bin:/sbin:/usr/sbin:/usr/bin:/root/bin
[root@localhost~]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/nginx
从此,我们创建了nginx的软链接,可以在任意目录下直接使用nginx命令来控制nginx服务。
#启动服务
[root@localhost~]# nginx
#重启服务
[root@localhost~]# nginx -s reload
#停止服务
[root@localhost~]# nginx -s quit
参考书目[Nginx高性能服务器实战教程].