阿里雲ECS例項 CentOS7 下搭建LNMP(Linux+Nginx+Mariadb+PHP)教程
一:安裝Nginx
1.下載對應當前系統版本的nginx包(package)
wget http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
2.建立nginx的yum倉庫(預設yum是沒有nginx的)
rpm -ivh nginx-release-centos-7-0.el7.ngx.noarch.rpm
3.下載並安裝nginx
yum install nginx
4.nginx啟動
systemctl start nginx.service
二.安裝php (7.2)
1:為yum包管理新增新的映象原始檔
rpm -Uvh https://mirror.webtatic.com/yum/el7/epel-release.rpm
rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
2:安裝
yum install php72w php72w-devel php72w-common php72w-process php72w-mysql php72w-pdo php72w-opcache php72w-xml php72w-gd.x86_64
5.安裝PHP FPM
yum install php72w-fpm
*修改nginx配置檔案之前記得備份(附帶我的配置檔案)
1.nginx配置檔案位置:(/etc/nginx/conf.d/default.conf)
修改root目錄(如果沒有需求也可以不用修改,使用預設即可):
location / { root /home; index index.html index.htm index.php; autoindex on; if (!-e $request_filename) { rewrite ^/(.*)$ /index.php?s=/$1 last; break; } } location ~ \.php$ { root /home; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name; include fastcgi_params; }
2.php-fpm配置檔案位置:(/etc/php-fpm.d/www.conf)
修改
user =nginx
group=nginx
3.啟動nginx服務:
systemctl start nginx.service
如需設定開機自啟使用以下命令:
sudo systemctl enable nginx.service
4.啟動PHP-FPM:
systemctl start php-fpm.service
如需設定開機自啟試用以下命令:
sudo systemctl enable php-fpm.service
三:安裝mysql
1、建立mariadb.repo
vim /etc/yum.repos.d/mariadb.repo
寫入以下內容:
# MariaDB 10.2 CentOS repository list - created 2017-07-03 06:59 UTC
# http://downloads.mariadb.org/mariadb/repositories/
[mariadb]
name = MariaDB
baseurl = https://mirrors.ustc.edu.cn/mariadb/yum/10.3/centos7-amd64
gpgkey=https://mirrors.ustc.edu.cn/mariadb/yum/RPM-GPG-KEY-MariaDB
gpgcheck=1
以上是中國科學技術大學的 mariadb yum源,下載起來非常快,如果直接官網下載,非常的慢,非常的慢,因為這軟體加依賴包,都有167M !!!
2、yum安裝最新版本mariadb
yum install MariaDB-server MariaDB-client
3、錯誤處理:
若安裝時遇到錯誤 “Failed to connect to 2001:da8:d800:95::110: Network is unreachable”,將源地址中的 mirrors.ustc.edu.cn 替換為 ipv4.mirrors.edu.cn 以強制使用 IPv4:
sudo sed -i 's#//mirrors.ustc.edu.cn#//ipv4.mirrors.ustc.edu.cn#g' /etc/yum.repos.d/mariadb.repo
4、安裝之後的各種配置:
1、啟動MariaDB
安裝完成MariaDB,首先啟動MariaDB,兩條命令都可以
systemctl start mariadb
或者
service mariadb start
設定開機啟動
systemctl enable mariadb
或者:
chkconfig mariadb on
2、接下來進行MariaDB的相關簡單配置
設定密碼
mysql -uroot -p 回車-》直接回車進入
MariaDB [(none)]> use mysql;
MariaDB [mysql]> UPDATE user SET password=password('12345678') WHERE user='root';
MariaDB [mysql]> flush privileges;
MariaDB [mysql]> exit
或者:
mysql_secure_installation
首先是設定密碼,會提示先輸入密碼
Enter current password for root (enter for none):<–初次執行直接回車
設定密碼
Set root password? [Y/n] <– 是否設定root使用者密碼,輸入y並回車或直接回車
New password: <– 設定root使用者的密碼
Re-enter new password: <– 再輸入一次你設定的密碼
其他配置
Remove anonymous users? [Y/n] <– 是否刪除匿名使用者,回車
Disallow root login remotely? [Y/n] <–是否禁止root遠端登入,回車,
Remove test database and access to it? [Y/n] <– 是否刪除test資料庫,回車
Reload privilege tables now? [Y/n] <– 是否重新載入許可權表,回車
初始化MariaDB完成,接下來測試登入
mysql -uroot -p [回車,之後輸入密碼]
3、配置MariaDB的字符集
設定客戶端:
vim /etc/my.cnf.d/mysql-clients.cnf
[mysql]
default-character-set=utf8
設定服務端:
vim /etc/my.cnf.d/server.cnf
[mysqld]
init_connect='SET collation_connection = utf8_general_ci'
init_connect='SET NAMES utf8'
character-set-server=utf8
collation-server=utf8_general_ci
skip-character-set-client-handshake
#開啟慢查詢
slow_query_log = ON
slow_query_log_file = /usr/local/mysql/data/slow.log
long_query_time = 1
全部配置完成,重啟mariadb
systemctl restart mariadb
之後進入MariaDB檢視字符集
mysql> show variables like "%character%";show variables like "%collation%";
MariaDB [(none)]> show variables like "%character%";show variables like "%collation%";
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)
+----------------------+-----------------+
| Variable_name | Value |
+----------------------+-----------------+
| collation_connection | utf8_general_ci |
| collation_database | utf8_general_ci |
| collation_server | utf8_general_ci |
+----------------------+-----------------+
3 rows in set (0.00 sec)
字符集配置完成。
4、新增使用者,設定許可權
建立使用者命令
mysql>create user [email protected] identified by 'password';
直接建立使用者並授權的命令
授予外網登陸許可權,但不能二級授權;
mysql>grant all privileges on *.* to [email protected]'%' identified by 'password';
授予許可權並且可以二次授權
mysql>grant all privileges on *.* to [email protected]'hostname' identified by 'password' with grant option;
簡單的使用者和許可權配置基本就這樣了。
其中只授予部分許可權把 其中 all privileges或者all改為: select,insert,update,delete,create,drop,index,alter,grant,references,reload,shutdown,process,file
其中一部分。