您好,登錄后才能下訂單哦!
今天嘗試用ansible自動化編譯安裝了lnmp環境,是以把自己的見解和大家分享,不足之處還望大家指正。
lnmp的構成
lnmp = linux + nginx + mysql + php/python/perl
下面給大家介紹下我的安裝步驟
系統:linux7
首先下載lnmp環境所需模塊,創建ansible角色
# mkdir -pv /tmp/roles/{mysql,php,nginx}/{files,vars,templates,tasks,handlers,meta}
# cd /tmp/roles/mysql/
第一步:安裝mysql,我安裝的版本是5.6.x
編寫腳本
vim /tmp/roles/mysql/files/mysql.sh
#!/bin/bash mount -o loop /dev/sr0 /yum yum clean all yum list #導入lnmp環境所需依賴包 yum install cmake* gcc gcc-c++ ncurses-devel perl libxml* libcurl* libjpeg* libpng* freetype* sleep 10 #解壓mysql壓縮包 tar xzf /root/Downloads/bag/11-mysql-5.6.26.tar.gz -C /usr/src cd /usr/src/mysql-5.6.26 cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_EXTRA_CHARSETS=gbk,gb2312 -DENABLED_LOCAL_INFILE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 && make && make install #安裝后,編寫mysql配置文件 #1,手動編寫my.cnf配置文件 mkdir /usr/local/mysql/etc cat > /usr/local/mysql/etc/my.cnf << EOF [mysqld] port=3307 datadir=/data56 pid-file=/var/run/mysqld/mysql56.pid socket=/tmp/mysql.sock log-error=/var/log/mysqld/mysql56-err.log user=mysql [client] port=3307 socket=/tmp/mysql.sock EOF #2:創建mysql相關目錄,并修改權限 useradd -u 27 mysql mkdir /data56 /var/run/mysqld /var/log/mysqld chown mysql.mysql /data56 /var/run/mysqld /var/log/mysqld /usr/local/mysql/ -R #3:初始化數據庫 /usr/local/mysql/scripts/mysql_install_db --defaults-file=/usr/local/mysql/etc/my.cnf --basedir=/usr/local/mysql --user=mysql #--拷貝服務啟動腳本 cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql56 cp /usr/local/mysql/bin/mysql /usr/bin/
保存退出
創建yml文件
touch tasks/main.yml handlers/main.yml vars/main.yml
編輯任務
vim tasks/main.yml
- name: copy tar copy: src=/run/media/zsy/KINGSTON/運維資料/20-Nginx(web服務器、反向代理負>載均衡、LNMP)/Nginx-軟件包/bag dest=/root/Downloads/ tags: - tar - name: copy mysql.sh to dbserver copy: src=mysql.sh dest=/tmp/ - name: bash mysql.sh shell: /bin/bash /tmp/mysql.sh tags: - sh - name: install configure template: src=mysql dest=/etc/init.d/mysql56 tags: - tem notify: - restarted mysql
保存退出
創建site.yml
#vim /tmp/site.yml
- hosts: dbserver remote_user: root roles: - mysql
至此完成數據庫的安裝
第二步:安裝PHP,我這安裝的是5.6.x版本
# cd /tmp/roles/php/
創建yml
# touch tasks/main.yml handlers/main.yml vars/main.yml
編寫腳本
vim files/php.sh
#!/bin/bash #安裝個大模塊 #1,安裝libiconv-1.13.tar.gz --語言編碼轉換 install_libiconv(){ tar xzf /root/Downloads/bag/4-libiconv-1.13.tar.gz -C /usr/src cd /usr/src/libiconv-1.13/ ./configure && make && make install echo /usr/local/lib > /etc/ld.so.conf.d/lnmp.conf /sbin/ldconfig } #2.安裝mhash-0.9.9.9.tar.bz2 install_mhash-0.9.9.9(){ tar xf /root/Downloads/bag/7-mhash-0.9.9.9.tar.bz2 -C /usr/src cd /usr/src/mhash-0.9.9.9/ ./configure && make && make install /sbin/ldconfig } #3.安裝libmcrypt-2.5.8.tar.bz2 install_libmcrypt-2.5.8(){ tar xf /root/Downloads/bag/5-libmcrypt-2.5.8.tar.bz2 -C /usr/src cd /usr/src/libmcrypt-2.5.8/ ./configure && make && make install /sbin/ldconfig } #4.安裝mcrypt-2.6.6.tar.gz install_mcrypt-2.6.6(){ tar xzf /root/Downloads/bag/6-mcrypt-2.6.6.tar.gz -C /usr/src cd /usr/src/mcrypt-2.6.6 ./configure && make && make install /sbin/ldconfig } #5.安裝pcre-7.9.tar.gz--perl兼容正則表達式,--或者使用rpm自帶的也可以(yum install pcre pcre-devel -y) install_pcre-7.9(){ tar xzf /root/Downloads/bag/8-pcre-7.9.tar.gz -C /usr/src/ cd /usr/src/pcre-7.9/ ./configure && make && make install /sbin/ldconfig } #6.編譯安裝php install_php(){ tar xf /root/Downloads/bag/12-php-5.6.12.tar.bz2 -C /usr/src/ cd /usr/src/php-5.6.12/ ln -s /usr/local/mysql/lib/libmysqlclient.so /usr/lib/ ln -s /usr/local/mysql/lib/libmysqlclient.so.18 /usr/lib/libmysqlclient.so.18 echo '/usr/local/lib' >> /etc/ld.so.conf /sbin/ldconfig ./configure --prefix=/usr/local/php/ --with-config-file-path=/usr/local/php/etc --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-iconv-dir=/usr/local --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --enable-mbregex --enable-fpm --enable-mbstring --enable-ftp --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --with-gettext --enable-mysqlnd --with-pcre-dir=/usr/local/ --with-mcrypt=/usr/local/ --with-pdo-mysql=/usr/local/mysql --enable-opcache=no && make ZEND_EXTRA_LIBS='-liconv' && make install } #7.安裝緩存模塊memcache install_memcache(){ tar xf /root/Downloads/bag/10-memcache-2.2.7.tgz -C /usr/src/ cd /usr/src/memcache-2.2.7/ /usr/local/php/bin/phpize ./configure --with-php-config=/usr/local/php/bin/php-config && make && make install echo /usr/src/memcache-2.2.7/modules/ >> /etc/ld.so.conf.d/lnmp.conf /sbin/ldconfig } #8.安裝繪圖引擎模塊p_w_picpathmagick,與GD類似 install_p_w_picpathmagick(){ tar xzf /root/Downloads/bag/2-ImageMagick-6.7.8-9.tar.gz -C /usr/src/ cd /usr/src/ImageMagick-6.7.8-9/ ./configure make;make install /sbin/ldconfig } #9.安裝imagick(連接php與p_w_picpathmagick的通道) install_imagick(){ tar xf /root/Downloads/bag/3-imagick-3.1.2.tgz -C /usr/src/ cd /usr/src/imagick-3.1.2/ /usr/local/php/bin/phpize ./configure --with-php-config=/usr/local/php/bin/php-config make;make install echo /usr/src/imagick-3.1.2/modules >> /etc/ld.so.conf.d/lnmp.conf /sbin/ldconfig } install_libiconv && install_mhash-0.9.9.9 && install_libmcrypt-2.5.8 && install_mcrypt-2.6.6 && install_pcre-7.9 && install_php && install_memcache && install_p_w_picpathmagick && install_imagick && cp /usr/src/php-5.6.12/php.ini-production /usr/local/php/etc/php.ini
注:如PHP版本小于5.6還需安裝php的緩存加速器opcache
編輯任務
#vim tasks/main.yml
- name: copy shell copy: src=php.sh dest=/tmp/ - name: bash shell shell: /bin/bash /tmp/php.sh - name: install configure template: src=php.ini dest=/usr/local/php/etc/php.ini tags: - ini
把角色加到site.yml中
#vim /tmp/site.yml
- hosts: dbserver remote_user: root roles: - mysql - php
至此可完成PHP的安裝
第三步:編譯安裝nginx,版本1.13.x
#cd /tmp/roles/nginx
編寫腳本
#vim files/nginx.sh
install_nginx(){ #新建nginx用戶 useradd nginx tar xzf /root/Downloads/bag/1-nginx-1.8.1.tar.gz -C /usr/src cd /usr/src/nginx-1.8.1/ ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_gzip_static_module --with-http_stub_status_module --with-http_ssl_module && make && make install } install_nginx #配置php-fpm配置文件 (配置fastcgi) mv /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf mkdir /var/run/fastcgi chown nginx.nginx /var/run/fastcgi/ mkdir /lnmp/web -p ulimit -SHn 65535 >> /etc/rc.local
創建yml文件
# touch tasks/main.yml handlers/main.yml vars/main.yml
創建任務
# vim tasks/main.yml
- name: copy shell copy: src=nginx.sh dest=/tmp/ - name: bash shell shell: /bin/bash /tmp/nginx.sh - name: change php-fpm.conf template: src=php-fpm.conf dest=/usr/local/php/etc/php-fpm.conf - name: change nginx.conf template: src=nginx.conf dest=/usr/local/nginx/conf/nginx.conf - name: change files open command: ulimit -SHn 65535
把任務加到site.yml中
# vim /tmp/site.yml
- hosts: dbserver remote_user: root roles: - mysql - php - nginx
最后執行ansible-playbook /tmp/site.yml
執行過程比較久,需耐心等待。至此ansible自動化搭建lnmp環境圓滿結束。
以上屬個人搭建步驟,其中有些小步驟沒有寫上,如模版的配置,畢竟大家所需模版的配置不一。
如有更好的建議,請留下你珍貴的一言,謝謝。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。