CentOS 7 yum nginx MySQL PHP 簡易環境搭建

用centos自帶的yum源來安裝nginx,mysql和php,超級方便,省去編譯的麻煩,省去本身配置的麻煩,還能節省很是多的時間。php

咱們先把yum源換成國內的阿里雲鏡像源(固然不換也能夠),先備份一下原來的源鏡像文件,以避免出錯後能夠恢復:html

[root@192 ~]# mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup

下載新的CentOS-Base.repo 到/etc/yum.repos.d/,版本根據本身的系統版本選擇下載:mysql

複製代碼

CentOS 5
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-5.repo

CentOS 6
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo

CentOS 7
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

複製代碼

更改/etc/yum.repos.d/CentOS-Media.repo使其爲不生效:nginx

enabled=0

運行yum makecache生成緩存:web

yum clean all
yum makecache
yum update

安裝Nginxsql

因爲yum源中沒有咱們想要的nginx,那麼咱們就須要建立一個「/etc/yum.repos.d/nginx.repo」的文件,其實就是新增一個yum源。數據庫

[root@192 yum.repos.d]# vi /etc/yum.repos.d/nginx.repo

把以下內容複製進去:centos

[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1

而後保存退出,輸入 yum list nginx 查看,瀏覽器

複製代碼

[root@192 yum.repos.d]# yum list nginx
已加載插件:fastestmirror
nginx                                                                           | 2.9 kB  00:00:00     
nginx/7/x86_64/primary_db                                                       |  18 kB  00:00:04     
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
可安裝的軟件包
nginx.x86_64                                  1:1.10.2-1.el7.ngx                                  nginx
[root@192 yum.repos.d]# yum list |grep nginx
nginx.x86_64                               1:1.10.2-1.el7.ngx          nginx    
nginx-debug.x86_64                         1:1.8.0-1.el7.ngx           nginx    
nginx-debuginfo.x86_64                     1:1.10.2-1.el7.ngx          nginx    
nginx-module-geoip.x86_64                  1:1.10.2-1.el7.ngx          nginx    
nginx-module-geoip-debuginfo.x86_64        1:1.10.2-1.el7.ngx          nginx    
nginx-module-image-filter.x86_64           1:1.10.2-1.el7.ngx          nginx    
nginx-module-image-filter-debuginfo.x86_64 1:1.10.2-1.el7.ngx          nginx    
nginx-module-njs.x86_64                    1:1.10.2.0.0.20160414.1c50334fbea6-2.el7.ngx
                                                                       nginx    
nginx-module-njs-debuginfo.x86_64          1:1.10.2.0.0.20160414.1c50334fbea6-2.el7.ngx
                                                                       nginx    
nginx-module-perl.x86_64                   1:1.10.2-1.el7.ngx          nginx    
nginx-module-perl-debuginfo.x86_64         1:1.10.2-1.el7.ngx          nginx    
nginx-module-xslt.x86_64                   1:1.10.2-1.el7.ngx          nginx    
nginx-module-xslt-debuginfo.x86_64         1:1.10.2-1.el7.ngx          nginx    
nginx-nr-agent.noarch                      2.0.0-10.el7.ngx            nginx    
pcp-pmda-nginx.x86_64                      3.10.6-2.el7                base     
[root@192 yum.repos.d]#

複製代碼

若是執行命令是這樣的顯示效果,那麼咱們的nginx的yum源就配置成功啦!緩存

而後要安裝咱們的nginx就直接執行:

yum -y install nginx

這樣nginx的最新官網版本就安裝好了!

啓動nginx:

複製代碼

# nginx      #啓動nginx
# curl 127.0.0.1
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

複製代碼

安裝MySQL 5.7版本,官網http://dev.mysql.com/downloads/repo/yum/ 

rpm -Uvh  http://dev.mysql.com/get/mysql57-community-release-el7-9.noarch.rpm

能夠看到已經有了,而且5.7版本已經啓用,能夠直接安裝:

複製代碼

root@192 yum.repos.d]# yum repolist all | grep mysql
mysql-connectors-community/x86_64 MySQL Connectors Community         啓用:    24
mysql-connectors-community-source MySQL Connectors Community - Sourc 禁用
mysql-tools-community/x86_64      MySQL Tools Community              啓用:    38
mysql-tools-community-source      MySQL Tools Community - Source     禁用
mysql-tools-preview/x86_64        MySQL Tools Preview                禁用
mysql-tools-preview-source        MySQL Tools Preview - Source       禁用
mysql55-community/x86_64          MySQL 5.5 Community Server         禁用
mysql55-community-source          MySQL 5.5 Community Server - Sourc 禁用
mysql56-community/x86_64          MySQL 5.6 Community Server         禁用
mysql56-community-source          MySQL 5.6 Community Server - Sourc 禁用
mysql57-community/x86_64          MySQL 5.7 Community Server         啓用:   146
mysql57-community-source          MySQL 5.7 Community Server - Sourc 禁用
mysql80-community/x86_64          MySQL 8.0 Community Server         禁用
mysql80-community-source          MySQL 8.0 Community Server - Sourc 禁用
[root@192 yum.repos.d]#

複製代碼

若是沒有開啓,或者你想要選擇須要的版本進行安裝,修改 /etc/yum.repos.d/mysql-community.repo,選擇須要的版本把enable改成1便可,其它的改成0:

修改好後查看可用的安裝版本:

[root@192 yum.repos.d]# yum repolist enabled | grep mysql
mysql-connectors-community/x86_64 MySQL Connectors Community                  24
mysql-tools-community/x86_64      MySQL Tools Community                       38
mysql57-community/x86_64          MySQL 5.7 Community Server                 146

