其实Nginx的安装非常简单,直接一行简单的命令yum install nginx就搞定了。但是很多时候,我们所处的生产环境可能还未通公网,这个时候,你就需要用源码来安装nginx了。
其实源码安装nginx也不复杂,因为nginx不需要太多的依赖,但是想要实现yum安装后的各种配置,例如你可以使用systemctl status nginx这样的命令来查看nginx的运行情况,就需要进行一定的配置;关于yum和源码安装的区别,可以参见文章末尾的致谢和参考部分。
这篇教程就介绍一下如何用源码安装nginx并配置过后实现和yum安装一样的效果。
开发环境#
按照惯例,还是列一下我们用到的系统环境和软件版本信息:
- RHEL/CentOS 7
- nginx 1.14.2
安装包下载#
既然没有办法连接外网,我们就先把nginx的源码安装包下载下来,然后再上传到所在的服务器上。下载地址在http://nginx.org/en/download.html ↗,建议下载stable版本。笔者写这篇博客时,稳定的版本是1.14.2。
之后上传到服务器上,我们假设上传到的目录是/app/nginx,并且我们已经cd到了该路径下,那么在当前目录下,应该有一个nginx-1.14.2.tar.gz的压缩包,我们通过下面的命令解压缩文件
1
$ tar -xzf nginx-1.14.2.tar.gz
在当前路径下会出现一个nginx-1.14.2的文件夹,我们cd到其中,准备下一步操作。
$ cd nginx-1.14.2bash准备工作#
依赖包检测#
因为我们要用nginx的c源码来打包编译,所以需要确保你的生产环境有如下的包已经安装好:
$ rpm -q gcc pcre pcre-devel zlib zlib-devel openssl openssl-develbash这些依赖包都是比较基础的,一般的生产环境都会具备;如果没有,请参见我的这篇博文【Oracle】在RedHat上静默安装Oracle 11g及初始化配置全过程中的依赖检测部分,并用iSO安装光盘挂载yum源进行安装。
用户和用户组#
接下来我们需要创建nginx用户和组
$ groupadd ngxin
$ useradd -g ngxin nginxbash创建相关文件夹#
我们需要创建相关文件夹并修改相应的权限
$ mkdir -p /usr/share/nginx/html
$ chmod -R 755 /usr/share/nginx
$ mkdir -p /etc/nginx/conf.d
$ mkdir -p /usr/lib64/nginx/modules
$ mkdir -p /var/cache/nginx/client_tempbash源码安装#
确保你还在nginx的源码文件夹下/app/nginx/nginx-1.14.2
执行下面的语句确保你的nginx和通过yum安装的保持一致
./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'bash注:命令比较长,请确保复制所有内容,为方便显示查看,在下面重新显示一边,折行用\表示
./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules \
--conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log \
--pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp \
--http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp \
--http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx \
--group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module \
--with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module \
--with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module \
--with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module \
--with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module \
--with-stream_ssl_preread_module \
--with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' \
--with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'bash等待一段时间后,确保命令执行完后,就可以通过make && make install命令进行安装了。
验证是否安装成功:
$ /usr/sbin/nginx -Vbash整理文件#
安装成功之后,我们会在/etc/nginx目录下看到很多文件,其中html文件夹不需要,我们可以直接删掉,因为我们已经为nginx创建好了根目录/usr/share/nginx/html并且可以通过nginx.conf文件来进行配置。所以,可以将该文件夹整理成如下的样子:

软连接创建#
确保你已经处于/etc/nginx目录中,通过下面的命令创建软连接
$ ln -s /usr/lib64/nginx/modules modulesbash文件整理#
删除多余的default文件,移动一些文件到conf.d目录中。
修改nginx.conf文件成如下内容(注:仅仅是最基本的nginx配置)
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}plaintext并且在conf.d中新建一个默认的配置/etc/nginx/conf.d/defautl.conf
server {
listen 80;
server_name localhost;
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}plaintext实现systemctl命令#
如果要实现systemctl start nginx这样的命令,只需要最后一步。
创建nginx.service文件
$ vim /usr/lib/systemd/system/nginx.servicebash接着写入一下内容
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target
[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID
[Install]
WantedBy=multi-user.targetplaintext完成以上步骤之后,我们在命令行中输入systemctl start nginx看看是否就可以启动nginx了?
如果弹出来错误提示,我们可以用systemctl status nginx来查看具体出错的原因,之后,你就可以像用yum安装后一样的操作方式来自由控制nginx了。