centos + nginx + docsify在服务器上搭建一个简单博客(2): 编译安装nginx

我是参考的这篇文章,照着做成功安装是没有问题的

https://www.howtoforge.com/how-to-build-nginx-from-source-on-centos-7/

说一下差别,我使用的是root账户编译的,可能也是这个原因造成了后期权限的混乱。

准备工作

更新一下yum源

yum update -y

安装一些必要的工具

yum install -y vim curl wget tree

从源码编译

需要C的编译工具

yum groupinstall -y 'Development Tools'

虽然现在nginx的版本以及到了1.19,不过为了避免其它依赖组件不一致造成不必要的麻烦,我还是下载了1.15.7。

wget https://nginx.org/download/nginx-1.15.7.tar.gz && tar zxvf nginx-1.15.7.tar.gz

下载nginx编译依赖

# PCRE version 8.42
wget https://ftp.pcre.org/pub/pcre/pcre-8.42.tar.gz && tar xzvf pcre-8.42.tar.gz

# zlib version 1.2.11
wget https://www.zlib.net/zlib-1.2.11.tar.gz && tar xzvf zlib-1.2.11.tar.gz

# OpenSSL version 1.1.1a
wget https://www.openssl.org/source/openssl-1.1.1a.tar.gz && tar xzvf openssl-1.1.1a.tar.gz

使用yum安装nginx的一些依赖

yum install -y perl perl-devel perl-ExtUtils-Embed libxslt libxslt-devel libxml2 libxml2-devel gd gd-devel GeoIP GeoIP-devel

现在进入到解压后的nginx源码目录

cd nginx-1.15.7

可以将nginx的文档放到系统目录

cp ~/nginx-1.15.7/man/nginx.8 /usr/share/man/man8
gzip /usr/share/man/man8/nginx.8
ls /usr/share/man/man8/ | grep nginx.8.gz
man nginx

运行configure, compile,最后安装nginx

./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 \
            --pid-path=/var/run/nginx.pid \
            --lock-path=/var/run/nginx.lock \
            --user=nginx \
            --group=nginx \
            --build=CentOS \
            --builddir=nginx-1.15.7 \
            --with-select_module \
            --with-poll_module \
            --with-threads \
            --with-file-aio \
            --with-http_ssl_module \
            --with-http_v2_module \
            --with-http_realip_module \
            --with-http_addition_module \
            --with-http_xslt_module=dynamic \
            --with-http_image_filter_module=dynamic \
            --with-http_geoip_module=dynamic \
            --with-http_sub_module \
            --with-http_dav_module \
            --with-http_flv_module \
            --with-http_mp4_module \
            --with-http_gunzip_module \
            --with-http_gzip_static_module \
            --with-http_auth_request_module \
            --with-http_random_index_module \
            --with-http_secure_link_module \
            --with-http_degradation_module \
            --with-http_slice_module \
            --with-http_stub_status_module \
            --with-http_perl_module=dynamic \
            --with-perl_modules_path=/usr/lib64/perl5 \
            --with-perl=/usr/bin/perl \
            --http-log-path=/var/log/nginx/access.log \
            --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 \
            --with-mail=dynamic \
            --with-mail_ssl_module \
            --with-stream=dynamic \
            --with-stream_ssl_module \
            --with-stream_realip_module \
            --with-stream_geoip_module=dynamic \
            --with-stream_ssl_preread_module \
            --with-compat \
            --with-pcre=../pcre-8.42 \
            --with-pcre-jit \
            --with-zlib=../zlib-1.2.11 \
            --with-openssl=../openssl-1.1.1a \
            --with-openssl-opt=no-nextprotoneg \
            --with-debug
make
make install

将/usr/lib64/nginx/modules链接到 /etc/nginx/modules,这是服务的标准目录

ln -s /usr/lib64/nginx/modules /etc/nginx/modules

现在nginx的指令应该可以运行了

[root@vultr ~]# nginx -V
nginx version: nginx/1.15.7 (CentOS)
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC)
built with OpenSSL 1.1.1a  20 Nov 2018
TLS SNI support enabled

为nginx增加系统组和用户

useradd --system --home /var/cache/nginx --shell /sbin/nologin --comment "nginx user" --user-group nginx

再检查一下nginx启动有没有什么问题

nginx -t

我就是在这一步失败了,失败日志:

