centos7 安装nginx

 

 

 

yum install gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel -y

 

wget  http://nginx.org/download/nginx-1.6.3.tar.gz
tar -zxvf nginx-1.6.3.tar.gz

cd nginx-1.6.3

./configure --prefix=/usr/local/nginx --conf-path=/usr/local/nginx/conf/nginx.conf --pid-path=/usr/local/nginx/conf/nginx.pid --lock-path=/var/lock/nginx.lock --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --with-http_gzip_static_module --http-client-body-temp-path=/var/temp/nginx/client --http-proxy-temp-path=/var/temp/nginx/proxy --http-fastcgi-temp-path=/var/temp/nginx/fastcgi --http-uwsgi-temp-path=/var/temp/nginx/uwsgi --http-scgi-temp-path=/var/temp/nginx/scgi --with-http_stub_status_module --with-http_ssl_module

注意:此处如果出现操作(我的虚拟机是最小安装的,碰到很多死坑,yum安装各种依赖包,最后还会报这个错,亲测,可用)

checking for OS
 + Linux 3.10.0-957.el7.x86_64 x86_64
checking for C compiler ... not found

排查思路:

1、网上找一个hello world C程序代码,尝试编译,提示 cannot find 'ld'

复制代码
[root@test ~]# vim hello_world.c
---------------------------------------
#include <stdio.h>
void main()
{
        printf("Hello World \n");
}
---------------------------------------
[root@test ~]# gcc hello_world.c 
collect2: fatal error: cannot find 'ld'
compilation terminated.

2、到另一台可正常编译电脑,查找ld位置

[root@xdc tmp]# which ld
/usr/bin/ld

3、回到问题机器,发现/usr/bin/ld有这个文件,但是which ld,找不到该文件

[root@test]# ll /usr/bin/ld
lrwxrwxrwx. 1 root root 20 May  1 17:22 /usr/bin/ld -> /etc/alternatives/ld
[root@test nginx-1.12.2]# which ld
/usr/bin/which: no ld in (/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin)

6、ll /usr/bin/ld 发现该文件最终链接到/usr/bin/ld.bfd,可软连接回来

[root@test nginx-1.12.2]# ln -s /etc/alternatives/ld /usr/bin/ld

make && make install
whereis nginx
cd /usr/local/nginx/sbin/

 ./nginx(如果出现下面报错)

 mkdir -p /var/temp/nginx 
./nginx

netstat -anput

 

开机自启动

 

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

vi /lib/systemd/system/nginx.service

内容如下

1
2
3
4
5
6
7
8
9
10
11
12
13
[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

[Unit]:服务的说明
Description:描述服务
After:描述服务类别
[Service]服务运行参数的设置
Type=forking是后台运行的形式
ExecStart为服务的具体运行命令
ExecReload为重启命令
ExecStop为停止命令
PrivateTmp=True表示给服务分配独立的临时空间
注意:[Service]的启动、重启、停止命令全部要求使用绝对路径
[Install]运行级别下服务安装的相关设置,可设置为多用户,即系统运行级别为3

保存退出。

2.设置开机启动

1
systemctl enable nginx.service

3.其他命令

启动nginx服务

设置开机自启动

停止开机自启动

查看服务当前状态

重新启动服务

查看所有已启动的服务

posted @ 2019-02-27 14:58  禾子、  阅读(179)  评论(0编辑  收藏  举报