馬哥教育網絡班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