Centos7 ubuntu 安装nginx

1.安装pcre,中文perl兼容正则,为了使nginx支持 http rewrite 模块

yum install pcre pcre-devel -y

2.安装 gcc, openssl

yum install gcc openssl openssl-devel -y

ubuntu安装:
sudo apt-get update 
sudo apt-get install libpcre3 libpcre3-dev  -y
sudo apt-get install openssl libssl-dev -y
sudo apt-get install zlib1g-dev -y

建立nginx用户

useradd nginx -s /sbin/nologin -M

echo $? 显示上一个命令退出的状态,0正常退出,其余都非正常

下载nginx,并解压:

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

注意:必须进入到Nginx目中:

nginx中看帮助

./configure --help

注意:必须进入到Nginx目中, --prefix为安装目录:

配置:

./configure --user=nginx --group=nginx --prefix=/opt/modules/nginx/ --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module

../../../../_resources/time/2024/09/25/1414f42e5992b506b9893b47f74be6a5_MD5.png*

make && sudo make install

检查语法:

/opt/modules/nginx/sbin/nginx -t

启动

sudo  /opt/modules/nginx/sbin/nginx

查看nginx启动情况:

netstat -ltunp | grep nginx

通过端口反查:

lsof -i :80

排错三部曲:

ping 192.168.176.100 物理是否通了

telnet 192.168.176.100 80 浏览器到web服务通不通

服务器本地是否正常 :curl 192.168.176.100

安装时可以查看安装错误:

cat /application/nginx/logs/error.log

发现centos7的防火墙阻挡80端口访问:

增加80端口访问:

firewall-cmd --zone=public --add-port=80/tcp

作为图片服务器:

先安装ftp服务器

Nginx配置:

location / {

    #root  html;

    root /home/ftpuser/www;

    index  index.html index.htm;

}

创建目录

sudo mkdir -p /home/ftpuser/www/images

sudo chown -R nginx:nginx /home/ftpuser/

sudo chmod -R 777 /home/ftpuser/

启动服务:

sudo sbin/nginx -c conf/nginx.conf

将图片上传至/home /ftpuser/www/images下,

就可以通过http://ip/image/图片.jpg  访问图片

修改export,修改了这个也没用,普通用户无权启动1024以下的端口:

export NGINX_HOME=/opt/modules/nginx  

export PATH=$NGINX_HOME/sbin:$PATH  

重启:
nginx -s reload

常见错误处理

提示

nginx 编译安装报错error: the HTTP rewrite module requires the PCRE library.

安装pcre.
[root@web_server01/usr/src/nginx-1.20.1]# yum -y install pcre

提示

./configure: error: C compiler cc is not found
源码和安装目录不能在同一目录,否则会有此错误

make[1]: *** [install] Error 1

make[1]: Leaving directory `/usr/local/nginx'

make: *** [install] Error 2
posted @ 2025-04-02 08:25  三杯两盏淡酒  阅读(38)  评论(0)    收藏  举报