个人环境是 centos7.0 php
n gcchtml
安装nginx须要先将官网下载的源码进行编译,编译依赖gcc环境,若是没有gcc环境,须要安装gcc:yum install gcc-c++linux
n PCREnginx
PCRE(Perl Compatible Regular Expressions)是一个Perl库,包括 perl 兼容的正则表达式库。nginx的http模块使用pcre来解析正则表达式,因此须要在linux上安装pcre库。c++
yum install -y pcre pcre-develweb
注:pcre-devel是使用pcre开发的一个二次开发库。nginx也须要此库。正则表达式
n zlib算法
zlib库提供了不少种压缩和解压缩的方式,nginx使用zlib对http包的内容进行gzip,因此须要在linux上安装zlib库。shell
yum install -y zlib zlib-develvim
n openssl
OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、经常使用的密钥和证书封装管理功能及SSL协议,并提供丰富的应用程序供测试或其它目的使用。
nginx不只支持http协议,还支持https(即在ssl协议上传输http),因此须要在linux安装openssl库。
yum install -y openssl openssl-devel
将nginx-1.8.0.tar.gz拷贝至linux服务器。
解压:
tar -zxvf nginx-1.8.0.tar.gz
cd nginx-1.8.0
一、 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目录
二、 编译安装
make
make install
安装成功查看安装目录 :
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))
方式1,快速中止:
cd /usr/local/nginx/sbin
./nginx -s stop
此方式至关于先查出nginx进程id再使用kill命令强制杀掉进程。
方式2,完整中止(建议使用):
cd /usr/local/nginx/sbin
./nginx -s quit
此方式中止步骤是待nginx进程处理任务完毕进行中止。
方式1,先中止再启动(建议使用):
对nginx进行重启至关于先中止nginx再启动nginx,即先执行中止命令再执行启动命令。
以下:
./nginx -s quit
./nginx
方式2,从新加载配置文件:
当nginx的配置文件nginx.conf修改后,要想让配置生效须要重启nginx,使用-s reload不用先中止nginx再启动nginx便可将配置信息在nginx中生效,以下:
./nginx -s reload
nginx安装成功,启动nginx,便可访问虚拟机上的nginx:
到这说明nginx上安装成功。
这里使用的是编写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 保存并退出
chmod a+x /etc/init.d/nginx (a+x ==> all user can execute 全部用户可执行)
这样在控制台就很容易的操做nginx了:查看Nginx当前状态、启动Nginx、中止Nginx、重启Nginx…
若是修改了nginx的配置文件nginx.conf,也能够使用上面的命令从新加载新的配置文件并运行,能够将此命令加入到rc.local文件中,这样开机的时候nginx就默认启动了
vi /etc/rc.local
加入一行 /etc/init.d/nginx start 保存并退出,下次重启会生效。
安装完后,有/etc/vsftpd/vsftpd.conf 文件,是vsftp的配置文件。
[root@bogon ~]# yum -y install vsftpd
此用户就是用来登陆ftp服务器用的。
[root@bogon ~]# useradd ftpuser
这样一个用户建完,能够用这个登陆,记得用普通登陆不要用匿名了。登陆后默认的路径为 /home/ftpuser.
[root@bogon ~]# passwd ftpuser
输入两次密码后修改密码。
由于ftp默认的端口为21,而centos默认是没有开启的,因此要修改iptables文件
[root@bogon ~]# vim /etc/sysconfig/iptables
在行上面有22 -j ACCEPT 下面另起一行输入跟那行差很少的,只是把22换成21,而后:wq保存。
还要运行下,重启iptables
[root@bogon ~]# service iptables restart
外网是能够访问上去了,但是发现无法返回目录(使用ftp的主动模式,被动模式仍是没法访问),也上传不了,由于selinux做怪了。
修改selinux:
执行如下命令查看状态:
[root@bogon ~]# getsebool -a | grep ftp
allow_ftpd_anon_write --> off
allow_ftpd_full_access --> off
allow_ftpd_use_cifs --> off
allow_ftpd_use_nfs --> off
ftp_home_dir --> off
ftpd_connect_db --> off
ftpd_use_passive_mode --> off
httpd_enable_ftp_server --> off
tftp_anon_write --> off
[root@bogon ~]#
执行上面命令,再返回的结果看到两行都是off,表明,没有开启外网的访问
[root@bogon ~]# setsebool -P allow_ftpd_full_access on
[root@bogon ~]# setsebool -P ftp_home_dir on
这样应该没问题了(若是,仍是不行,看看是否是用了ftp客户端工具用了passive模式访问了,如提示Entering Passive mode,就表明是passive模式,默认是不行的,由于ftp passive模式被iptables挡住了,下面会讲怎么开启,若是懒得开的话,就看看你客户端ftp是否有port模式的选项,或者把passive模式的选项去掉。若是客户端仍是不行,看看客户端上的主机的电脑是否开了防火墙,关吧)
修改/etc/vsftpd/vsftpd.conf文件:
重启ftp服务:
[root@bogon ~]# service vsftpd restart
默认是开启的,可是要指定一个端口范围,打开vsftpd.conf文件,在后面加上
pasv_min_port=30000
pasv_max_port=30999
表示端口范围为30000~30999,这个能够随意改。改完重启一下vsftpd
因为指定这段端口范围,iptables也要相应的开启这个范围,因此像上面那样打开iptables文件。
也是在21上下面另起一行,更那行差很少,只是把21 改成30000:30999,而后:wq保存,重启下iptables。这样就搞定了。
[root@bogon ~]# chkconfig vsftpd on
在nginx.conf 中配置 vsftpd 的路径
#user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; #access_log logs/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; server { listen 80; server_name localhost; charset utf-8; #access_log logs/host.access.log main; location ~ .*\.(gif|jpg|jpeg|png)$ { expires 24h; root /home/ftpuser/picture/;#Ö¸¶¨Í¼Æ¬´æ·Å·¾¶ access_log /home/ftpuser/picture/images.log;#ÈÕÖ¾´æ·Å·¾¶ proxy_store on; proxy_store_access user:rw group:rw all:rw; proxy_temp_path /home/ftpuser/picture/;#ͼƬ·ÃÎÊ·¾¶ proxy_redirect off; proxy_set_header Host 127.0.0.1; client_max_body_size 10m; client_body_buffer_size 1280k; proxy_connect_timeout 900; proxy_send_timeout 900; proxy_read_timeout 900; proxy_buffer_size 40k; proxy_buffers 40 320k; proxy_busy_buffers_size 640k; proxy_temp_file_write_size 640k; if ( !-e $request_filename) { proxy_pass http://127.0.0.1;#ĬÈÏ80¶Ë¿Ú } } location / { root html; index index.html index.htm; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} } # another virtual host using mix of IP-, name-, and port-based configuration # #server { # listen 8000; # listen somename:8080; # server_name somename alias another.alias; # location / { # root html; # index index.html index.htm; # } #} # HTTPS server # #server { # listen 443 ssl; # server_name localhost; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # } #} }