LNMP環境安裝方法

Cetetos7.6 lnmp環境安裝步驟

lnmp 是由Linux+nginx+mysql+php這個幾個組件組成的,用來搭建php網站應用而生。

網上有很多一鍵安裝腳本,不過 如果你是運維工程師 建議你還是手工去搭建這個環境,那樣你對整個過程就比較清楚排除問題起來也更加有思路,好那麼來開始我們的安裝之旅吧!

(1)安裝Nginx

nginx的安裝可以通過yum進行安裝也可以通過源碼包編譯安裝,我們以淘寶的tengine作爲nginx的版本進行安裝,本文采用最新的穩定版 Tengine-2.1.2 爲例

$mkdir /opt/tools
$cd /opt/tools
$wget http://tengine.taobao.org/download/tengine-2.1.2.tar.gz
$tar -xzf tengine-2.1.2.tar.gz
$cd tengine-2.1.2
$./configure --prefix=/usr/local/nginx  --with-file-aio --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_ssl_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie' --with-http_stub_status_module --with-http_sysguard_module --with-http_concat_module    
$make && make install
$ln -s /usr/local/nginx/sbin/nginx /usr/bin/  
$nginx  			 #啓動

(2)安裝php和fpm

我們以安裝php5.6版本爲例

$yum -y install epel-release
$rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
$yum install php56w php56w-mysql php56w-gd libjpeg* php56w-ldap php56w-odbc php56w-pear php56w-xml php56w-xmlrpc php56w-mbstring php56w-bcmath
$ yum instal php56w-fpm   # 安裝fpm

我們把其他用戶改下,vim /etc/php-fpm.d/www.conf

user = apache
group = apache

改成
user = www
group = www

然後啓動:

systemctl start php-fpm
netstat -ntlp |grep 9000   # 查看是否已經啓動

php -m|grep mysql      # 查看啓用了哪些擴展
配置nginx能解析php

vim /usr/local/nginx/nginx.conf的配置文件如下(生產環境肯定需要優化ng,這裏只做最簡單的安裝使用)

worker_processes  1;
events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;



## 日誌格式
log_format access '[$time_local] $remote_addr $status $request_time $upstream_response_time '   
                  '$body_bytes_sent "$request" "$http_referer" $upstream_addr '          
                  '$http_x_real_ip "$http_x_forwarded_for"  $http_user_agent" $request_filename ';

###
    server {
        listen       80;
        server_name  172.20.128.33;
        root   /home/data/webroot/zabbix/;
        
      location / {
        index   index.php index.html;
        location ~ \.php$ {
                fastcgi_pass   127.0.0.1:9000;
                fastcgi_index  index.php;
                fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include        fastcgi_params;
            }
          }

        access_log /home/data/logs/zabbix/zabbix.access.log access;
    }
}

對網站目錄進行授權

chown -R www:www /home/data/logs/
chown -R www:www /home/data/webroot/

寫一個測試文件打開看看是否正常,cat /home/data/webroot/zabbix/index.php,

<?php
phpinfo();

訪問:

curl http://172.20.128.33/info.php |grep mysql    # 能有內容說明訪問頁面正常
(3)安裝mysql

mysql 的安裝方式有很多。mysql的安裝方式有以下幾種:

  • 源碼包編譯安裝(安裝時間較長)
  • 二進制包安裝(推薦此種方式)
  • yum進行安裝

如果生產環境我一般使用二進制包進行安裝,這裏爲了簡單起見,直接用yum 安裝

開始安裝
wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm
yum repolist all | grep mysql  # 查看可用的 mysql 安裝文件
yum install mysql-server   # 安裝
rpm -qa | grep mysql   # 檢查是否安裝成功

啓動mysql服務

systemctl start mysqld.service 			#啓動 mysql
systemctl status mysqld.service         # 查看狀態
systemctl restart mysqld.service 		#重啓 mysql
systemctl stop mysqld.service 			#停止 mysql
systemctl enable mysqld.service 		#設置 mysql 開機啓動

登錄mysql,首次登錄是不需要密碼的,清除掉無效的用戶

select user,host from mysql.user   # 查看有哪些用戶
>use mysql;
>Delete FROM user Where User='root' and Host='::1';
>Delete FROM user Where User='' and Host='localhost';
>Delete FROM user Where User='' and Host='hostname';
>Delete FROM user Where User='' and Host='vm203';
>Delete FROM user Where User='root' and Host='vm203';  

給root用戶設置一個密碼,如果生產環境清設置複雜的密碼

>use mysql
>SET PASSWORD FOR 'root'@'localhost' = PASSWORD('123456'); 
> flush privileges;

創建一個應用庫,並設置密碼,讓業務能讀寫這個庫

>create database zabbix character set utf8 collate utf8_bin;
> grant all privileges on zabbix.* to zabbix@'localhost' identified by '123456';
>flush privileges;

退出來,再次登錄試一試是否正常

mysql -uroot -p

到此,LNMP環境就安裝完成

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章