Nginx官网下载安装包-手动安装及配置自启动

如果已安装过nginx需要配置如下

进入到 /usr/lib/systemd/system 目录下,编辑文件 nginx.service

cd  /usr/lib/systemd/system
vi nginx.service

在nginx.service文件中加入以下代码

[Unit]
Description=The nginx HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target
 
[Service]
Type=forking
PIDFile=/run/nginx.pid
ExecStartPre=/usr/bin/rm -f /run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t
ExecStart=/usr/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
KillMode=process
KillSignal=SIGQUIT
TimeoutStopSec=5
PrivateTmp=true
 
[Install]
WantedBy=multi-user.target

Nginx启动服务命令

命令 功能
systemd 保存,重新加载
systemctl daemon-reload 保存,重新加载
systemctl enable nginx.service 设置nginx服务开机自启动
systemctl start nginx.service 启动nginx服务
systemctl stop nginx.service 停止nginx服务
systemctl restart nginx.service 重启nginx服务
systemctl reload nginx.service 重新读取nginx配置(这个最常用, 不用停止nginx服务就能使修改的配置生效)
/usr/local/nginx/sbin/nginx 路径启动,默认在
/usr/local/nginx/sbin/nginx -s reload 平滑重启
/usr/local/nginx/sbin/nginx -s stop 停止
/usr/local/nginx/sbin/nginx -s restart 重启

———————————未安装过请往下看————————————

1、官网下载安装包

http://nginx.org/en/download.html,选择适合Linux的版本,这里选择最新的版本,下载到本地后上传到服务器或者centos下直接wget命令下载。

切换到/usr/local目录,下载软件包

cd /usr/local
wget http://nginx.org/download/nginx-1.11.5.tar.gz

2、安装nginx

先执行以下命令,安装nginx依赖库,如果缺少依赖库,可能会安装失败,具体可以参考文章后面的错误提示信息。

yum install gcc-c++
yum install pcre
yum install pcre-devel
yum install zlib 
yum install zlib-devel
yum install openssl
yum install openssl-devel
解压安装包
tar -zxvf nginx-1.11.5.tar.gz

nginx被解压到了/usr/local/nginx-1.11.5 目录下(不要把压缩包解压到/usr/local/nginx目录下,或者将解压后的目录重命名为nginx,因为nginx会默认安装到/usr/local/nginx目录下),切换到nginx-1.11.5/目录

cd /usr/local/nginx-1.11.5/
./configure

该操作会检测当前系统环境,以确保能成功安装nginx,执行该操作后可能会出现以下几种提示:

    checking for OS
     + Linux 3.10.0-123.el7.x86_64 x86_64
    checking for C compiler ... not found
    ./configure: error: C compiler cc is not found

如果出现以上错误提示信息,执行yum install gcc-c++安装gcc,

    ./configure: error: the HTTP rewrite module requires the PCRE library.
    You can either disable the module by using --without-http_rewrite_module
    option, or install the PCRE library into the system, or build the PCRE library
    statically from the source with nginx by using --with-pcre=<path> option.

如果出现上面提示,表示缺少PCRE库

    ./configure: error: the HTTP gzip module requires the zlib library.
    You can either disable the module by using --without-http_gzip_module
    option, or install the zlib library into the system, or build the zlib library
    statically from the source with nginx by using --with-zlib=<path> option.

如果出现以上提示,表示缺少zlib库
如果没有出现./configure: error提示,表示当前环境可以安装nginx,执行make和make install编译nginx

make
make install

没有出错的话,表示nginx已经成功安装完成,默认安装位置为/usr/local/nginx,之前的/usr/local/nginx-1.11.5/可以删除掉了。
如果出现cp: 'conf/koi-win' and '/usr/local/nginx/conf/koi-win' are the same file,可能是你把安装包解压到了/usr/local/nginx目录,解决办法是将该目录重命名为其他名称后再执行make,make install.

3、配置nginx开机启动

切换到/lib/systemd/system/目录,创建nginx.service文件vim nginx.service

cd /lib/systemd/system/
vim nginx.service

文件内容如下:
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
ExecStart=``/usr/local/nginx/sbin/nginx
ExecReload=``/usr/local/nginx/sbin/nginx reload
ExecStop=``/usr/local/nginx/sbin/nginx quit
PrivateTmp=``true
[Install]
WantedBy=multi-user.target
退出并保存文件,执行systemctl enable nginx.service使nginx开机启动

systemctl enable nginx.service
systemctl start nginx.service    启动nginx
systemctl stop nginx.service    结束nginx
systemctl restart nginx.service    重启nginx

4、验证是否安装成功

输入http://服务器IP/ 如果能看到nginx的界面,就表示安装成功了

Nginx的启动、停止与重启[其他方式]

启动

启动代码格式:nginx安装目录地址 -c nginx配置文件地址
例如:

/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

停止

nginx的停止有三种方式:

从容停止

1、查看进程号

ps -ef|grep nginx
image

2、杀死进程

kill -QUIT 2072
image
快速停止

1、查看进程号

ps -ef|grep nginx
image

2、杀死进程

kill -TERM 2132 
或  kill -INT 2132
image
强制停止
pkill -9 nginx

重启

1、验证nginx配置文件是否正确

方法一:进入nginx安装目录sbin下,输入命令./nginx -t

看到如下显示nginx.conf syntax is ok

nginx.conf test is successful

说明配置文件正确!

image

方法二:在启动命令-c前加-t

image

2、重启Nginx服务

方法一:进入nginx可执行目录sbin下,输入命令
./nginx -s reload 

方法二:查找当前nginx进程号,然后输入命令:kill -HUP 进程号 实现重启nginx服务

image
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容