阿里雲centOS下LNMP搭建

蜻蜓隊長dragonfly429發表於2018-05-03

公司新買了個阿里雲專門給小程式用,已經裝了centOS,我再裝下環境,記錄下。 時間 2018年5月3日

1檢視伺服器資訊

我得到了ip 使用者名稱和密碼 putty登陸 看了下 40G+2G centOS6.3 nginx之類的沒有裝

2.安裝第三方yum源

wget http://www.atomicorp.com/installers/atomic  #下載
sh ./atomic  #安裝
yum check-update  #更新yum源
複製程式碼
2.1如果不能用wget,那麼先要安裝wget
yum -y install wget
複製程式碼
2.2 如果出現Permission denied

改下許可權

chmod 777 atomic
複製程式碼

3和4被吃了 所以直接5

5.安裝nginx

yum remove httpd* php* #刪除系統自帶的軟體包
yum install nginx #安裝nginx 根據提示輸入y進行安裝
chkconfig nginx on #設定nginx開機啟動
service nginx start #啟動nginx
複製程式碼
# yum install nginx
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * atomic: mirrors.neusoft.edu.cn
 * base: mirrors.aliyuncs.com
 * epel: mirrors.aliyuncs.com
 * extras: mirrors.aliyuncs.com
 * updates: mirrors.aliyuncs.com
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package nginx.x86_64 0:1.13.12-4308.el6.art will be installed
--> Finished Dependency Resolution

Dependencies Resolved

========================================================================================================
 Package             Arch                 Version                            Repository            Size
========================================================================================================
Installing:
 nginx               x86_64               1.13.12-4308.el6.art               atomic               956 k

Transaction Summary
========================================================================================================
Install       1 Package(s)

Total download size: 956 k
Installed size: 2.6 M
Is this ok [y/N]: yes
Downloading Packages:
nginx-1.13.12-4308.el6.art.x86_64.rpm                                            | 956 kB     00:00
warning: rpmts_HdrFromFdno: Header V4 RSA/SHA256 Signature, key ID 4520afa9: NOKEY
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY.art.txt
Importing GPG key 0x5EBD2744:
 Userid : Atomic Rocket Turtle <admin@atomicrocketturtle.com>
 Package: atomic-release-1.0-21.el6.art.noarch (installed)
 From   : /etc/pki/rpm-gpg/RPM-GPG-KEY.art.txt
Is this ok [y/N]: yes
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY.atomicorp.txt
Importing GPG key 0x4520AFA9:
 Userid : Atomicorp (Atomicorp Official Signing Key) <support@atomicorp.com>
 Package: atomic-release-1.0-21.el6.art.noarch (installed)
 From   : /etc/pki/rpm-gpg/RPM-GPG-KEY.atomicorp.txt
Is this ok [y/N]: y
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
Warning: RPMDB altered outside of yum.
  Installing : nginx-1.13.12-4308.el6.art.x86_64                                                    1/1
----------------------------------------------------------------------

Thanks for using nginx!

Please find the official documentation for nginx here:
* http://nginx.org/en/docs/

Please subscribe to nginx-announce mailing list to get
the most important news about nginx:
* http://nginx.org/en/support.html

Commercial subscriptions for nginx are available on:
* http://nginx.com/products/

----------------------------------------------------------------------
  Verifying  : nginx-1.13.12-4308.el6.art.x86_64                                                    1/1

Installed:
  nginx.x86_64 0:1.13.12-4308.el6.art

Complete!
複製程式碼

如果發現埠占用,一般有兩種常見情況 1.是nginx的ipv6 bug, 2.是其他程式埠占用 我這次遇到的是httpd佔用 ipv6的情況見下文 8.1


# netstat -ntpl
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      1415/httpd
tcp        0      0 0.0.0.0:21                  0.0.0.0:*                   LISTEN      1422/vsftpd
tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN      1017/sshd
tcp        0      0 0.0.0.0:3306                0.0.0.0:*                   LISTEN      1389/mysqld
tcp        0      0 :::22                       :::*                        LISTEN      1017/sshd
# kill 1415
# service nginx start
Starting nginx:                                            [  OK  ]

複製程式碼

此時瀏覽器中檢視你的網站 應該能看到nginx的歡迎介面

6.安裝MySQL

yum install mysql mysql-server #輸入Y即可自動安裝,直到安裝完成
/etc/init.d/mysqld start #啟動MySQL
chkconfig mysqld on #設為開機啟動
cp /usr/share/mysql/my-medium.cnf /etc/my.cnf #拷貝配置檔案(注意:如果/etc目錄下面預設有一個my.cnf,直接覆蓋即可)
為root賬戶設定密碼
mysql_secure_installation 回車,根據提示輸入,輸入2次密碼,回車,根據提示一路輸入Y,最後出現:Thanks for using MySQL!
MySql密碼設定完成,重新啟動 MySQL:
/etc/init.d/mysqld restart #重啟
/etc/init.d/mysqld stop #停止
/etc/init.d/mysqld start #啟動
複製程式碼

然後我遇到了一些問題,因為這個伺服器上裝過mysql 我刪了重灌, 啟動失敗了,查了資料參照這個解決了 Linux下重新安裝MySql後出現'MySQL Daemon failed to start‘解決方法

7.安裝PHP5

