虚拟化容器,大数据,DBA,中间件,监控。

Shell nginx配置wordpress站点

20 12月
作者:admin|分类:脚本编程

第一步:配置文件删除包含#号的注释行


默认安装完nginx的时候,会给你一个配置文件,该文件里面有很多的#注释行,使用sed d就可以将包含#的行删除 ,注意这里的#号并不是行的开头,所以不能这样删除sed '/^#/d' /usr/src/nginx-1.16.1/conf/nginx.conf 

#可以看到包含#的行都被删除了
[root@www ~]# sed '/#/d' /usr/src/nginx-1.16.1/conf/nginx.conf 

worker_processes  1;




events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;



    sendfile        on;

    keepalive_timeout  65;


    server {
        listen       80;
        server_name  localhost;



        location / {
            root   html;
            index  index.html index.htm;
        }


        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }



    }










}

 第二步:删除以空格开头和以空格结尾的空行


在上面的基础上面将空行删除,这样使得配置更加简洁明了。 -e是:该选项会将其后跟的脚本命令添加到已有的命令中。

sed '/^$/d' file 就是删除文件中的空行,^开头$结尾
[root@www ~]# sed -e '/#/d' -e '/^$/d' /usr/src/nginx-1.16.1/conf/nginx.conf 
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}


#将server指令块以下全部删除,然后在nginx配置文件中引用虚拟主机配置文件
sed -i '/server/,$d'  nginx.conf
echo -e "include /usr/local/nginx/conf/vhosts/*.conf; \n}" >>nginx.conf

 全部实现如下(记得要把wordpress软件包放在/usr/src下面,脚本里面会去该目录下面解压)

[root@www ~]# cat wordpress_install.sh 
#nginx wordpress define variables
nginx_src="/usr/src"
nginx_dir="/usr/local/nginx"
nginx_url="http://nginx.org/download"
nginx_ver="1.16.0"
nginx_soft="nginx-${nginx_ver}.tar.gz"
nginx_args="--prefix=${nginx_dir} --user=nginx --group=nginx --with-http_stub_status_module"
 
word_press_name="blog.wordpress.com"
word_press_conf="blog.wordpress.com.conf"
word_press_dir="wordpress"
word_press_soft="wordpress-4.9.4-zh_CN.tar.gz"
vhost_dir="vhost"

yum install gcc pcre pcre-devel zlib zlib-devel wget make openssl-devel net-tools -y
sed -i 's/enforcing/disabled/g' /etc/selinux/config
systemctl stop firewalld.service
 
cd $nginx_src
wget -c $nginx_url/$nginx_soft
tar xf $nginx_soft
cd nginx-${nginx_ver}
 
useradd -s /sbin/nologin nginx
./configure $nginx_args
 
make&&make install
\cp -R /usr/src/nginx-${nginx_ver}/contrib/vim/* /usr/share/vim/vimfiles/

$nginx_dir/sbin/nginx
ps -ef | grep nginx | grep -v grep


#上面部分是自动安装nginx,下面是开始配置虚拟主机和配置Nginx的配置文件
 
echo -e "\033[32m-----------------config Nginx vhost now------------------------------\033[0m"
sleep 3s
 
#备份你的nginx配置文件,然后修改为wordpress配置
cd $nginx_dir/conf
\cp nginx.conf nginx.conf.bak
\cp $nginx_src/nginx-${nginx_ver}/conf/nginx.conf $nginx_dir/conf
 
#上面已经演示了,不再说明
sed -i -e '/#/d' -e '/^$/d' -e '/server/,$d'  nginx.conf
echo -e "include /usr/local/nginx/conf/vhosts/*.conf; \n}" >>nginx.conf
 
#解压wordpress人家包至html目录下面,并且修改该目录递归更改权限为nginx所有者和所属组
tar -xf  $nginx_src/wordpress-4.9.4-zh_CN.tar.gz -C $nginx_dir/html
chown -R  nginx:nginx $nginx_dir/html/$word_press_dir

#创建虚拟主机目录,虚拟主机配置文件存放在html/vhost目录下面 
mkdir -p $vhost_dir
cd $vhost_dir

#配置虚拟主机配置文件的内容
cat >$word_press_conf<<EOF
server {
        listen       80;
        server_name  $word_press_name;
        location / {
            root   html/$word_press_dir;
            index index.php index.html index.htm;
        }
        
	error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
       
	location ~ \.php$ {
	root html/$word_press_dir;
	fastcgi_pass 127.0.0.1:9000;
	fastcgi_index index.php;
	fastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name; #\是转义符,让$显示为$,而不是变量值
     include fastcgi_params;
  }
}
EOF
 
 
$nginx_dir/sbin/nginx -t
$nginx_dir/sbin/nginx -s reload


#整个脚本结束之后将配置信息全部打印在屏幕上面
echo -e "\033[32m................................nginx config info......................................\033[0m"
cat  $nginx_dir/conf/nginx.conf

echo -e "\033[32m................................virtual host config......................................\033[0m"
cat  $nginx_dir/conf/$vhost_dir/$word_press_conf

echo -e "\033[32m.................................Wordpress dir privilege............................\033[0m"
ls -ld $nginx_dir/html/$word_press_dir

 

浏览364 评论0
返回
目录
返回
首页
shell 在shell脚本中正确的编写Mysql Shell 配置Nginx虚拟主机脚本