2019/07/04 12:44:31 [emerg] 6#6: mkdir() "/var/cache/nginx/proxy_temp" failed (13: Permission denied)
nginx: [emerg] mkdir() "/var/cache/nginx/proxy_temp" failed (13: Permission denied)

说是一个路径无法创建,见招拆招,手工给他建了,要注意一下还得改一下目录的归属改为nginx用户与用户组:

mkdir -p /var/cache/nginx
chown nginx:nginx /var/cache/nginx

这样nginx就能检查通过了。

接下来为nginx创建快捷启动方式

vim /etc/systemd/system/nginx.service

把下面的文本拷贝到刚才打开的/etc/systemd/system/nginx.service里去

[Unit]
Description=nginx - high performance web server
Documentation=https://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
ExecStartPre=/usr/sbin/nginx -t -c /etc/nginx/nginx.conf
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.target

设置nginx开机时启动

systemctl enable nginx

启动nginx,如果成功的话运行这条指令你什么输出都看不到

systemctl start nginx

可以运行以下指令查看nginx运行状态

[root@vultr ~]# systemctl status nginx
● nginx.service - nginx - high performance web server

nginx运行起来了,再做一点其它的配置。

nginx默认会在/etc/nginx下创建备份的.default文件,运行以下命令删除(虽然我还不清楚这些文件的作用)

