进入Nginx官网站 http://nginx.org/,进入后选择最新的版本,进入页面后在 Stable version
中选择 nginx-*
右键复制链接,然后用 wget
下载
[root@iZuf6b4wamau0tcvk7hwglZ ~]# wget http://nginx.org/download/nginx-1.12.0.tar.gz
- 1
如果提示wget命令未找到,则执行
[root@iZuf6b4wamau0tcvk7hwglZ ~]# sudo yum install wget
- 1
下载完用 ll 命令查看刚刚下载获得的 nginx-*.tar.gz
文件,用下面命令解压它
[root@iZuf6b4wamau0tcvk7hwglZ ~]# tar -zxvf nginx-1.12.0.tar.gz
- 1
-z: 表示使用gzip的属性。
-x: 解开一个压缩文件的参数指令。
-v: 表示压缩的过程中显示文件!这个常用,但不建议用在背景执行过程!
-f: 使用档名,请留意,在 f 之后要立即接档名喔!不要再加参数!
安装Nginx前,需要安装以下三个依赖包:
- gzip 模块需要
zlib
库 - rewrite 模块需要
pcre
库 ssl 功能需要
openssl
库
https://www.openssl.org/source/
[root@iZuf6b4wamau0tcvk7hwglZ ~]# wget https://www.openssl.org/source/openssl-fips-2.0.14.tar.gz
- 1
[root@iZuf6b4wamau0tcvk7hwglZ ~]# wget http://www.zlib.net/zlib-1.2.11.tar.gz
- 1
(http://www.pcre.org/)[http://www.pcre.org/
[root@iZuf6b4wamau0tcvk7hwglZ ~]# wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.39.tar.gz
- 1
下载完成后安装,依赖包安装顺序依次为: openssl
、zlib
、pcre
, 然后安装 Nginx
包
1.安装openssl
[root@iZuf6b4wamau0tcvk7hwglZ ~]# tar -zxvf openssl-fips-2.0.14.tar.gz
[root@iZuf6b4wamau0tcvk7hwglZ ~]# cd openssl-fips-2.0.14/
[root@iZuf6b4wamau0tcvk7hwglZ openssl-fips-2.0.14]# ./config
[root@iZuf6b4wamau0tcvk7hwglZ openssl-fips-2.0.14]# make
[root@iZuf6b4wamau0tcvk7hwglZ openssl-fips-2.0.14]# make install
- 1
- 2
- 3
- 4
- 5
2.安装zlib
[root@iZuf6b4wamau0tcvk7hwglZ ~]# tar -zxvf zlib-1.2.11.tar.gz
[root@iZuf6b4wamau0tcvk7hwglZ ~]# cd zlib-1.2.11/
[root@iZuf6b4wamau0tcvk7hwglZ zlib-1.2.11]# ./configure
[root@iZuf6b4wamau0tcvk7hwglZ zlib-1.2.11]# make
[root@iZuf6b4wamau0tcvk7hwglZ zlib-1.2.11]# make install
- 1
- 2
- 3
- 4
- 5
安装gcc g++
yum install gcc-c++
- 1
3.安装pcre
[root@iZuf6b4wamau0tcvk7hwglZ ~]# tar -zxvf pcre-8.39.tar.gz
[root@iZuf6b4wamau0tcvk7hwglZ ~]# cd pcre-8.39/
[root@iZuf6b4wamau0tcvk7hwglZ pcre-8.39]# ./configure
[root@iZuf6b4wamau0tcvk7hwglZ pcre-8.39]# make
[root@iZuf6b4wamau0tcvk7hwglZ pcre-8.39]# make install
- 1
- 2
- 3
- 4
- 5
4.安装nginx
[root@iZuf6b4wamau0tcvk7hwglZ ~]# tar -zxvf nginx-1.12.0.tar.gz
[root@iZuf6b4wamau0tcvk7hwglZ ~]# cd nginx-1.12.0/
[root@iZ2844brz0xZ nginx-1.12.0]# ./configure --with-pcre=../pcre-8.39 --with-zlib=../zlib-1.2.11 --with-openssl=../openssl-fips-2.0.14
[root@iZuf6b4wamau0tcvk7hwglZ nginx-1.12.0]# make
[root@iZuf6b4wamau0tcvk7hwglZ nginx-1.12.0]# make install
- 1
- 2
- 3
- 4
- 5
检测是否安装成功
[root@iZuf6b4wamau0tcvk7hwglZ nginx-1.12.0]# cd /usr/local/nginx/sbin/
[root@iZuf6b4wamau0tcvk7hwglZ sbin]# ./nginx -t
- 1
- 2
若出现下方信息表示安装成功
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
- 1
- 2
最后一步:启动Nginx
[root@iZuf6b4wamau0tcvk7hwglZ sbin]# ./nginx
- 1
查看端口
[root@iZuf6b4wamau0tcvk7hwglZ sbin]# netstat -ntlp
- 1
出现以下结果
至此,nginx的安装完成‘
配置Nginx
进入Nginx目录,编辑 nginx.conf
配置文件
[root@iZuf6b4wamau0tcvk7hwglZ nginx]# cd /usr/local/nginx/
[root@iZuf6b4wamau0tcvk7hwglZ nginx]# cd conf/
[root@iZuf6b4wamau0tcvk7hwglZ conf]# vim nginx.conf
- 1
- 2
- 3
看到 server
模块下有:
location / {
root html;
index index.html index.htm;
}
- 1
- 2
- 3
- 4
这段表示所有的文件都会进入html这个目录下,我们现在编写与php相关的语句
在下面我们可以找到配置文件已经帮我们写好了我们只需要把前面的注释符号 #
去掉就好了
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;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
输入:wq
保存退出
进入nginx的html
目录,新建一个文件测试一下
[root@iZuf6b4wamau0tcvk7hwglZ conf]# cd ..
[root@iZuf6b4wamau0tcvk7hwglZ nginx]# cd html/
[root@iZuf6b4wamau0tcvk7hwglZ html]# vim test.php
- 1
- 2
- 3
输入
<?php
phpinfo();
?>
- 1
- 2
- 3
忘了说,修改完配置文件记得要重新启动nginx哦
[root@iZuf6b4wamau0tcvk7hwglZ ~]# cd /usr/local/nginx/sbin/
[root@iZuf6b4wamau0tcvk7hwglZ sbin]# ./nginx -s reload
- 1
- 2
在花费超多时间和精力再查阅各种博客的配置LNMP后,终于配好了。其实以前配过一次,只不过那时候配的是简陋版,并且自己也对其中过程不是很熟悉。之前配好后没有进行记录,很多大佬都有将他们的配置过程写在博客中,于是这次重新配置,并记录完毕。
说说其中的坑吧,在最后的创建test.php后,在浏览器上显示file not found,本来想询问阿里客服,了解是fpm没配好还是哪个路径没设好,当时毫无头绪。结果阿里客服推荐我去那边收费的区域合作机构帮忙配置,又只好硬着头皮再去翻翻繁多的资料,没想到最后居然配好了。(此刻刚配好的我在写这篇文章时就已经笑出了声)果然是/usr/local/nginx/conf 下的 nginx.conf 里的路径有问题。
这是之前的
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;
}
改为
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html/$fastcgi_script_name;
include fastcgi_params;
}
忘了说,修改完配置文件记得要重新启动nginx哦
[root@iZuf6b4wamau0tcvk7hwglZ~]# cd /usr/local/nginx/sbin/ [root@iZuf6b4wamau0tcvk7hwglZ sbin]# ./nginx -s reload
- 1
- 2
打开网址,运行成功~
接下来创建开机启动命令脚本文件
vim /etc/init.d/nginx
- 1
在这个nginx文件中插入一下启动脚本代码,启动脚本代码来源网络复制,实测有效
#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon
#
# chkconfig: - 85 15
# description: NGINX is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /etc/nginx/nginx.conf
# config: /etc/sysconfig/nginx
# pidfile: /var/run/nginx.pid
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
nginx="/usr/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/etc/nginx/nginx.conf"
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
lockfile=/var/lock/subsys/nginx
make_dirs() {
# make required directories
user=`$nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
if [ -z "`grep $user /etc/passwd`" ]; then
useradd -M -s /bin/nologin $user
fi
options=`$nginx -V 2>&1 | grep 'configure arguments:'`
for opt in $options; do
if [ `echo $opt | grep '.*-temp-path'` ]; then
value=`echo $opt | cut -d "=" -f 2`
if [ ! -d "$value" ]; then
# echo "creating" $value
mkdir -p $value && chown -R $user $value
fi
fi
done
}
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
make_dirs
echo -n $"Starting $prog: "
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}
stop() {
echo -n $"Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}
restart() {
configtest || return $?
stop
sleep 1
start
}
reload() {
configtest || return $?
echo -n $"Reloading $prog: "
killproc $nginx -HUP
RETVAL=$?
echo
}
force_reload() {
restart
}
configtest() {
$nginx -t -c $NGINX_CONF_FILE
}
rh_status() {
status $prog
}
rh_status_q() {
rh_status >/dev/null 2>&1
}
case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit 2
esac
设置所有人都有对这个启动脚本nginx文件的执行权限
[root@iZuf6b4wamau0tcvk7hwglZ init.d]# chmod a+x /etc/init.d/nginx
- 1
把nginx加入系统服务中
[root@iZuf6b4wamau0tcvk7hwglZ init.d]# chkconfig --add nginx
- 1
把php-fpm加入系统服务中
[root@iZuf6b4wamau0tcvk7hwglZ init.d]# chkconfig --add php-fpm
- 1
查看chkconfig 启动项
[root@iZuf6b4wamau0tcvk7hwglZ init.d]# chkconfig --list
- 1
至此服务器配置完成。
参考资料
http://www.cnblogs.com/hzh19870110/p/6100661.html 作者:grhlove123
<script>
(function(){
function setArticleH(btnReadmore,posi){
var winH = $(window).height();
var articleBox = $("div.article_content");
var artH = articleBox.height();
if(artH > winH*posi){
articleBox.css({
'height':winH*posi+'px',
'overflow':'hidden'
})
btnReadmore.click(function(){
if(typeof window.localStorage === "object" && typeof window.csdn.anonymousUserLimit === "object"){
if(!window.csdn.anonymousUserLimit.judgment()){
window.csdn.anonymousUserLimit.Jumplogin();
return false;
}else if(!currentUserName){
window.csdn.anonymousUserLimit.updata();
}
}
articleBox.removeAttr("style");
$(this).parent().remove();
})
}else{
btnReadmore.parent().remove();
}
}
var btnReadmore = $("#btn-readmore");
if(btnReadmore.length>0){
if(currentUserName){
setArticleH(btnReadmore,3);
}else{
setArticleH(btnReadmore,1.2);
}
}
})()
</script>
</article>