Linux安装配置nginx

nginx启动时报[error] invalid PID number "" in "/usr/local/var/run/nginx/nginx.pid错误

sudo nginx -s reload重新启动nginx时,报[error] invalid PID number "" in "/usr/local/var/run/nginx/nginx.pid错误

发生这个错误的原因是,nginx读取配置文件时出错,需要指定一个特定的nginx配置文件,所以解决这个问题需要先执行

sudo nginx -c /usr/local/etc/nginx/nginx.conf

然后执行 sudo nginx -s reload

https://blog.csdn.net/marco__/article/details/88634025

 

Linux 安装Nginx详细图解教程

 

进入:/usr/local/src位置

1
2
3
4
5
6
7
下载nginx:    wget http://nginx.org/download/nginx-1.8.0.tar.gz
 
下载openssl : wget http://www.openssl.org/source/openssl-fips-2.0.9.tar.gz
 
下载zlib    : wget http://zlib.net/zlib-1.2.8.tar.gz
 
下载pcre    : wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.38.tar.gz

如果没有安装c++编译环境,还得安装,通过yum install gcc-c++完成安装

 

下一步,编译安装

openssl :

1
2
3
4
5
6
7
8
9
[root@localhost] tar zxvf openssl-fips-2.0.9.tar.gz
 
  
 
[root@localhost] cd openssl-fips-2.0.9
 
  
 
[root@localhost] ./config && make && make install

  

pcre:

1
2
3
4
5
6
7
8
9
[root@localhost] tar zxvf pcre-8.36.tar.gz
 
  
 
[root@localhost] cd pcre-8.36
 
  
 
[root@localhost]  ./configure && make && make install

  

zlib:

1
2
3
4
5
6
7
8
9
[root@localhost]tar zxvf zlib-1.2.8.tar.gz
 
  
 
[root@localhost] cd zlib-1.2.8
 
  
 
[root@localhost]  ./configure && make && make install

  

最后安装nginx

1
2
3
4
5
6
7
8
9
[root@localhost]tar zxvf nginx-1.8.0.tar.gz
 
  
 
[root@localhost] cd nginx-1.8.0
 
  
 
[root@localhost]  ./configure && make && make install

  

 启动nginx

1
/usr/local/nginx/sbin/nginx



出现错误提示

1
2
3
4
5
6
7
8
9
[root@localhost lib]# error while loading shared libraries: libpcre.so.1: cannot open shared object file: No such file or directory
 
原因   在RedHat 64位机器上nginx读取的pcre文件为/lib64/libpcre.so.1文件,默认安装pcre时libpcre.so文件安装在/usr/local/lib/目录下,所以输入/opt/nginx/sbin/nginx -V 找不到文件路径!!
 
1.首先确定安装了pcre.
 
2.切换路径: cd /usr/local/lib  执行   ln -s /usr/local/lib/libpcre.so.1 /lib64/
 
3.root权限下添加软链接 /usr/local/lib/libpcre.so.1 到 /lib64/ :  ln -s /usr/local/lib/libpcre.so.1 /lib64/

  

1
ps –ef|grep nginx

  

将Nginx设置为开机自动启动

a.当上面步骤完成之后,说明安装已经完全成功了,但是每次开机我们面临的一个问题,就是每次都要执行命令(1: cd /usr/local/nginx/sbin/   2:./nginx -t),那么这时候有这个需要,设置开机自启动,开机自动启动的命令为:将Nginx的启动命令添加到/etc/rc.local,命令如下:

1
echo "/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf" >> /etc/rc.local

b.然后将Linux关机重启测试一下,如果http://IP还能够访问,则说明配置成功了,我这边已测试,配置完全成功。

回到顶部

使用server命令启动nginx服务

   a.  现在觉得启动命令太麻烦,虽然开机可以自启动,但是每次改动要重新启动nginx的话,要么输入命令,要么开机,都还不是很好,那么我们能不能创造一个更好的方式呢?当然可以,我们可以通过设置System V脚本。

  b.脚本代码如下所示:

 server命令的代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon
#
# chkconfig: - 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
#   proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /etc/nginx/nginx.conf
# config: /etc/sysconfig/nginx
# pidfile: /var/run/nginx.pid
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
    nginx="/usr/local/nginx/sbin/nginx"
    prog=$(basename $nginx)
    NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
    lockfile=/var/lock/subsys/nginx
  
start() {
    [ -x $nginx ] || exit 5
    [ -f $NGINX_CONF_FILE ] || exit 6
    echo -n $"Starting $prog: "
    daemon $nginx -c $NGINX_CONF_FILE
    retval=$?
    echo
[ $retval -eq 0 ] && touch $lockfile
    return $retval
}
  
stop() {
    echo -n $"Stopping $prog: "
    killproc $prog -QUIT
    retval=$?
    echo
[ $retval -eq 0 ] && rm -f $lockfile
    return $retval
    killall -9 nginx
}
  
restart() {
    configtest || return $?
    stop
    sleep 1
    start
}
  
reload() {
    configtest || return $?
    echo -n $"Reloading $prog: "
    killproc $nginx -HUP
    RETVAL=$?
    echo
}
  
force_reload() {
    restart
}
  
configtest() {
    $nginx -t -c $NGINX_CONF_FILE
}
  
rh_status() {
    status $prog
}
  
rh_status_q() {
    rh_status >/dev/null 2>&1
}
  
case "$1" in
    start)
        rh_status_q && exit 0
        $1
    ;;
    stop)
        rh_status_q || exit 0
        $1
    ;;
    restart|configtest)
        $1
    ;;
    reload)
        rh_status_q || exit 7
        $1
    ;;
    force-reload)
        force_reload
    ;;
    status)
        rh_status
    ;;
    condrestart|try-restart)
        rh_status_q || exit 0
    ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
        exit 2
esac
 
server命令的代码

  c.创建命令如下,手先跳转到/etc/init.d下创建nginx启动脚本文件,命令如下;

1
2
3
c.1 cd /etc/init.d/
 
c.2 vim nginx  创建一个新的nginx文件,将上面的命令代码copy到里面,然后保存

  d.修改脚本权限,命令如下:

1
chmod 755 nginx

  e.将脚本文件加入到chkconfig中  

1
chkconfig --add nginx

  f.设置nginx开机在3和5级别自动启动  

1
chkconfig --level 35 nginx on

  g.测试nginx脚本文件是否能够正常使用,命令如下,我均已测试,全部可以使用。

1
2
3
4
5
g.1  /etc/init.d/nginx restart
 
g.2  /etc/init.d/nginx reload
 
g.3   /etc/init.d/nginx stop

     

遇到 nginx: [error] invalid PID number "" in "/usr/local/var/run/nginx/nginx.pid"

解决办法:

$ sudo nginx -c /usr/local/etc/nginx/nginx.conf
 
$ sudo nginx -s reload

y

 

  到这里我们这片笔记就完成了,能帮助大家就帮到,帮不到大家,谢谢大家了,这只是学习笔记,不用较真某些东西,谢谢~~~~

 

参考:

http://www.cnblogs.com/lovexinyi8/p/5845017.html (nginx安装)

http://www.cnblogs.com/hanyinglong/p/5102141.html(nginx自动启动)

posted @   程序生(Codey)  阅读(254)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 通过 API 将Deepseek响应流式内容输出到前端
· 因为Apifox不支持离线,我果断选择了Apipost!
点击右上角即可分享
微信分享提示