马哥教育网络班22期+第16周课程练习
1、源码编译安装LNMP架构环境;
安装开发包组 yum groupinstall "Development Tools" "Server Platform Development" -y yum install -y pcre-devel zlib-devel yum install openssl openssl-devel -y groupadd -r -g 911 nginx useradd -r -g nginx -u 911 -s /sbin/nologin nginx mkdir -p /var/tmp/nginx/client cd /usr/local wget http://nginx.org/download/nginx-1.8.1.tar.gz tar -xf nginx-1.8.1.tar.gz cd /usr/local/nginx-1.8.1/ ./configure \ --prefix=$dst \ --conf-path=/etc/nginx/nginx.conf \ --user=nginx \ --group=nginx \ --error-log-path=/var/log/nginx/error.log \ --http-log-path=/var/log/nginx/access.log \ --pid-path=/var/run/nginx/nginx.pid \ --lock-path=/var/lock/nginx.lock \ --with-http_ssl_module \ --with-http_gzip_static_module \ --with-debug \ --with-http_stub_status_module \ --http-client-body-temp-path=/var/tmp/nginx/client \ --http-proxy-temp-path=/var/tmp/nginx/proxy \ --http-fastcgi-temp-path=/var/tmp/nginx/fcgi \ --http-uwsgi-temp-path=/var/tmp/nginx/uwsg \ --http-scgi-temp-path=/var/tmp/nginx/scgi \ --with-pcre \ --with-debug make make install vim /etc/nginx/nginx.conf //编辑nginx配置安装文件 location / { root html; index index.php index.html index.htm; //设置根目录可识别index.php } 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; //设置使用fastcg协议的php文件根目录 include fastcgi_params; } 修改PATH环境变量,让系统可以直接使用nginx的相关命令 vim /etc/profile.d/nginx.sh export PATH=/usr/local/nginx/sbin:$PATH source /etc/profile.d/nginx.sh nginx 启动nginx yum install -y mariadb mariadb-server php-fpm php-mysql php-gd libjpeg* php-ldap php-odbc php-pear php-xml php-xmlrpc php-mbstring php-bcmath php-mhash systemctl start mariadb.service vim /etc/php-fpm.d/www.conf //编辑php-fpm配置文件 listen = 127.0.0.1:9000 //如不是监听本地地址,可修改为需要监听的地址 listen.allowed_clients = 127.0.0.1 //本地访问 user = nginx group = nginx systemctl start php-fpm //启动 php-fpm vim /usr/local/nginx/html/index.php 测试页面index.php示例如下: <?php $conn = mysql_connect('127.0.0.1','root',''); if ($conn) echo "ok"; else echo "Failure"; phpinfo(); ?>
2、编写一个脚本完成以下功能:
(1)、一键搭建LNMP源码编译环境;
(2)、可通过在脚本后面跟上一些参数来自定义安装目录等其他选项。
# install run environment packages echo "install development tools and server platform develpment" yum groupinstall "Development Tools" "Server Platform Development" -y if [ $? -ne 0 ];then echo -e "\033[31m install development packages faild \033[0m" && exit else echo -e "\033[37m install development packages finished \033[0m" fi yum install -y pcre-devel zlib-devel openssl openssl-devel if [ $? -ne 0 ];then echo -e "\033[31m install run environment packages faild\033[0m" && exit else echo -e "\033[37m install run environment packages finished\033[0m" fi sleep 3s #install nginx echo -e "\033[37m install nginx\033[0m" groupadd -r -g 911 nginx useradd -r -g nginx -u 911 -s /sbin/nologin nginx dst=$1 #dst=/usr/local/nginx #[ ! -d $dst ] && mkdir -p $dst mkdir -p /var/tmp/nginx/client cd /usr/local [ ! -f nginx-1.8.1.tar.gz ] && wget http://nginx.org/download/nginx-1.8.1.tar.gz tar -xf nginx-1.8.1.tar.gz cd /usr/local/nginx-1.8.1/ ./configure \ --prefix=$dst \ --conf-path=/etc/nginx/nginx.conf \ --user=nginx \ --group=nginx \ --error-log-path=/var/log/nginx/error.log \ --http-log-path=/var/log/nginx/access.log \ --pid-path=/var/run/nginx/nginx.pid \ --lock-path=/var/lock/nginx.lock \ --with-http_ssl_module \ --with-http_gzip_static_module \ --with-debug \ --with-http_stub_status_module \ --http-client-body-temp-path=/var/tmp/nginx/client \ --http-proxy-temp-path=/var/tmp/nginx/proxy \ --http-fastcgi-temp-path=/var/tmp/nginx/fcgi \ --http-uwsgi-temp-path=/var/tmp/nginx/uwsg \ --http-scgi-temp-path=/var/tmp/nginx/scgi \ --with-pcre \ --with-debug make make install if [ $? -ne 0 ];then echo -e "\033[31m install nginx faild \033[0m" && exit else echo -e "\033[37m install nginx finished \033[0m" fi sleep 3s #install mariadb php-fpm echo -e "\033[37m install mariadb php-fpm \033[0m" yum install -y mariadb mariadb-server php-fpm php-mysql php-gd libjpeg* php-ldap php-odbc php-pear php-xml php-xmlrpc php-mbstring php-bcmath php-mhash if [ $? -ne 0 ];then echo -e "\033[31m install mariadb php-fpm faild \033[0m" && exit else echo -e "\033[37m install mariadb php-fpm finished \033[0m" fi sleep 3s #config nginx echo -e "\033[37m config nginx \033[0m" sed -r -i 's/(^[[:space:]]+index ).*/\1index.php index.html index.htm;/g' /etc/nginx/nginx.conf sed -r -i '/pass the/,/}/s/(.*)#(.*)/\1\2/g' /etc/nginx/nginx.conf sed -r -i 's/(^[[:space:]]+)(pass the .*)/\1#\2/g' /etc/nginx/nginx.conf sed -r -i 's@(^[[:space:]]+fastcgi_param.*)/scripts(.*)@\1/usr/local/nginx/html\2@g' /etc/nginx/nginx.conf #config php-fpm echo -e "\033[37m config php-fpm \033[0m" sed -i 's/^user = apache/user = nginx/g' /etc/php-fpm.d/www.conf sed -i 's/^group = apache/group = nginx/g' /etc/php-fpm.d/www.conf #create php test page cd /usr/local/nginx/html #[ ! -f "/usr/local/nginx/html/index.php" ] && cat >>test.test<<EOF [ ! -f "/usr/local/nginx/html/index.php" ] && cat >>index.php<<EOF <?php \$conn = mysql_connect('127.0.0.1','root',''); if (\$conn) echo "ok"; else echo "Failure"; phpinfo(); ?> EOF sleep 3s #start service echo -e "\033[37m start nginx service\033[37m" /usr/local/nginx/sbin/nginx echo -e "\033[37m start mariadb serivce\033[0m" systemctl start mariadb.service echo -e "\033[37m start php-fpm serivce \033[0m" systemctl start php-fpm.service [root@test ~]# bash lnmp.sh /usr/local/nginx