centos7.3 yum LNMP 環境配置

服務器配置太低,裝了gitlab后掛了,重新配置一下。

一、掛載數據盤

df -h

只有系統盤了,掛載上原來的數據盤

fdisk -l

看到數據盤了/dev/vdb1
掛載上這個數據盤,

mkdir /data
mount /dev/vdb1 /data

然后寫入分區:

echo '/dev/vdb1 /data ext4 defaults 0 0' >> /etc/fstab

不寫入分區表,重啟后又要掛載的。
怎么知道分區類型是ext4,用這個命令:

df -hT

好的成功了!

二、安裝nginx

首先更新系統軟件

# yum update

安裝nginx

1.安裝nginx源

# yum localinstall http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm

2.安裝nginx

# yum install nginx

3.啟動nginx

# service nginx start

Redirecting to /bin/systemctl start  nginx.service

4.訪問http://你的ip/

如果成功安裝會出來nginx默認的歡迎界面


三、安裝MySQL5.7.*

1.安裝mysql源

# yum localinstall http://dev.mysql.com/get/mysql57-community-release-el7-7.noarch.rpm

2.安裝mysql

# yum install mysql-community-server

3.安裝mysql的開發包,以后會有用

# yum install mysql-community-devel

4.啟動mysql

# service mysqld start

Redirecting to /bin/systemctl start  mysqld.service

5.查看mysql啟動狀態

# service mysqld status

出現pid
證明啟動成功

6.獲取mysql默認生成的密碼

# grep 'temporary password' /var/log/mysqld.log

選中的就是密碼。

7.換成自己的密碼

# mysql -uroot -p

Enter password:輸入上頁的密碼,進入mysql

8. 更換密碼

1

mysql>  ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPasdfs4!';

這個密碼一定要足夠復雜,不然會不讓你改,提示密碼不合法;

9.退出mysql并試用下新密碼

mysql> quit;mysql -uroot -p

確認密碼正確

四、編譯安裝php7.0.0

1.下載php7源碼包

# cd /root & wget -O php7.tar.gz http://cn2.php.net/get/php-7.0.1.tar.gz/from/this/mirror

2.解壓源碼包

# tar -xvf php7.tar.gz

3.進入目錄

# cd php-7.0.1

4.安裝php依賴包

# yum install libxml2 libxml2-devel openssl openssl-devel bzip2 bzip2-devel libcurl libcurl-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel gmp gmp-devel libmcrypt libmcrypt-devel readline readline-devel libxslt libxslt-devel

5.編譯配置,這里如果上一步的某些依賴包沒有安裝好,就會遇到很多configure error,我們一一解決,安裝上相關軟件開發包就可以

# ./configure \
--prefix=/usr/local/php \
--with-config-file-path=/etc \
--enable-fpm \
--with-fpm-user=nginx \
--with-fpm-group=nginx \
--enable-inline-optimization \
--disable-debug \
--disable-rpath \
--enable-shared \
--enable-soap \
--with-libxml-dir \
--with-xmlrpc \
--with-openssl \
--with-mcrypt \
--with-mhash \
--with-pcre-regex \
--with-sqlite3 \
--with-zlib \
--enable-bcmath \
--with-iconv \
--with-bz2 \
--enable-calendar \
--with-curl \
--with-cdb \
--enable-dom \
--enable-exif \
--enable-fileinfo \
--enable-filter \
--with-pcre-dir \
--enable-ftp \
--with-gd \
--with-openssl-dir \
--with-jpeg-dir \
--with-png-dir \
--with-zlib-dir \
--with-freetype-dir \
--enable-gd-native-ttf \
--enable-gd-jis-conv \
--with-gettext \
--with-gmp \
--with-mhash \
--enable-json \
--enable-mbstring \
--enable-mbregex \
--enable-mbregex-backtrack \
--with-libmbfl \--with-onig \
--enable-pdo \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-zlib-dir \
--with-pdo-sqlite \
--with-readline \
--enable-session \
--enable-shmop \
--enable-simplexml \
--enable-sockets \
--enable-sysvmsg \
--enable-sysvsem \
--enable-sysvshm \
--enable-wddx \
--with-libxml-dir \
--with-xsl \
--enable-zip \
--enable-mysqlnd-compression-support \
--with-pear \
--enable-opcache

configure error:
1.configure: error: xml2-config not found. Please check your libxml2 installation.
解決:

# yum install libxml2 libxml2-devel

2.configure: error: Cannot find OpenSSL's <evp.h>
解決:

# yum install openssl openssl-devel

3.configure: error: Please reinstall the BZip2 distribution
解決:

# yum install bzip2 bzip2-devel

4.configure: error: Please reinstall the libcurl distribution - easy.h should be in <curl-dir>/include/curl/
解決:

# yum install libcurl libcurl-devel