rm /etc/nginx/*.default

想要用vim编辑nginx配置文件做语法高亮的话可以将配置文件放到.vim下,注意你的解压出来的nginx路径

mkdir /root/.vim/
cp -r ~/nginx-1.15.7/contrib/vim/* /root/.vim/

创建一些必要的目录

mkdir /etc/nginx/{conf.d,snippets,sites-available,sites-enabled}

修改nginx日志文件目录权限与所属用户,这是在当初运行./configure的时候指定的

chmod 640 /var/log/nginx/*
chown nginx:adm /var/log/nginx/access.log /var/log/nginx/error.log

创建日志配置

vim /etc/logrotate.d/nginx

把下面的文本拷贝进去

/var/log/nginx/*.log {
    daily
    missingok
    rotate 52
    compress
    delaycompress
    notifempty
    create 640 nginx adm
    sharedscripts
    postrotate
        if [ -f /var/run/nginx.pid ]; then
            kill -USR1 `cat /var/run/nginx.pid`
        fi
    endscript
}

至此nginx所有安装配置完成。我是参考的这篇文章,照着做成功安装是没有问题的

https://www.howtoforge.com/how-to-build-nginx-from-source-on-centos-7/

说一下差别,我使用的是root账户编译的,可能也是这个原因造成了后期权限的混乱。

准备工作

更新一下yum源

yum update -y

安装一些必要的工具

yum install -y vim curl wget tree

从源码编译

需要C的编译工具

yum groupinstall -y 'Development Tools'

虽然现在nginx的版本以及到了1.19,不过为了避免其它依赖组件不一致造成不必要的麻烦,我还是下载了1.15.7。

wget https://nginx.org/download/nginx-1.15.7.tar.gz && tar zxvf nginx-1.15.7.tar.gz

下载nginx编译依赖

# PCRE version 8.42
wget https://ftp.pcre.org/pub/pcre/pcre-8.42.tar.gz && tar xzvf pcre-8.42.tar.gz

# zlib version 1.2.11
wget https://www.zlib.net/zlib-1.2.11.tar.gz && tar xzvf zlib-1.2.11.tar.gz

# OpenSSL version 1.1.1a
wget https://www.openssl.org/source/openssl-1.1.1a.tar.gz && tar xzvf openssl-1.1.1a.tar.gz

使用yum安装nginx的一些依赖

yum install -y perl perl-devel perl-ExtUtils-Embed libxslt libxslt-devel libxml2 libxml2-devel gd gd-devel GeoIP GeoIP-devel

现在进入到解压后的nginx源码目录

cd nginx-1.15.7

可以将nginx的文档放到系统目录

cp ~/nginx-1.15.7/man/nginx.8 /usr/share/man/man8
gzip /usr/share/man/man8/nginx.8
ls /usr/share/man/man8/ | grep nginx.8.gz
man nginx

运行configure, compile,最后安装nginx

./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 \
            --pid-path=/var/run/nginx.pid \
            --lock-path=/var/run/nginx.lock \
            --user=nginx \
            --group=nginx \
            --build=CentOS \
            --builddir=nginx-1.15.7 \
            --with-select_module \
            --with-poll_module \
            --with-threads \
            --with-file-aio \
            --with-http_ssl_module \
            --with-http_v2_module \
            --with-http_realip_module \
            --with-http_addition_module \
            --with-http_xslt_module=dynamic \
            --with-http_image_filter_module=dynamic \
            --with-http_geoip_module=dynamic \
            --with-http_sub_module \
            --with-http_dav_module \
            --with-http_flv_module \
            --with-http_mp4_module \
            --with-http_gunzip_module \
            --with-http_gzip_static_module \
            --with-http_auth_request_module \
            --with-http_random_index_module \
            --with-http_secure_link_module \
            --with-http_degradation_module \
            --with-http_slice_module \
            --with-http_stub_status_module \
            --with-http_perl_module=dynamic \
            --with-perl_modules_path=/usr/lib64/perl5 \
            --with-perl=/usr/bin/perl \
            --http-log-path=/var/log/nginx/access.log \
            --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 \
            --with-mail=dynamic \
            --with-mail_ssl_module \
            --with-stream=dynamic \
            --with-stream_ssl_module \
            --with-stream_realip_module \
            --with-stream_geoip_module=dynamic \
            --with-stream_ssl_preread_module \
            --with-compat \
            --with-pcre=../pcre-8.42 \
            --with-pcre-jit \
            --with-zlib=../zlib-1.2.11 \
            --with-openssl=../openssl-1.1.1a \
            --with-openssl-opt=no-nextprotoneg \
            --with-debug
make
make install

将/usr/lib64/nginx/modules链接到 /etc/nginx/modules,这是服务的标准目录

ln -s /usr/lib64/nginx/modules /etc/nginx/modules

现在nginx的指令应该可以运行了

[root@vultr ~]# nginx -V
nginx version: nginx/1.15.7 (CentOS)
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC)
built with OpenSSL 1.1.1a  20 Nov 2018
TLS SNI support enabled

为nginx增加系统组和用户

useradd --system --home /var/cache/nginx --shell /sbin/nologin --comment "nginx user" --user-group nginx

再检查一下nginx启动有没有什么问题

nginx -t

我就是在这一步失败了,失败日志:

2019/07/04 12:44:31 [emerg] 6#6: mkdir() "/var/cache/nginx/proxy_temp" failed (13: Permission denied)
nginx: [emerg] mkdir() "/var/cache/nginx/proxy_temp" failed (13: Permission denied)

说是一个路径无法创建,见招拆招,手工给他建了,要注意一下还得改一下目录的归属改为nginx用户与用户组:

mkdir -p /var/cache/nginx
chown nginx:nginx /var/cache/nginx

这样nginx就能检查通过了。

接下来为nginx创建快捷启动方式

vim /etc/systemd/system/nginx.service

把下面的文本拷贝到刚才打开的/etc/systemd/system/nginx.service里去

[Unit]
Description=nginx - high performance web server
Documentation=https://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
ExecStartPre=/usr/sbin/nginx -t -c /etc/nginx/nginx.conf
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.target

设置nginx开机时启动

systemctl enable nginx

启动nginx,如果成功的话运行这条指令你什么输出都看不到

systemctl start nginx

可以运行以下指令查看nginx运行状态

[root@vultr ~]# systemctl status nginx
● nginx.service - nginx - high performance web server

nginx运行起来了,再做一点其它的配置。

nginx默认会在/etc/nginx下创建备份的.default文件,运行以下命令删除(虽然我还不清楚这些文件的作用)

rm /etc/nginx/*.default

想要用vim编辑nginx配置文件做语法高亮的话可以将配置文件放到.vim下,注意你的解压出来的nginx路径

mkdir /root/.vim/
cp -r ~/nginx-1.15.7/contrib/vim/* /root/.vim/

创建一些必要的目录

mkdir /etc/nginx/{conf.d,snippets,sites-available,sites-enabled}

修改nginx日志文件目录权限与所属用户,这是在当初运行./configure的时候指定的

chmod 640 /var/log/nginx/*
chown nginx:adm /var/log/nginx/access.log /var/log/nginx/error.log

创建日志配置

vim /etc/logrotate.d/nginx

把下面的文本拷贝进去

/var/log/nginx/*.log {
    daily
    missingok
    rotate 52
    compress
    delaycompress
    notifempty
    create 640 nginx adm
    sharedscripts
    postrotate
        if [ -f /var/run/nginx.pid ]; then
            kill -USR1 `cat /var/run/nginx.pid`
        fi
    endscript
}

至此nginx所有安装配置完成。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值