虚拟机--CentOS 7安装Nginx

1.虚拟机网络模式改为桥接,ip地址改为静态
可以在网上查阅

2. 关闭防火墙
停止防火墙服务

systemctl stop firewalld.service

关闭开机自启

systemctl disable firewalld.service

3. 安装nginx依赖

这里一定要安装好,不然下面会报错,错误为nginx c compiler cc is not found.

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

4. 安装wget

yum -y install wget

5. 安装nginx
下载源码包

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

解压

tar -zxvf nginx-1.17.2.tar.gz

进入解压出来的文件夹

cd nginx-1.17.2

预编译

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

安装

make && make install

6. 修改环境变量
编辑环境变量配置文件

vi /etc/profile

在空白处输入:

PATH=$PATH:/usr/local/nginx/sbin
export PATH

保存文件后刷新

source /etc/profile

7. 修改端口
编辑nginx配置文件

vi /usr/local/nginx/conf/nginx.conf

将listen 80改成如图所示的listen 8080
8. 使用systemctl控制nginx
创建systemctl服务文件

vi /usr/lib/systemd/system/nginx.service

将以下内容输入:

[Unit]
Description=nginx - high performance web server
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop

[Install]
WantedBy=multi-user.target

保存,使文件生效:

systemctl daemon-reload

然后就可以使用如下命令进行控制:

启动nginx服务:
systemctl start nginx.service
设置开机自启动:
systemctl enable nginx.service
停止开机自启动:
systemctl disable nginx.service
查看服务当前状态:
systemctl status nginx.service
重新启动服务:
systemctl restart nginx.service

9.启动nginx,并设为开机自启

systemctl start nginx.service
systemctl enable nginx.service

10. 访问nginx

在浏览器中输入“当前CentOS的ip:8080”,如192.168.1.1:8080

如果出现下面图片,就说明安装成功了。

å¨è¿éæå¥å¾çæè¿°

发布了67 篇原创文章 · 获赞 23 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_39071599/article/details/99061570
今日推荐