[nginx学习之道]linux的nginx安装

准备:首先要安装下一些gcc库用于编译 和一些nginx的扩展lib包:linux

[root@localhost nginx-1.8.1]# yum -y install gcc gcc-c++ autoconf automake zlib zlib-devel openssl openssl-devel pcre pcre-devel

1.首先去nginx的官网选择要安装的nginx版本并复制其下载的url,并在linux用wget进行下载,tar解压和进入安装的源码目录:nginx

[root@localhost ~]# wget -c http://nginx.org/download/nginx-1.8.1.tar.gz && tar zxvf ./nginx-1.8.1.tar.gz && cd ./nginx-1.8.1

而后根据本身的状况以及./configure --help命令查看下一些主要的配置选项:而后进行配置make三部中的一部./configurec++

[root@localhost nginx-1.8.1]# ./configure \
 --prefix=/usr/local/nginx \
 --sbin-path=/usr/local/nginx/sbin/ \
 --conf-path=/usr/local/nginx/conf/nginx.conf \
 --error-log-path=/var/log/nginx/error.log \
 --pid-path=/var/run/nginx/nginx.pid \
 --lock-path=/var/lock/nginx.lock \
 --user=nginx \
 --group=nginx \
 --with-http_ssl_module \
 --with-http_flv_module \
 --with-http_gzip_static_module \
 --http-log-path=/var/log/nginx/access.log \
 --http-client-body-temp-path=/var/tmp/nginx/cilent/ \
 --http-proxy-temp-path=/var/tmp/nginx/proxy/ \
 --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/

遇到的一些问题:服务器

问题1:(没有安装gcc)测试

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

./configure: error: C compiler cc is not found
解决:
[root@localhost nginx-1.8.1]# yum -y install gcc gcc-c++ autoconf automake

问题2:(没有安装一些库,由于在./configure中我用到了 例如ssl等等这个根据本身的实际需求进行安装)ui

./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.
解决:
[root@localhost nginx-1.8.1]# yum -y install zlib zlib-devel openssl openssl-devel pcre pcre-devel

好接下来用make 去尝试编译下检测下是否有问题(不少时候咱们习惯用 make && make install直接进编译安装,那是由于我在下面已经作了测试没有问题在搭建生产环境的时候为了节约时间才这样作的)url

[root@localhost nginx-1.8.1]# make

若是没有出现error的字眼就说明make检测没有问题。这时候运行make install就会不多有错误。spa

[root@localhost nginx-1.8.1]# make install

Ok到此已经安装完成:code

这个时候我尝试启动下:(根据我上面的./configure 的配置sbin-path是在/usr/local/nginx/sbin/nginx 个人默认配置文件在/usr/local/nginx/conf/nginx.conf)blog

[root@localhost nginx-1.8.1]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nginx: [emerg] getpwnam("nginx") failed

上面错误提示我没有nginx这个用户和主:两种方式1.将nginx.conf中的user和group修改为已经存在的其余用户。不建议用root;2.建立我指定的用户和组命令以下:

[root@localhost nginx-1.8.1]# /usr/sbin/groupadd -f nginx
[root@localhost nginx-1.8.1]# /usr/sbin/useradd -g nginx nginx

在尝试启动还有错误提示:

[root@localhost nginx-1.8.1]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nginx: [emerg] mkdir() "/var/tmp/nginx/cilent/" failed (2: No such file or directory)
[root@localhost nginx-1.8.1]# mkdir /var/tmp/nginx/cilent
mkdir: 没法建立目录"/var/tmp/nginx/cilent": 没有那个文件或目录

解决办法建立:

[root@localhost nginx-1.8.1]# mkdir -p /var/tmp/nginx/cilent

而后启动:

[root@localhost nginx-1.8.1]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

OK没有报错:确认是否启动能够用ps -ef | grep nginx去检索下(这里有个技巧,通常nginx的启动都是这种形式,若是你的linux服务器上有不少的nginx.conf这个命令能够帮你更快的锁定如今用的配置文件。)

[root@localhost nginx-1.8.1]# ps -ef | grep nginx
root      42388      1  0 15:58 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nginx     42389  42388  0 15:58 ?        00:00:00 nginx: worker process
root      42391  10803  0 15:58 pts/1    00:00:00 grep --color=auto nginx

解释下上面的参数   master process 是主进程的意思  pid是 42388 下面的是子进程。

ok这个进程咱们其实还能够去一个文件中看到,对就是咱们在./configure 的配置的pid文件:cat下这个文件会看到里面已经有了对应的pid号:

[root@localhost nginx-1.8.1]# cat /var/run/nginx/nginx.pid 
42388

这个pid的做用可让咱们在操做nginx和方便:好比说重启 或者中止服务(kill) 或者平滑重启。

从容中止nginx:
kill - QUIT `/var/run/nginx/nginx.pid`
快速中止:
kill - TERM `/var/run/nginx/nginx.pid`
强制中止:
kill - 9 `/var/run/nginx/nginx.pid`
以上均可以直接指定pid号例如
kill - QUIT 42388
相关文章
相关标签/搜索