CENTOS7下安装配置nginx(wget方法)

Centos7 下安装nginx 1.13.2,配置WEB服务器运行,并设置开机自行启动服务。

安装nginx

参考教程:CentOS 7 下安装 Nginx

# 安装编译环境
yum install gcc-c++
yum install -y pcre pcre-devel
yum install -y zlib zlib-devel
yum install -y openssl openssl-devel
# 安装nginx
wget -c https://nginx.org/download/nginx-1.13.2.tar.gz
tar -zxvf nginx-1.13.2.tar.gz
cd nginx-1.13.2
./configure
# 开启静态缓存
# ./configure --with-http_gzip_static_module
make && make install
# 参考自定义配置(不推荐)
./configure \
--prefix=/usr/local/nginx \
--conf-path=/usr/local/nginx/conf/nginx.conf \
--pid-path=/usr/local/nginx/conf/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/temp/nginx/client \
--http-proxy-temp-path=/var/temp/nginx/proxy \
--http-fastcgi-temp-path=/var/temp/nginx/fastcgi \
--http-uwsgi-temp-path=/var/temp/nginx/uwsgi \
--http-scgi-temp-path=/var/temp/nginx/scgi

启动nginx

cd /usr/local/nginx/sbin/
./nginx
./nginx -s stop
./nginx -s reload
./nginx -s quit

查看nginx进程

ps aux | grep nginx

配置nginx

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

nginx.conf配置内容(http{}内):

gzip on;
gzip_http_version 1.1;
gzip_comp_level 2;
upstream node {
server 127.0.0.1:xxxx;
}
server {
listen 80;
server_name localhost;
location / {
root /var/public_root/www;
index index.html index.htm;
proxy_pass_header Server;
proxy_set_header Host $http_host;
proxy_set_header x-forwarded-for $remote_addr;
proxy_set_header X-Scheme $scheme;
proxy_pass http://node;
}
location /assets/ {
root /var/public_root/www;
}
}

设置NGINX开机启动

参考教程:Nginx+Center OS 7.2 开机启动设置(转载)

vi /lib/systemd/system/nginx.service

nginx.service内容:

[Unit]
Description=nginx
After=network.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 quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target

赋予所有用户权限

chmod a+x /lib/systemd/system/nginx.service

设置开机启动:systemctl enable nginx.service

其他命令:
systemctl start nginx.service
systemctl disable nginx.service
systemctl status nginx.service
systemctl restart nginx.service
systemctl list-units --type=service

图片资源403

# 将nginx.conf文件第一行修改为
user root

开放端口

centos7 打开mysql 3306端口并 设置外部访问

# 开放80端口
sudo firewall-cmd --zone=public --add-port=80/tcp --permanent
# 查看开放的端口号
sudo firewall-cmd --zone=public --list-ports
# 重启防火墙
sudo systemctl restart firewalld.service

关闭防火墙

外网无法访问时,记得关闭防火墙↓或放开外部访问端口↑

#查看防火墙状态
systemctl status firewalld.service
#关闭防火墙
systemctl stop firewalld.service
#禁止防火墙开机启动
systemctl disable firewalld.service
NOOLDEY

本文作者:NOOLDEY

做一个诗情画意的码农,皮皮猪,我们走!

原文链接: http://zhuweisheng.com.cn/tech/centos-nginx/

本站文章如无特殊声明均为原创,创作不易,转载请注明来源,谢谢!