###### tags: `mirle` # CentOS 7 LNMP - Nginx & PHP 7.4 & MariaDB & Laravel 7 # 1.架設Server (Linode / DigitalOcean / HostMonster) ## 以root登入 ### 更新yum <center> yum update</center> ### 建立新使用者 <center>adduser xxx</center> <center>passwd xxx</center> ### 編輯取得最高權限 <center>vim /etc/sudoers</center> <center>xxx ALL=(ALL)  ALL</center> ![](https://i.imgur.com/tFLlGlG.png) ### 停用root登入權限 <center>vi /etc/ssh/sshd_config</center> <center>PermitRootLogin no</center> ![](https://i.imgur.com/X2tWS26.png) ### 以剛建立之使用者重新登入 #### CentOS 7重啟sshd <center> systemctl restart sshd</center> # 2.安裝服務(Nginx、php7.4、php-fpm) ## Nginx http://nginx.org/en/linux_packages.html#RHEL-CentOS ![](https://i.imgur.com/cX4j5NC.png) ### 設定防火牆開啟HTTP(80 port)服務 <center>firewall-cmd --permanent --zone=public --add-service=http;</center> <center>firewall-cmd --reload;</center> ## 設定預設開機啟動nginx <center>systemctl enable nginx</center> ## 啟動nginx <center>systemctl start nginx</center> ## 設定default.conf (可於安裝php-fpm後再進行以下步驟) <center> vim /etc/nginx/conf.d/default.conf </center> ``` ... location / { root /var/www/html; index index.php index.html index.htm; } ... location ~ \.php$ { root /var/www/html; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; include fastcgi_params; } ``` #查看nginx語法正確與否 ``` nginx -t ``` #重啟nginx ``` systemctl restart nginx ``` php7.3 #Installation Steps of PHP 7.0, 7.1, 7.2 & 7.3 on CentOS 7 Server1) Install yum-utils and enable EPEL repository Login to your server and use the below yum command to install yum-utils & enable epel repository ``` yum install epel-release yum-utils -y ``` Or ``` rpm -Uvh https://rpms.remirepo.net/enterprise/remi-release-7.rpm; rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm; ``` #Download and Install remirepo using yum command ``` yum install http://rpms.remirepo.net/enterprise/remi-release-7.rpm ``` #Base on your requirement, configure the PHP 7.x repository To configure PHP 7.3 repository, use below command ``` yum-config-manager --enable remi-php73 ``` #Install PHP 7.3 along with dependencies. In this tutorial, I will install latest version of PHP 7.3 along with its modules, Run beneath yum command ``` yum install php php-ctype php-json php-openssl php-nette-tokenizer php-pecl-zip php-pdo php-mbstring php-xml (php-mysqli) ``` #設定php.ini (可於安裝php-fpm後再進行以下步驟) ``` vi /etc/php.ini ``` # 建立index.php ``` vim /var/www/html/index.php ``` <?php phpinfo(); ?> php-fpm ``` yum install php-fpm ``` #設定php-fpm 修改user和group,為nginx ``` vim /etc/php-fpm.d/www.conf ``` #設定預設開機開啟php-fpm ``` systemctl enable php-fpm ``` #啟動php-fpm ``` systemctl start php-fpm ``` #查看www.conf參數設定 ``` vi /etc/php-fpm.d/www.conf ``` ``` [www] listen = 127.0.0.1:9000 listen.allowed_clients = 127.0.0.1 user = nginx group = nginx pm = dynamic pm.max_children = 50 pm.start_servers = 5 pm.min_spare_servers = 5 pm.max_spare_servers = 35 slowlog = /var/log/php-fpm/www-slow.log php_admin_value[error_log] = /var/log/php-fpm/www-error.log php_admin_flag[log_errors] = on ``` #查看 port 9000 ``` netstat -ant ``` #參考 https://medium.com/@iven00000000/%E6%96%BCcentos7%E5%AE%89%E8%A3%9D-nginx-php7-php-fpm-laravel5-6-df8631681acf 3.架設MySQL資料庫 #查詢 mysql ``` find / -name mysql ``` #查詢mariadb ``` rpm -qa | grep mariadb ``` #可能出現的結果mariadb-libs-5.5.56-2.el7.x86_64 #將查找出來的結果進行強制刪除 ``` rpm -e --nodeps mariadb-libs-5.5.60-1.el7_5.x86_64 ``` MySQL 8.0 ``` cd /usr/src wget https://dev.mysql.com/get/mysql80-community-release-el7-1.noarch.rpm #安裝yum源 yum localinstall mysql80-community-release-el7-1.noarch.rpm ``` Or ``` yum install https://dev.mysql.com/get/mysql80-community-release-el7-1.noarch.rpm ``` #更新yum源 ``` yum clean all yum makecache ``` #安裝MySQL ``` yum install mysql-community-server ``` #啟動MySQL ``` systemctl start mysqld ``` #啟動成功後可以檢視初始化密碼隨機生成的 ``` cat /var/log/mysqld.log | grep password ``` #初始化密碼 ``` mysql_secure_installation ``` #登入MySQL修改mysql使用者密碼 ``` mysql -u root -p mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'yourpassword'; ``` #遠端設定 ``` mysql> use mysql; mysql> update user set host='%' where user='root'; ``` #授權使用者名稱的許可權,賦予任何主機訪問資料的許可權 ``` mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%'WITH GRANT OPTION; mysql> FLUSH PRIVILEGES; ``` #查看user資訊 ``` select host, user, authentication_string, plugin from user; ``` #更改host為% ``` ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY ' yourpassword'; ``` #實施環境使用者 ``` CREATE USER 'dbuser'@'%' IDENTIFIED BY '123456'; GRANT SELECT,INSERT,UPDATE,DELETE ON *.* TO 'dbuser'@'%'; flush privileges; ``` #參考 https://www.itread01.com/article/1480580139.html phpAdmin ``` yum install wget wget https://files.phpmyadmin.net/phpMyAdmin/4.9.0.1/phpMyAdmin-4.9.0.1-all-languages.zip yum install unzip unzip phpMyAdmin-4.9.0.1-all-languages.zip cp -rf phpMyAdmin-4.9.0.1-all-languages /var/www/html/phpmyadmin/ ``` ``` vim config.sample.inc.php ``` ``` $cfg['Servers'][$i]['host'] = 'localhost'; // MySQL hostname or IP address $cfg['Servers'][$i]['port'] = '3306'; // MySQL port - leave blank for default port $cfg['Servers'][$i]['socket'] = '/var/lib/mysql/mysql.sock'; // Path to the socket - leave blank for default socket $cfg['Servers'][$i]['connect_type'] = 'socket'; // How to connect to MySQL server ('tcp' or 'socket') $cfg['Servers'][$i]['extension'] = 'mysqli'; // The php MySQL extension to use ('mysql' or 'mysqli') ``` ``` chmod 777 -R /var/lib/php/session/ yum install php-mysqli systemctl restart php-fpm ```