您的位置:首页 > 运维架构 > Nginx

CentOS7安装Nginx1.10.1详细图解教程

2017-08-29 18:37 639 查看
1.下载文件并安装gcc

新建/usr/local/nginx,并赋予最高权限

[root@localhost ~]# mkdir /usr/local/nginx

[root@localhost ~]# chmod 777 /usr/local/nginx

在Windows系统下载nginx,openssl ,zlib,pcre,通Xftp上传到linux系统的/usr/local/nginx

下载openssl :http://download.csdn.net/download/yuanyuan_186/9503203

下载zlib:http://download.csdn.net/download/qin1174586290/6364757

下载pcre:http://download.csdn.net/download/zlyzheng/8301861

下载nginx:https://nginx.org/download/nginx-1.10.1.tar.gz

查看是否安装gcc,输入rpm -qa | grep gcc

[root@localhost ~]# rpm -qa | grep gcc
gcc-c++-4.8.2-16.el7.x86_64
libgcc-4.8.2-16.el7.x86_64
gcc-4.8.2-16.el7.x86_64
gcc-gfortran-4.8.2-16.el7.x86_64


出现gcc-c++,libgcc,gcc说明已经安装。

否则在线安装gcc,安装gcc输入yum install gcc-c++

[root@localhost ~]# yum install gcc-c++

2.编译安装

安装和编译openssl

[root@localhost ~]# cd /usr/local/nginx/

[root@localhost nginx]# tar zxvf openssl-fips-2.0.12.tar.gz

[root@localhost nginx]# cd openssl-fips-2.0.12

[root@localhost openssl-fips-2.0.12]# ./config && make && make install

安装和编译pcre

[root@localhost ~]# cd /usr/local/nginx/

[root@localhost nginx]# tar zxvf pcre-8.39.tar.gz

[root@localhost nginx]# cd pcre-8.39

[root@localhost pcre-8.39]# ./configure && make && make install

安装和编译zlib

[root@localhost ~]# cd /usr/local/nginx/

[root@localhost nginx]# tar zxvf zlib-1.2.8.tar.gz

[root@localhost nginx]# cd zlib-1.2.8

[root@localhost cd zlib-1.2.8]# ./configure && make && make install

安装和编译nginx

[root@localhost ~]# cd /usr/local/nginx/

[root@localhost nginx]# tar zxvf nginx-1.10.1.tar.gz

[root@localhost nginx]# cd nginx-1.10.1

[root@localhost nginx]# mv nginx-1.10.1 nginx10

[root@localhost nginx10] ./configure && make && make install

3.配置开放防火墙80端口

编辑文件/etc/sysconfig/iptables

[root@localhost nginx]# vi + /etc/sysconfig/iptables

添加内容

-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT




保存退出

先按esc,然后按:键盘,在这个输入wq,然后按回车即可保存退出

重启防火墙

[root@localhost nginx]# service iptables restart

3.启动nginx

[root@localhost nginx]# cd /usr/local/nginx/sbin/

[root@localhost sbin]# ./nginx

查看nginx状态ps -ef | grep nginx

[root@localhost sbin]# ps -ef | grep nginx

root 13502 1 0 18:16 ? 00:00:00 nginx: master process ./nginx

nobody 13503 13502 0 18:16 ? 00:00:00 nginx: worker process

root 13521 4097 0 18:16 pts/1 00:00:00 grep –color=auto nginx

浏览器访问地址http://192.168.3.128/



4.停止nginx

[root@localhost sbin]# cd /usr/local/nginx/sbin/

[root@localhost sbin]# ./nginx -s stop

查看nginx状态ps -ef | grep nginx

[root@localhost sbin]# ps -ef | grep nginx

root 13553 4097 0 18:19 pts/1 00:00:00 grep –color=auto nginx

./nginx -s stop

./nginx -s quit

./nginx -s reload

./nginx -s stop:停止nginx

./nginx -s quit:此方式停止步骤是待nginx进程处理任务完毕进行停止。

./nginx -s stop:此方式相当于先查出nginx进程id再使用kill命令强制杀掉进程。

5.nginx开机启动

5.1 在系统服务目录里创建nginx.service文件

[root@localhost ~]# vi /lib/systemd/system/nginx.service

添加内容

[Unit]
Description=nginx
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true

[Install]
WantedBy=multi-user.target


保存退出

先按esc,然后按:键盘,在这个输入wq,然后按回车即可保存退出

内容说明:

Description:描述服务

After:描述服务类别

[Service]服务运行参数的设置

Type=forking是后台运行的形式

ExecStart为服务的具体运行命令

ExecReload为重启命令

ExecStop为停止命令

PrivateTmp=True表示给服务分配独立的临时空间

注意:[Service]的启动、重启、停止命令全部要求使用绝对路径

[Install]运行级别下服务安装的相关设置,可设置为多用户,即系统运行级别为3

5.2 设置开机启动

[root@localhost ~]# systemctl enable nginx.service

5.3 其他命令

启动nginx服务

[root@localhost ~]# systemctl start nginx.service 

设置开机自启动

[root@localhost ~]# systemctl enable nginx.service

停止开机自启动

[root@localhost ~]# systemctl disable nginx.service

查看服务当前状态

[root@localhost ~]# systemctl status nginx.service

重新启动服务

[root@localhost ~]# systemctl restart nginx.service 

查看所有已启动的服务

[root@localhost ~]# systemctl list-units –type=service
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: