009 Linux下编译安装Nginx服务器 并 修改Nginx服务器访问根目录,009nginx
一、 nginx安装环境
yum install -y gcc-c pcre pcre-devel zlib zlib-devel openssl openssl-devel
该命令等效于下面4句:
1、 gcc 安装
安装nginx需要先将官网下载的源码进行编译,编译依赖gcc环境,如果没有gcc环境,需要安装gcc:
yum install gcc-c
2、 PCRE pcre-devel 安装
PCRE(PerlCompatible Regular Expressions)是一个Perl库,包括 perl
兼容的正则表达式库。nginx的http模块使用pcre来解析正则表达式,所以需要在linux上安装pcre库。
yum install -y pcre pcre-devel
3、 zlib 安装
zlib库提供了很多种压缩和解压缩的方式,nginx使用zlib对http包的内容进行gzip,所以需要在linux上安装zlib库。
yum install -y zlib zlib-devel
4、OpenSSL 安装
OpenSSL是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及SSL协议,并提供丰富的应用程序供测试或其它目的使用。nginx不仅支持http协议,还支持https(即在ssl协议上传输http),所以需要在linux安装openssl库。
yum install -y openssl openssl-devel
二、下载nginx
1.直接下载.tar.gz安装包,地址:
2.使用wget命令下载1.10.1版本(推荐)。
wget -c
三、安装
1、解压
tar -zxvf nginx-1.10.1.tar.gz
cd nginx-1.10.1
2、配置
其实在 nginx-1.10.1
版本中你就不需要去配置相关东西,默认就可以了。当然,如果你要自己配置目录也是可以的。
1.使用默认配置
澳门新浦京娱乐场网站,./configure
输出:
nginx path prefix: "/usr/local/nginx"
nginx binary file: "/usr/local/nginx/sbin/nginx"
nginx modules path: "/usr/local/nginx/modules"
nginx configuration prefix: "/usr/local/nginx/conf"
nginx configuration file: "/usr/local/nginx/conf/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"
3、编译安装
make
make install
澳门新浦京娱乐场网站:修改Nginx服务器访问根目录,在虚拟机中的Linux系统搭建ftp服务器。查找安装路径:"/usr/local/nginx
四、开放80端口、开放http服务,重启防火墙
firewall-cmd --zone=public --add-port=80/tcp --permanent
firewall-cmd --permanent --zone=public --add-service=http
firewall-cmd --reload
firewall-cmd --list-all
#查看开放服务、端口中是否有http服务和80端口。
五、更改nginx访问根目录
安装完nginx服务器后发现nginx的根目录在
安装目录的/html/下(/usr/local/nginx/html/),但是对于部署文件来说,在该目录下是不太习惯的,我就尝试着更改nginx访问的根目录
1、更改nginx配置文件
vi /usr/local/nginx/conf/nginx.conf
更改如下:
server {
listen 80;
server_name localhost;
location / {
root /home/ftpuser/wwwRoot; #新的根目录
index index.html index.htm index.jpg;
#添加一张图片,测试用。
}
2、nginx根目录权限设置:
chmod -R 755 /home/ftpuser/wwwRoot/
3、重启nginx务器
service nginx restart
4、测试
拷贝一直张jpg格式图片到:/home/ftpuser/wwwRoot/目录下,改名为:index.jpg
。
chmod 744 /home/ftpuser/wwwRoot/index.jpg #设置所有人可读。
访问
设置完成后此时访问 即为/home/www/XXX
六、使用nginx
1 启动nginx
/usr/local/nginx/sbin/nginx
注意:执行 /usr/local/nginx/sbin/nginx,这里可以-c指定加载的nginx配置文件,如下:
/usr/local/nginx/sbin/nginx -c /xxxxx
如果不指定-c,nginx在启动时默认加载/usr/local/nginx/conf/nginx.conf文件。
2 停止nginx
/usr/local/nginx/sbin/nginx -s quit
此方式停止步骤是待nginx进程处理任务完毕进行停止。
3 重启nginx:先停止再启动
/usr/local/nginx/sbin/nginx -s quit
/usr/local/nginx/sbin/nginx
centos6.5安装nginx-1.8.0,nginx-1.8.0
1.Nginx安装环境:
gcc: 安装nginx需要先将官网下载的源码进行编译,编译依赖gcc环境,如果没有gcc环境,需要安装gcc:yum install gcc-c
pcre:PCRE(Perl Compatible Regular Expressions)是一个Perl库,包括 perl 兼容的正则表达式库。nginx的http模块使用pcre来解析正则表达式,所以需要在linux上安装pcre库。yum install -y pcre pcre-devel (注:pcre-devel是使用pcre开发的一个二次开发库。nginx也需要此库。)
zlib:zlib库提供了很多种压缩和解压缩的方式,nginx使用zlib对http包的内容进行gzip,所以需要在linux上安装zlib库。yum install -y zlib zlib-devel
openssl:OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及SSL协议,并提供丰富的应用程序供测试或其它目的使用。nginx不仅支持http协议,还支持https(即在ssl协议上传输http),所以需要在linux安装openssl库。yum install -y openssl openssl-devel
2.清除系统中的httpd痕迹:
yum remove httpd
rm -rvf /etc/httpd
3.创建www用户和用户组:
groupadd www
useradd -s /sbin/nologin -g www www
5.解压、配置、编译、安装nginx:
tar zxvf nginx-1.8.0.tar.gz -C /usr/src/
cd /usr/src/nginx-1.8.0/
./configure --prefix=/usr/local/nginx
--user=www
--group=www
--with-mail
--with-mail_ssl_module
--with-http_ssl_module
--with-http_flv_module
--with-http_dav_module
--with-http_sub_module
--with-http_spdy_module
--with-http_realip_module
--with-http_addition_module
--with-http_gzip_static_module
--with-http_stub_status_module
--with-pcre
make && make install
6.编辑nginx.conf配置文件:
vim /usr/local/nginx/conf/nginx.conf
user www www;
worker_processes auto;
pid /home/www/pid/nginx.pid;
worker_rlimit_nofile 51200;
events
{
use epoll;
worker_connections 51200;
multi_accept on;
}
http
{
include mime.types;
default_type application/octet-stream;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 50m;
sendfile on;
tcp_nopush on;
keepalive_timeout 60;
tcp_nodelay on;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 256k;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css
application/xml;
gzip_vary on;
gzip_proxied expired no-cache no-store private auth;
gzip_disable "MSIE [1-6].";
server_tokens off;
log_format access '$remote_addr - $remote_user
[$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $http_x_forwarded_for';
server
{
listen 80 default;
server_name 127.0.0.1;
access_log /home/www/log/access.log access;
error_log /home/www/log/error.log error;
index index.html index.htm index.php;
root /home/www/html/;
error_page 404 /404.html;
location ~ [^/].php(/|$)
{
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
location /nginx_status
{
stub_status on;
access_log off;
}
location ~ .*.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
location ~ .*.(js|css)?$
{
expires 12h;
}
}
include vhost/*.conf;
}
7.创建目录并修改权限:
mkdir -p /home/www/log
mkdir -p /home/www/pid
mkdir -p /home/www/html
chown -R www:www /home/www/log
chown -R www:www /home/www/pid
chown -R www:www /home/www/html
chown -R www:www /usr/local/nginx
8.测试启动Nginx
./nginx
9.创建测试页面和错误页面,并赋予权限:
touch /home/www/html/index.html
cat>/home/www/html/index.html<<EOF
This is the test Page for Nginx !!!
EOF
touch /home/www/html/404.html
cat>/home/www/html/404.html<<EOF
Error Page for Nginx !!!
EOF
chown -R www:www /home/www/html
10.测试:
11.防火墙开启80端口:
vim /etc/sysconfig/iptablse.conf
添加 -A INPUT -p tcp --dport 80 -j ACCEPT
12.重启防火墙
service iptables restart
1.Nginx安装环境: gcc: 安装 nginx 需要先将官网下载的源码进行编译,编译依赖 gcc 环境,如果没有 gcc 环境,...
Linux操作系统:CentOS 5.6
本系列主要讲五个方面,这是第二步。
在/usr目录下执行命令下载最新Nginx源代码:
给虚拟机中的Linux系统设置静态ip,使ip固定,方便访问。
nginx安装及配置
- wget
3.ftp安装及配置
解压后可以看到nginx-1.2.0目录:
4.selinux配置或直接暴力关闭
- tar zxvf nginx-1.2.0.tar.gz
5.遇到过的问题及解决方案汇总
安装PCRE库
- yum -y install pcre-devel
偷个懒。nginx安装挺简单的,按流程一步一步走,没有大问题。
安装openssl库
唯一一个需要注意的问题是,安装好了之后,如果在虚拟机中已经可以通过浏览器访问nginx,但是无法从主机的浏览器访问,参考上上篇博客的开放端口的办法,配置linux系统的防火墙开发80端口。
- yum -y install openssl openssl-devel
具体开放方法:
生成make file文件:
先停止防火墙
- ./configure --prefix=/usr/nginx
service iptables stop
结果要仔细看一下,有些很重要的信息:
打开配置文件
- creating objs/Makefile
- 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/nginx"
- nginx binary file: "/usr/nginx/sbin/nginx"
- nginx configuration prefix: "/usr/nginx/conf"
- nginx configuration file: "/usr/nginx/conf/nginx.conf"
- nginx pid file: "/usr/nginx/logs/nginx.pid"
- nginx error log file: "/usr/nginx/logs/error.log"
- nginx http access log file: "/usr/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"
vim /etc/sysconfig/iptables
比如pid 文件,error log文件,access log文件等等。
开放80端口
现在编译和安装:
-A INPUT -m state –state NEW -m tcp -p tcp –dport 80 -j ACCEPT
- make
- make install
重启防火墙
进入/usr/nginx/sbin目录下,可以执行nginx程序启动。
service iptables restart
如果要修改端口,修改/usr/nginx/conf/nginx.conf文件,比如:
1 nginx安装环境
nginx是C语言开发,建议在linux上运行,本教程使用Centos6.5作为安装环境。
n gcc
安装nginx需要先将官网下载的源码进行编译,编译依赖gcc环境,如果没有gcc环境,需要安装gcc:yum install gcc-c
n PCRE
PCRE(Perl Compatible Regular Expressions)是一个Perl库,包括 perl 兼容的正则表达式库。nginx的http模块使用pcre来解析正则表达式,所以需要在linux上安装pcre库。
yum install -y pcre pcre-devel
注:pcre-devel是使用pcre开发的一个二次开发库。nginx也需要此库。
n zlib
zlib库提供了很多种压缩和解压缩的方式,nginx使用zlib对http包的内容进行gzip,所以需要在linux上安装zlib库。
yum install -y zlib zlib-devel
n openssl
OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及SSL协议,并提供丰富的应用程序供测试或其它目的使用。
nginx不仅支持http协议,还支持https(即在ssl协议上传输http),所以需要在linux安装openssl库。
yum install -y openssl openssl-devel
- server {
- listen 8081;
2 编译安装
将nginx-1.8.0.tar.gz拷贝至linux服务器。
解压:
tar -zxvf nginx-1.8.0.tar.gz
cd nginx-1.8.0
1、 configure
./configure --help查询详细参数(参考本教程附录部分:nginx编译参数)
参数设置如下:
./configure
--prefix=/usr/local/nginx
--pid-path=/var/run/nginx/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
注意:上边将临时文件目录指定为/var/temp/nginx,需要在/var下创建temp及nginx目录
2、 编译安装
make
make install
安装成功查看安装目录 :
3 启动nginx
cd /usr/local/nginx/sbin/
./nginx
查询nginx进程:
15098是nginx主进程的进程id,15099是nginx工作进程的进程id
注意:执行./nginx启动nginx,这里可以-c指定加载的nginx配置文件,如下:
./nginx -c /usr/local/nginx/conf/nginx.conf
如果不指定-c,nginx在启动时默认加载conf/nginx.conf文件,此文件的地址也可以在编译安装nginx时指定./configure的参数(--conf-path= 指向配置文件(nginx.conf))
4 停止nginx
方式1,快速停止:
cd /usr/local/nginx/sbin
./nginx -s stop
此方式相当于先查出nginx进程id再使用kill命令强制杀掉进程。
方式2,完整停止(建议使用):
cd /usr/local/nginx/sbin
./nginx -s quit
此方式停止步骤是待nginx进程处理任务完毕进行停止。
5 重启nginx
方式1,先停止再启动(建议使用):
对nginx进行重启相当于先停止nginx再启动nginx,即先执行停止命令再执行启动命令。
如下:
./nginx -s quit
./nginx
方式2,重新加载配置文件:
当nginx的配置文件nginx.conf修改后,要想让配置生效需要重启nginx,使用-s reload不用先停止nginx再启动nginx即可将配置信息在nginx中生效,如下:
./nginx -s reload
6 测试
nginx安装成功,启动nginx,即可访问虚拟机上的nginx:
到这说明nginx上安装成功。
7 开机自启动nginx
7.1编写shell脚本
这里使用的是编写shell脚本的方式来处理
vi /etc/init.d/nginx (输入下面的代码)
#!/bin/bash # nginx Startup script for the Nginx HTTP Server # it is v.0.0.2 version. # chkconfig: - 85 15 # description: Nginx is a high-performance web and proxy server. # It has a lot of features, but it's not for everyone. # processname: nginx # pidfile: /var/run/nginx.pid # config: /usr/local/nginx/conf/nginx.conf nginxd=/usr/local/nginx/sbin/nginx nginx_config=/usr/local/nginx/conf/nginx.conf nginx_pid=/var/run/nginx.pid RETVAL=0 prog="nginx" # Source function library. . /etc/rc.d/init.d/functions # Source networking configuration. . /etc/sysconfig/network # Check that networking is up. [ ${NETWORKING} = "no" ] && exit 0 [ -x $nginxd ] || exit 0 # Start nginx daemons functions. start() { if [ -e $nginx_pid ];then echo "nginx already running...." exit 1 fi echo -n $"Starting $prog: " daemon $nginxd -c ${nginx_config} RETVAL=$? echo [ $RETVAL = 0 ] && touch /var/lock/subsys/nginx return $RETVAL } # Stop nginx daemons functions. stop() { echo -n $"Stopping $prog: " killproc $nginxd RETVAL=$? echo [ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /var/run/nginx.pid } # reload nginx service functions. reload() { echo -n $"Reloading $prog: " #kill -HUP `cat ${nginx_pid}` killproc $nginxd -HUP RETVAL=$? echo } # See how we were called. case "$1" in start) start ;; stop) stop ;; reload) reload ;; restart) stop start ;; status) status $prog RETVAL=$? ;; *) echo $"Usage: $prog {start|stop|restart|reload|status|help}" exit 1 esac exit $RETVAL
|
:wq 保存并退出
7.2设置文件的访问权限
chmod a x /etc/init.d/nginx (a x ==> all user can execute 所有用户可执行)
这样在控制台就很容易的操作nginx了:查看Nginx当前状态、启动Nginx、停止Nginx、重启Nginx…
如果修改了nginx的配置文件nginx.conf,也可以使用上面的命令重新加载新的配置文件并运行,可以将此命令加入到rc.local文件中,这样开机的时候nginx就默认启动了
7.3加入到rc.local文件中
vi /etc/rc.local
加入一行 /etc/init.d/nginx start 保存并退出,下次重启会生效。
本文由澳门新浦京娱乐场网站发布于服务器,转载请注明出处:澳门新浦京娱乐场网站:修改Nginx服务器访问根目