5.If configure fails try --with-webp-dir=<DIR> configure: error: jpeglib.h not found.

解決:

# yum install libjpeg libjpeg-devel

6.If configure fails try --with-webp-dir=<DIR>
checking for jpeg_read_header in -ljpeg... yes
configure: error: png.h not found.
解決:

# yum install libpng libpng-devel

7.If configure fails try --with-webp-dir=<DIR>
checking for jpeg_read_header in -ljpeg... yes
checking for png_write_image in -lpng... yes
If configure fails try --with-xpm-dir=<DIR>
configure: error: freetype-config not found.
解決:

# yum install freetype freetype-devel

8.configure: error: Unable to locate gmp.h
解決:

# yum install gmp gmp-devel

9.configure: error: mcrypt.h not found. Please reinstall libmcrypt.
解決:

  # yum install libmcrypt libmcrypt-devel

10.configure: error: Please reinstall readline - I cannot find readline.h
解決:

# yum install readline readline-devel

11.configure: error: xslt-config not found. Please reinstall the libxslt >= 1.1.0 distribution
解決:

# yum install libxslt libxslt-devel

6.編譯與安裝

# make && make install

這里要make好久,要耐心一下

7.添加 PHP 命令到環境變量

# vim /etc/profile

在末尾加入

PATH=$PATH:/usr/local/php/bin
export PATH

要使改動立即生效執行

# source /etc/profile

查看環境變量

# echo $PATH

查看php版本

# php -v

8.配置php-fpm

# cp php.ini-production /etc/php.ini

# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf# cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf

# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm

# chmod +x /etc/init.d/php-fpm

9.啟動php-fpm

# /etc/init.d/php-fpm start

五、配置nginx虛擬機,綁定域名

1.添加配置文件

# vim /etc/nginx/conf.d/www.chaisz.xyz.conf

這里可以把www.chaisz.xyz改成自己的域名

把下面的內容復制到www.chaisz.xyz.conf

server{
    listen 80;
    server_name  www.chaisz.xyz;
    root /data/www/; # 該項要修改為你準備存放相關網頁的路徑
    location / {
        index  index.php index.html index.htm;
         #如果請求既不是一個文件,也不是一個目錄,則執行一下重寫規則
         if (!-e $request_filename)
         {
            #地址作為將參數rewrite到index.php上。
            rewrite ^/(.*)$ /index.php/$1;
            #若是子目錄則使用下面這句,將subdir改成目錄名稱即可。
            #rewrite ^/subdir/(.*)$ /subdir/index.php/$1;
         }
    }
    #proxy the php scripts to php-fpm
    location ~ \.php {
            include fastcgi_params;
            ##pathinfo支持start
            #定義變量 $path_info ,用于存放pathinfo信息
            set $path_info "";
            #定義變量 $real_script_name,用于存放真實地址
            set $real_script_name $fastcgi_script_name;
           #如果地址與引號內的正則表達式匹配
            if ($fastcgi_script_name ~ "^(.+?\.php)(/.+)$") {
                    #將文件地址賦值給變量 $real_script_name
                    set $real_script_name $1;
                    #將文件地址后的參數賦值給變量 $path_info
                    set $path_info $2;
            }
            #配置fastcgi的一些參數
            fastcgi_param SCRIPT_FILENAME $document_root$real_script_name;
            fastcgi_param SCRIPT_NAME $real_script_name;
            fastcgi_param PATH_INFO $path_info;
            ###pathinfo支持end
        fastcgi_intercept_errors on;
        fastcgi_pass   127.0.0.1:9000;
    }

    location ^~ /data/runtime {
    return 404;
    }
    location ^~ /application {
    return 404;
    }

    location ^~ /simplewind {
    return 404;
    }
}
#************如果不需要支持https,以下內容不需要***********
#************支持https需要ssl證書 
server {
    listen 443;
    server_name localhost;
    ssl on;
    root /data/www/;
    index index.html index.htm;
    ssl_certificate   /etc/nginx/ssl/214052098260625.pem;
    ssl_certificate_key  /etc/nginx/ssl/214052098260625.key;
    ssl_session_timeout 5m;
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;
    location / {
        index  index.php index.html index.htm;
         #如果請求既不是一個文件,也不是一個目錄,則執行一下重寫規則
         if (!-e $request_filename)
         {
            #地址作為將參數rewrite到index.php上。
            rewrite ^/(.*)$ /index.php/$1;
            #若是子目錄則使用下面這句,將subdir改成目錄名稱即可。
            #rewrite ^/subdir/(.*)$ /subdir/index.php/$1;
         }
    }
    #proxy the php scripts to php-fpm
    location ~ \.php {
            include fastcgi_params;
            ##pathinfo支持start
            #定義變量 $path_info ,用于存放pathinfo信息
            set $path_info "";
            #定義變量 $real_script_name,用于存放真實地址
            set $real_script_name $fastcgi_script_name;
            #如果地址與引號內的正則表達式匹配
            if ($fastcgi_script_name ~ "^(.+?\.php)(/.+)$") {
                    #將文件地址賦值給變量 $real_script_name
                    set $real_script_name $1;
                    #將文件地址后的參數賦值給變量 $path_info
                    set $path_info $2;
            }
            #配置fastcgi的一些參數
            fastcgi_param SCRIPT_FILENAME $document_root$real_script_name;
            fastcgi_param SCRIPT_NAME $real_script_name;
            fastcgi_param PATH_INFO $path_info;
            ###pathinfo支持end
        fastcgi_intercept_errors on;
        fastcgi_pass   127.0.0.1:9000;
    }

    location ^~ /data/runtime {
    return 404;
    }

    location ^~ /application {
    return 404;
    }

    location ^~ /simplewind {
    return 404;
    }
    #******************如果不需要支持https,以上內容不需要
}

