CentOS7编译安装nginx

系统版本


1.png

1、安装nginx服务器需要的相关依赖

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

gcc gcc-c++编译环境

gzip 模块需要 zlib 库

rewrite 模块需要 pcre 库

ssl 功能需要openssl库

2、下载nginx源码包。

wget http://nginx.org/download/nginx-1.19.2.tar.gz

3、解压nginx源码包

tar xvf nginx-1.19.2.tar.gz

4、进入安装目录

cd nginx-1.19.2

5、使用./configure,生成配置文件makefile,并配置nginx的安装路径

./configure --prefix=/usr/local/nginx

2.png

6、编译安装nginx

make && make install

7、因为编译需要配置才能使用systemctl启动,所以我们先通过安装路径启动一下我们的nginx

/usr/local/nginx/sbin/nginx 启动服务

/usr/local/nginx/sbin/nginx -s top 停止服务

/usr/local/nginx/sbin/nginx -s reload 重启服务

8、通过netstat -tnlp 或者 ps aux|grep nginx查看服务是否启动。


3.png

9、测试(注意如果你用的是VMWare虚拟机的话,需要关闭防火墙,才能在我们的物理机访问nginx)

4.png

10、给nginx配置通过systemctl启动,打开vim /usr/lib/systemd/system/nginx.service文件,输入以下内容:

[Unit]

Description=nginx - high performance web server

Documentation=http://nginx.org/en/docs/

After=network.target remote-fs.target nss-lookup.target

[Service]

Type=forking

ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

ExecReload=/bin/kill -s HUP $MAINPID

ExecStop=/bin/kill -s QUIT $MAINPID

PrivateTmp=true

[Install]

WantedBy=multi-user.target

这个时候会出现如下图所示的报错

5.png

解决方法如下:

[root@localhost ~]# mkdir /etc/systemd/system/nginx.service.d

[root@localhost ~]# printf "[Service]\nExecStartPost=/bin/sleep 0.1\n" > /etc/systemd/system/nginx.service.d/override.conf

[root@localhost ~]# systemctl deamon-reload 加载新的unit 配置文件

11、如下图所示就是配置成功了,可以设置一下开机自启动systemctl enable nginx.service


6.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容