Linux下安装nginx教程(Centos7)

1. 环境及准备

环境:Centos7

准备:确保系统中安装了gcc、pcre-devel、zlib-devel、openssl-devel。
Linux下检查是否安装过某软件包:http://www.cnblogs.com/xxoome/p/5866553.html

# 安装命令:
yum -y install gcc pcre-devel zlib-devel openssl openssl-devel

2. 下载安装

# 指定目录下创建nginx目录(我的是/usr/myname/nginx)
mkdir nginx
# 下载tar包
wget http://nginx.org/download/nginx-1.13.7.tar.gz
# 解压
tar -zxvf nginx-1.13.7.tar.gz
# 进入nginx目录执行,不加后面的命令会默认给你装到/usr/local/nginx目录下
./configure --prefix=/usr/myname/nginx
# 如果此步报错:c compiler cc is not found,说明是缺少 gcc-c++ 的包,没有则忽略
# 解决办法:
yum -y install gcc-c++
# 执行make命令
make
# 执行make install命令
make install

3. 测试是否安装成功

# cd到刚才配置的安装目录
./sbin/nginx -t
  • 正常情况的信息输出:
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
  • 如果报如下的错,说明nginx安装目录下没有logs文件夹
nginx: [alert] could not open error log file: open() "/usr/local/nginx/logs/error.log" failed (2: No such file or directory)
2020/04/04 10:08:56 [emerg] 6996#0: open() "/usr/local/nginx/logs/access.log" failed (2: No such file or directory)
  • 解决办法:
mkdir logs
chmod 700 logs
  • 启动
cd /usr/local/nginx/sbin
./nginx //启动nginx
  • 在浏览器中输入服务器的ip地址,如:192.168.11.12,(默认是80端口),会显示welcome to ngnix。
  • 如果你的80端口被apache占了,你可以在./conf/nginx.conf中修改端口。
server
  {
    listen 80 ;#修改此处为任意端口,访问时访问你的ip:该端口即可
    server_name localhost;#域名
    index index.html index.htm index.php;
    root /usr/local/webserver/nginx/html;#站点目录
      location ~ .*\.(php|php5)?$
    {
      #fastcgi_pass unix:/tmp/php-cgi.sock;
      fastcgi_pass 127.0.0.1:9000;
      fastcgi_index index.php;
      include fastcgi.conf;
    }
    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|ico)$
    {
      expires 30d;
  # access_log off;
    }
    location ~ .*\.(js|css)?$
    {
      expires 15d;
   # access_log off;
    }
    access_log off;
  }

你可能感兴趣的:(Linux,nginx)