写在前面的话
每次在Linux搭建web环境都要一顿google求参考文档,这次索性写个简单的文章记录一下。其实Linux下编译安装主要就是这几个步骤。
-
安装编译工具、依赖包及下载源码包
-
解压编译
-
安装
-
启动
准备工作
安装编译工具、依赖包
$ yum -y install gcc gcc-c++ autoconf automake
$ yum -y install zlib zlib-devel openssl openssl-devel pcre-devel
以上安装的是一些主要的依赖包,具体可根据自己情况或者报错信息提示安装或修改
新建匿名用户和用户组
新建的用户组和用户主要是在编译配置的时候指定nginx运行的用户和用户组。这样指定后以后配置使用也方便。
$ sudo groupadd -r nginx
$ sudo useradd -s /sbin/nologin -g nginx -r nginx
Nginx编译安装
下载源码包
我一般从官网下载
wget http://nginx.org/download/nginx-1.10.0.tar.gz
解压并编译
解压
tar -zxvf nginx-1.10.0.tar.gz
配置
./configure \
--prefix=/usr/local/nginx \
--conf-path=/etc/nginx/nginx.conf \
--user=nginx \
--group=nginx \
配置没毛病的话可以看到这些信息
Configuration summary
+ using system PCRE library
+ OpenSSL library is not used
+ md5: using system crypto library
+ sha1: using system crypto library
+ using system zlib library
nginx path prefix: "/usr/local/nginx"
nginx binary file: "/usr/local/nginx/sbin/nginx"
nginx modules path: "/usr/local/nginx/modules"
nginx configuration prefix: "/etc/nginx"
nginx configuration file: "/etc/nginx/nginx.conf"
nginx pid file: "/usr/local/nginx/logs/nginx.pid"
nginx error log file: "/usr/local/nginx/logs/error.log"
nginx http access log file: "/usr/local/nginx/logs/access.log"
nginx http client request body temporary files: "client_body_temp"
nginx http proxy temporary files: "proxy_temp"
nginx http fastcgi temporary files: "fastcgi_temp"
nginx http uwsgi temporary files: "uwsgi_temp"
nginx http scgi temporary files: "scgi_temp"
以上编译参数只是配置了主要的东西,全部配置参数说明你可以通过这个命令查看
./configure --help
编译并安装
$ make && make install
启动等命令
须进入到/usr/local/nginx/sbin
目录下
启动:
$ nginx
停止:
$ nginx -s stop
重启:
$ nginx -s reload
以上各个步骤报错的话根据报错提示缺啥装啥,不犯几次错误都不好意思说自己配过环境。PS:有问题可留言讨论哦
注意地方:
在web服务器中,不管是Apache还是Nginx,这些服务器默认占用的端口都是80端口。但是,有时候80端口被占用,或者一些其他原因,我们需要这些服务工作在非80端口上,那么如何修改Nginx默认端口,使其占用8089端口(或者其它非80端口),方法步骤如下:
1.首先修改nginx根目录下的配置文件nginx.conf,修改如下:
1
2
3
4
5
6
7
8
9
|
server {
listen 8089;
server_name 192.168.171.210;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
|
其中:listen后面的端口号改为你自己需要的端口号,server_name改为你自己的外网ip。server_name默认为localhost即127.0.0.1。
修改完配置文件后重启nginx:/usr/local/nginx/nginx -s reload.我的nginx是安装在/usr/local/目录下,根据你自己nginx的安装目录重启你的服务器。
2.然后修改防火墙配置文件
#配置防火墙,开启8089端口(注意:这里要开启你自己配置的端口,和上面listen指定端口一样)
1
2
|
vi /etc/sysconfig/iptables 添加如下语句:
-A INPUT -m state --state NEW -m tcp -p tcp --dport 8089 -j ACCEPT #允许8089端口通过防火墙
|
防火墙配置修改完成后:
1
|
/etc/init.d/iptables restart #最后重启防火墙使配置生效
|
最后访问你的站点:http://ip:8089
其中ip是你设置的server_name也就是你外网的ip地址,访问站点时要记得加上你配置的端口号。