centos7编译安装nginx

一、安装依赖包

yum install gcc gcc-c++ autoconf automake zlib zlib-devel openssl openssl-devel pcre-devel

二、下载

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

三、编译

tar -zxvf nginx-1.14.2.tar.gz
cd nginx-1.14.2
./configure --prefix=/usr/local/nginx  --with-http_stub_status_module --with-http_ssl_module
make && make install

 四、将nginx设置为服务

centos 7 中采用 systemd 来管理系统,我们为 nginx 创建服务文件

vim /lib/systemd/system/nginx.service
[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target

这里的 PIDFile 的配置要和前面的 配置保持一致。

设置自启动

 

[root@node2 logs]# systemctl enable nginx
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.

 

五、启动nginx

[root@node2 logs]# systemctl start nginx
Job for nginx.service failed because the control process exited with error code. See "systemctl status nginx.service" and "journalctl -xe" for details.

提示报错,查看查看状态

[root@node2 logs]# systemctl status nginx
● nginx.service - The NGINX HTTP and reverse proxy server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
   Active: failed (Result: exit-code) since 二 2019-04-23 14:06:19 CST; 15s ago
  Process: 5873 ExecStart=/usr/local/nginx/sbin/nginx (code=exited, status=1/FAILURE)
  Process: 5869 ExecStartPre=/usr/local/nginx/sbin/nginx -t (code=exited, status=0/SUCCESS)

4月 23 14:06:16 node2.jinglong nginx[5873]: nginx: [emerg] bind() to 0.0.0.0:80 failed (9...e)
4月 23 14:06:17 node2.jinglong nginx[5873]: nginx: [emerg] bind() to 0.0.0.0:80 failed (9...e)
4月 23 14:06:17 node2.jinglong nginx[5873]: nginx: [emerg] bind() to 0.0.0.0:80 failed (9...e)
4月 23 14:06:18 node2.jinglong nginx[5873]: nginx: [emerg] bind() to 0.0.0.0:80 failed (9...e)
4月 23 14:06:18 node2.jinglong nginx[5873]: nginx: [emerg] bind() to 0.0.0.0:80 failed (9...e)
4月 23 14:06:19 node2.jinglong nginx[5873]: nginx: [emerg] still could not bind()
4月 23 14:06:19 node2.jinglong systemd[1]: nginx.service: control process exited, code=e...s=1
4月 23 14:06:19 node2.jinglong systemd[1]: Failed to start The NGINX HTTP and reverse pr...er.
4月 23 14:06:19 node2.jinglong systemd[1]: Unit nginx.service entered failed state.
4月 23 14:06:19 node2.jinglong systemd[1]: nginx.service failed.
Hint: Some lines were ellipsized, use -l to show in full.

提示80端口被占用,此时执行:

[root@node2 sbin]# killall -9 nginx
-bash: killall: 未找到命令

安装killall

yum install psmisc

再执行

killall -9 nginx

启动服务

[root@node2 sbin]# systemctl start nginx
[root@node2 sbin]# 

没有报错,查看状态

[root@node2 sbin]# systemctl status nginx
● nginx.service - The NGINX HTTP and reverse proxy server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
   Active: active (running) since 二 2019-04-23 14:24:54 CST; 17s ago
  Process: 6864 ExecStop=/bin/kill -s QUIT $MAINPID (code=exited, status=1/FAILURE)
  Process: 6896 ExecStart=/usr/local/nginx/sbin/nginx (code=exited, status=0/SUCCESS)
  Process: 6893 ExecStartPre=/usr/local/nginx/sbin/nginx -t (code=exited, status=0/SUCCESS)
 Main PID: 6897 (nginx)
   CGroup: /system.slice/nginx.service
           ├─6897 nginx: master process /usr/local/nginx/sbin/nginx
           └─6898 nginx: worker process

4月 23 14:24:54 node2.jinglong systemd[1]: Starting The NGINX HTTP and reverse proxy server...
4月 23 14:24:54 node2.jinglong nginx[6893]: nginx: the configuration file /usr/local/ngin...ok
4月 23 14:24:54 node2.jinglong nginx[6893]: nginx: configuration file /usr/local/nginx/co...ul
4月 23 14:24:54 node2.jinglong systemd[1]: Started The NGINX HTTP and reverse proxy server.
Hint: Some lines were ellipsized, use -l to show in full.

此时为running,启动成功,执行

[root@node2 sbin]# curl 127.0.0.1


<head>
Welcome to nginx!

head>

Welcome to nginx!

If you see this page, the nginx web server is successfully installed and working. Further configuration is required.

For online documentation and support please refer to "http://nginx.org/">nginx.org.
Commercial support is available at "http://nginx.com/">nginx.com.

Thank you for using nginx.

获取页面成功

 建立软连接

ln -s /usr/local/nginx/sbin/nginx /usr/sbin/

此时nginx可以在任意目录下执行命令

你可能感兴趣的:(centos7编译安装nginx)