不用猶豫,開始安裝吧!

yum -y install mysql-community-server

 ……通過漫長的等待後,看到下圖所示:

開始啓動mysql:

service mysqld start
Redirecting to /bin/systemctl start  mysqld.service

看下mysql的啓動狀態:

複製代碼

[root@192 yum.repos.d]# service mysqld status
Redirecting to /bin/systemctl status  mysqld.service
● mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: active (running) since 日 2016-10-23 22:51:48 CST; 3min 14s ago
  Process: 36884 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
  Process: 36810 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
 Main PID: 36887 (mysqld)
   CGroup: /system.slice/mysqld.service
           └─36887 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid

10月 23 22:51:45 192.168.0.14 systemd[1]: Starting MySQL Server...
10月 23 22:51:48 192.168.0.14 systemd[1]: Started MySQL Server.
10月 23 22:52:24 192.168.0.14 systemd[1]: Started MySQL Server.

複製代碼

開機啓動設置:

systemctl enable mysqld
systemctl daemon-reload

mysql安裝完成以後,在/var/log/mysqld.log文件中給root生成了一個默認密碼。經過下面的方式找到root默認密碼,而後登陸mysql進行修改:

[root@192 yum.repos.d]# grep 'temporary password' /var/log/mysqld.log
2016-10-23T14:51:45.705458Z 1 [Note] A temporary password is generated for root@localhost: a&sqr7dou7N_
mysql -uroot -p

修改root密碼:

ALTER USER 'root'@'localhost' IDENTIFIED BY 'NewPassWord!';

 注意:mysql5.7默認安裝了密碼安全檢查插件,默認密碼檢查策略要求密碼必須包含:大小寫字母、數字和特殊符號,而且長度不能少於8位。不然會提示ERROR 1819 (HY000): Your password does not satisfy the current policy requirements錯誤,以下圖所示:

經過msyql環境變量能夠查看密碼策略的相關信息:

複製代碼

mysql> show variables like '%password%';
+---------------------------------------+--------+
| Variable_name                         | Value  |
+---------------------------------------+--------+
| default_password_lifetime             | 0      |
| disconnect_on_expired_password        | ON     |
| log_builtin_as_identified_by_password | OFF    |
| mysql_native_password_proxy_users     | OFF    |
| old_passwords                         | 0      |
| report_password                       |        |
| sha256_password_proxy_users           | OFF    |
| validate_password_check_user_name     | OFF    |
| validate_password_dictionary_file     |        |
| validate_password_length              | 8      |
| validate_password_mixed_case_count    | 1      |
| validate_password_number_count        | 1      |
| validate_password_policy              | MEDIUM |
| validate_password_special_char_count  | 1      |
+---------------------------------------+--------+
14 rows in set (0.00 sec)

複製代碼

validate_password_policy:密碼策略,默認爲MEDIUM策略 
validate_password_dictionary_file:密碼策略文件,策略爲STRONG才須要 
validate_password_length:密碼最少長度 
validate_password_mixed_case_count:大小寫字符長度,至少1個 
validate_password_number_count :數字至少1個 
validate_password_special_char_count:特殊字符至少1個 
上述參數是默認策略MEDIUM的密碼檢查規則。

修改密碼策略

若是想修改密碼策略,在/etc/my.cnf文件添加validate_password_policy配置:

# 選擇0(LOW),1(MEDIUM),2(STRONG)其中一種,選擇2須要提供密碼字典文件
validate_password_policy=0

配置默認編碼爲utf8

修改/etc/my.cnf配置文件,在[mysqld]下添加編碼配置,以下所示:

[mysqld]
character_set_server=utf8
init_connect='SET NAMES utf8'

從新啓動mysql服務使配置生效:

systemctl restart mysqld

添加遠程登陸用戶

默認只容許root賬戶在本地登陸,若是要在其它機器上鍊接mysql,必須修改root容許遠程鏈接,或者添加一個容許遠程鏈接的賬戶,爲了安全起見,咱們添加一個新的賬戶:

mysql> GRANT ALL PRIVILEGES ON *.* TO 'evai'@'%' IDENTIFIED BY '@evai2016' WITH GRANT OPTION;
mysql> FLUSH PRIVILEGES;

這樣遠程就能夠用帳戶名爲evai,密碼爲@evai2016來登陸數據庫了,運行 select host, user from mysql.user 查看下:

複製代碼

mysql> select host,user from mysql.user;
+-----------+-----------+
| host      | user      |
+-----------+-----------+
| %         | evai      |
| localhost | mysql.sys |
| localhost | root      |
+-----------+-----------+
3 rows in set (0.00 sec)

複製代碼

安裝PHP7

rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm

執行命令安裝php7:

yum install php70w.x86_64 php70w-cli.x86_64 php70w-common.x86_64 php70w-gd.x86_64 php70w-ldap.x86_64 php70w-mbstring.x86_64 php70w-mcrypt.x86_64 php70w-mysql.x86_64 php70w-pdo.x86_64

安裝php-fpm:

yum install php70w-fpm php70w-opcache

啓動php-fpm:

systemctl start php-fpm

修改 /etc/nginx/conf.d/default.conf 文件,找到下面這段並改成以下所示:

複製代碼

location ~ \.php$ {
        root           /usr/share/nginx/html;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }

複製代碼

接着到 /usr/share/nginx/html 目錄下建立一個test.php文件,內容爲phpinfo():

vi /usr/share/nginx/html/test.php

#內容
<?php
phpinfo();

保存退出。接着重啓nginx:

nginx -s reload

打開瀏覽器,看到以下圖說明運行成功:

至此環境搭建完成。

相關文章
相關標籤/搜索