yum install php php-fpm #根據提示輸入Y直到安裝完成
安裝PHP元件,使PHP5支援MySQL
yum install php-mysql php-gd libjpeg* php-imap php-ldap php-odbc php-pear php-xml php-xmlrpc php-mbstring php-mcrypt php-bcmath php-mhash libmcrypt #這裡選擇以上安裝包進行安裝,根據提示輸入Y回車
chkconfig php-fpm on #設定php-fpm開機啟動
/etc/init.d/php-fpm start #啟動php-fpm
複製程式碼

8.配置nginx支援php

cp /etc/nginx/nginx.conf /etc/nginx/nginx.confbak  #備份原有配置檔案
vi /etc/nginx/nginx.conf  #編輯
user nginx nginx; #修改nginx執行賬號為:nginx組的nginx使用者
:wq #儲存退出
複製程式碼
cp /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/default.confbak #備份原有配置檔案
vi /etc/nginx/conf.d/default.conf #編輯
index index.php index.html index.htm; #增加index.php
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
#取消FastCGI server部分location的註釋,並要注意fastcgi_param行的引數,改為$document_root$fastcgi_script_name,或者使用絕對路徑
service nginx restart #重啟nginx
複製程式碼
8.1可能出現修改nginx配置檔案後,重啟報錯:

nginx: [emerg] socket() [::]:80 failed (97: Address family not supported by protocol) 解決辦法:

vim /etc/nginx/conf.d/default.conf
複製程式碼

listen       80 default_server;
listen       [::]:80 default_server;
複製程式碼

改為:

listen       80;
#listen      [::]:80 default_server;
複製程式碼

重新啟動nginx即可

9.php配置

vi /etc/php.ini #編輯
date.timezone = PRC #把前面的分號去掉,改為date.timezone = PRC
disable_functions = passthru,exec,system,chroot,scandir,chgrp,chown,shell_exec,proc_open,proc_get_status,ini_alter,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,stream_socket_server,escapeshellcmd,dll,popen,disk_free_space,checkdnsrr,checkdnsrr,getservbyname,getservbyport,disk_total_space,posix_ctermid,posix_get_last_error,posix_getcwd,posix_getegid,posix_geteuid,posix_getgid,posix_getgrgid,posix_getgrnam,posix_getgroups,posix_getlogin,posix_getpgid,posix_getpgrp,posix_getpid,posix_getppid,posix_getpwnam,posix_getpwuid, posix_getrlimit, posix_getsid,posix_getuid,posix_isatty,posix_kill,posix_mkfifo,posix_setegid,posix_seteuid,posix_setgid,posix_setpgid,posix_setsid,posix_setuid,posix_strerror,posix_times,posix_ttyname,posix_uname
#在386行 列出PHP可以禁用的函式,如果某些程式需要用到這個函式,可以刪除,取消禁用。
expose_php = Off #禁止顯示php版本的資訊
short_open_tag = ON #支援php短標籤
open_basedir = 網站根目錄:/tmp/
#設定表示允許訪問當前目錄(即PHP指令碼檔案所在之目錄)和/tmp/目錄;冒號為分隔符。
:wq! #儲存退出
複製程式碼

open_basedir配置選項中有些教程會配置為open_basedir=.:/tmp/,其中’.’代表當前目錄。這種方式在nginx+phpfastcgi下好像行不通。

配置php-fpm
cp /etc/php-fpm.d/www.conf /etc/php-fpm.d/www.confbak #備份原有配置檔案
vi /etc/php-fpm.d/www.conf  #編輯
user = nginx #修改使用者為nginx
group = nginx #修改組為nginx
:wq #儲存退出
複製程式碼

10.測試

cd /usr/share/nginx/html
vi index.php #新增以下程式碼
<?php
phpinfo();
?>
:wq! #儲存退出
複製程式碼
chown nginx.nginx /usr/share/nginx/html -R #設定許可權
service nginx restart #重啟nginx
service php-fpm restart #重啟php-fpm
複製程式碼

在客戶端瀏覽器輸入伺服器IP地址,可以看到相關的配置資訊!說明lnmp配置成功!

至此,CnetOS 安裝配置LNMP(Nginx+PHP+MySQL)完成。 ####11.檢視版本 檢視centos版本

more /etc/issue;
CentOS release 6.8 (Final)
複製程式碼

檢視nginx版本

nginx -v 
nginx version: nginx/1.6.2
複製程式碼

檢視mysql版本

mysql -uroot -p
然後輸入 password
status;
mysql  Ver 14.14 Distrib 5.5.55, for Linux (x86_64) using readline 5.1
複製程式碼

檢視php版本

php -v
PHP 5.4.45 (cli) (built: Aug 23 2016 14:41:13)
複製程式碼

11.要記錄儲存的資訊

item 資訊
VPS 網站網址
使用者名稱密碼
VPS上面的例項 IP地址
centos的root密碼
CentOS release 6.3 (Final)
mysql mysqlroot密碼

參考 CentOS 6.5安裝配置LNMP伺服器(Nginx+PHP+MySQL) Getting Started with Linode CentOS下檢視apache,php,mysql版本資訊 nginx伺服器安裝及配置檔案詳解

Linux下重新安裝MySql後出現'MySQL Daemon failed to start‘解決方法 LNMP伺服器安裝配置(Rhel+Nginx+PHP+MySQL)

相關文章