2.重啟nginx

# service nginx reload

3.測試腳本

# vim /data/www/index.php

把下面的代碼復制到這個文件 里

<?php
phpinfo();

4.查看訪問http://www.chaisz.xyzhttps://www.chaisz.xyz

ok!收工!

MySQL密碼忘記解決方法

  1. vim /etc/my.cnf 加入skip-grant-tables
屏幕快照 2017-09-22 下午7.33.34.png
  1. service mysqld restart

  2. mysql -u root -p 直接進入mysql

  3. update mysql.user set authentication_string=password('root') where user='root' ;

  4. 編輯my.cnf文件刪掉skip-grant-tables 這一行,然后重啟MySQL,/etc/init.d/mysqld restart,否則MySQL仍能免密碼登錄

  5. mysql -u root -p
    然后輸入密碼即可登錄MySQL數據庫

mysql創建遠程用戶并授權

先用ssh登錄遠程服務器,用root連上數據庫看看情況;

mysql> select Host,User,authentication_string from mysql.user;
+----------------+------------------+-------------------------------------------+
| Host           | User             | Password                                  |
+----------------+------------------+-------------------------------------------+
| localhost      | root             | *836E233974EBE6EA32F95F890A91363F8427F78B |
| iz94926clkiz   | root             | *836E233974EBE6EA32F95F890A91363F8427F78B |
| 127.0.0.1      | root             | *836E233974EBE6EA32F95F890A91363F8427F78B |
| ::1            | root             | *836E233974EBE6EA32F95F890A91363F8427F78B |
| localhost      | debian-sys-maint | *1460ED3535ABDBB887F9E5F57F40A2354610CDF3 |
+----------------+------------------+-------------------------------------------+
rows in set (0.00 sec)

一共有5個mysql賬號,Host列可以看出來,這些賬號都只支持服務器本機連接,現在我們來創建一個遠程用戶;

create user test identified by '123456';
+----------------+------------------+-------------------------------------------+
| Host           | User             | Password                                  |
+----------------+------------------+-------------------------------------------+
| localhost      | root             | *836E283974EBE6EA32F95F890A91363F8427F78B |
| iz949s6clkiz   | root             | *836E283974EBE6EA32F95F890A91363F8427F78B |
| 127.0.0.1      | root             | *836E283974EBE6EA32F95F890A91363F8427F78B |
| ::1            | root             | *836E283974EBE6EA32F95F890A91363F8427F78B |
| localhost      | debian-sys-maint | *1460ED35E5ABDBB887F9E5F57F40A2354610CDF3 |
| %              | test             | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
+----------------+------------------+-------------------------------------------+
rows in set (0.00 sec)

創建完成了,在程序里面連接發現還是沒有權限,剛才我們只是創建了用戶,還沒有對這個用戶分配權限;

grant all privileges on *.* to 'test'@'%'identified by '123456' with grant option;

all代表接受所有操作,比如 select,insert,delete....; . 代表所有庫下面的所有表;% 代表這個用戶允許從任何地方登錄;為了安全,這個%可以替換為你允許的ip地址;

然后刷新mysql用戶權限相關表;

flush privileges ;

到這里就可以收工了。如果還是連不上去:access deny ;
可以看一下mysql配置文件 vim /etc/my.cnf 看是否有下面這行,

bind-address           = 127.0.0.1

這里mysql默認綁定了本地ip,不接受其他來源;注釋掉,重啟mysql 一切OK;

還有兩個常用操作;

修改指定用戶密碼

update mysql.user set authentication_string=password('新密碼') where User="test" and Host="localhost";

刪除用戶

delete from user where User='test' and Host='localhost';

原文鏈接

參考鏈接:http://www.cnblogs.com/codeAB/p/6391022.html

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容