本文檔介紹如何手動在ECS實例上搭建LNMP環境(CentOS 7),其中LNMP分別代表Linux、Nginx、MySQL和PHP。
項目配置
本篇教程在示例步驟中使用了以下版本的軟件。操作時,請您以實際軟件版本為準。
操作系統:CentOS 7.2 64位
Nginx版本:Nginx 1.10.2
MySQL版本:MySQL 5.6.24
PHP版本:PHP 5.6.38
申請阿里雲服務時,可以使用2000元阿里雲代金券,阿里雲官網領取網址:https://dashi.aliyun.com/site/yun/youhui (長期有效)
適用對象
適用於熟悉Linux操作系統,剛開始使用阿里雲進行建站的個人用戶。
阿里雲服務器1核2G低至89元/年,阿里雲官活動網址:https://dashi.aliyun.com/site/yun/aliyun
基本流程
使用雲服務器ECS搭建LNMP平臺的操作步驟如下:
準備編譯環境。
安裝Nginx。
安裝MySQL。
安裝PHP-FPM。
測試訪問。
步驟一:準備編譯環境。
本文主要說明手動安裝LNMP平臺的操作步驟,您也可以在 雲市場 購買LNMP鏡像直接啟動ECS,以便快速建站。
說明 本篇教程創建的ECS實例選用了CentOS 7.2 64位的操作系統,專有網絡和公網IP。
輸入命令cat /etc/redhat-release查看系統版本。
關閉防火牆。
輸入systemctl status firewalld命令查看當前防火牆的狀態。
如果防火牆的狀態參數是active,則防火牆為開啟狀態。如果防火牆的狀態參數是inactive,則防火牆為關閉狀態。如上圖所示,此處防火牆為開啟狀態,需要運行如下命令關閉防火牆:
如果您想臨時關閉防火牆,輸入命令systemctl stop firewalld。
說明 這只是暫時關閉防火牆,下次重啟Linux後,防火牆還會開啟。
如果您想永久關閉防火牆,輸入命令systemctl disable firewalld。
說明 您可參考firewalld官網信息來決定何時開啟防火牆。
關閉SELinux。
如果您想臨時關閉SELinux,輸入命令setenforce 0。
說明 這只是暫時關閉SELinux,下次重啟Linux後,SELinux還會開啟。
如果您想永久關閉SELinux,輸入命令vi /etc/selinux/config編輯SELinux配置文件。回車後,把光標移動到SELINUX=enforcing這一行,按下i鍵進入編輯模式,修改為SELINUX=disabled, 按下Esc鍵,然後輸入:wq並回車以保存並關閉SELinux配置文件。
說明 您可參考redhat關於SELinux的官方文檔來決定何時開啟SELinux。
輸入getenforce命令查看當前SELinux的狀態。
如果SELinux狀態參數是Enforcing,則SELinux為開啟狀態。如果SELinux狀態參數是Disabled, 則SELinux為關閉狀態。如上圖所示,此處SELinux為開啟狀態,需要運行如下命令關閉SELinux:
重啟系統使設置生效。
參考添加安全組規則,放行所需端口入方向規則。
步驟二:安裝Nginx。
安裝依賴包。
yum groupinstall "Development tools" -yyum install zlib-devel pcre-devel openssl-devel -yyum install epel-release -yyum install perl perl-devel perl-ExtUtils-Embed libxslt libxslt-devel libxml2 libxml2-devel gd gd-devel GeoIP GeoIP-devel -y
下載源碼包解壓編譯。
wget http://nginx.org/download/nginx-1.10.2.tar.gz
tar xvf nginx-1.10.2.tar.gz -C /usr/local/src
cd /usr/local/src/nginx-1.10.2
./configure --prefix=/etc/nginx \
--sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/tmp/nginx/client --http-proxy-temp-path=/var/tmp/nginx/proxy --http-fastcgi-temp-path=/var/tmp/nginx/fcgi --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi --http-scgi-temp-path=/var/tmp/nginx/scgi --user=nginx --group=nginx --with-pcre --with-http_v2_module --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_stub_status_module --with-http_auth_request_module --with-mail --with-mail_ssl_module --with-file-aio --with-ipv6 --with-http_v2_module --with-threads --with-stream --with-stream_ssl_modulemake && make install
mkdir -p /var/tmp/nginx/client
輸入命令nginx -v可查看Nginx的版本號。
添加運行Nginx服務進程的用戶。
useradd nginx
chown -R nginx:nginx /etc/nginx/
添加nginx.service啟動配置文件。
輸入命令vi /usr/lib/systemd/system/nginx.service打開Nginx的啟動配置文件,按下i鍵,然後在配置文件中寫下如下內容:
[Unit]Description=nginx - high performance web serverDocumentation=https://nginx.org/en/docs/After=network-online.target remote-fs.target nss-lookup.targetWants=network-online.target
[Service]Type=forkingPIDFile=/var/run/nginx.pidExecStartPre=/usr/sbin/nginx -t -c /etc/nginx/nginx.confExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.confExecReload=/bin/kill -s HUP $MAINPIDExecStop=/bin/kill -s TERM $MAINPID [Install]
WantedBy=multi-user.target
按下Esc鍵,然後輸入:wq並回車以保存並關閉Nginx啟動配置文件。
啟動Nginx服務並設置開機自動啟動。
systemctl start nginx
systemctl enable nginx
登錄ECS管理控制檯,單擊左側導航欄中的實例,在實例列表中找到正在部署環境的實例,從這個實例的IP地址項中複製它的公網IP,用瀏覽器訪問這個IP地址可看到默認歡迎頁面。
步驟三:安裝MySQL。
準備編譯環境。
yum install ncurses-devel bison gnutls-devel –y
yum install cmake -y
準備MySQL數據存放目錄。
mkdir /mnt/datagroupadd -r mysqluseradd -r -g mysql -s /sbin/nologin mysqlid mysql
更改數據目錄屬主和屬組。
chown -R mysql:mysql /mnt/data
下載穩定版源碼包解壓編譯。
wget https://downloads.mysql.com/archives/get/file/mysql-5.6.24.tar.gztar xvf mysql-5.6.24.tar.gz -C /usr/local/srccd /usr/local/src/mysql-5.6.24cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_DATADIR=/mnt/data \
-DSYSCONFDIR=/etc \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_READLINE=1 \
-DWITH_SSL=system \
-DWITH_ZLIB=system \
-DWITH_LIBWRAP=0 \
-DMYSQL_TCP_PORT=3306 \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \
-DWITH_SYSTEMD=1 \
-DINSTALL_SYSTEMD_UNITDIR=/usr/lib/systemd/system \
make && make install
修改安裝目錄的屬組為mysql。
chown -R mysql:mysql /usr/local/mysql/
初始化數據庫並複製配置文件。
cd /usr/local/mysql/usr/local/mysql/scripts/mysql_install_db --user=mysql --datadir=/mnt/data/mv /etc/my.cnf /etc/my.cnf.bak
cp /usr/local/mysql/support-files/my-default.cnf /etc/my.cnf
修改配置文件中的安裝路徑及數據目錄存放路徑。
echo -e "basedir = /usr/local/mysqlndatadir = /mnt/datan" >> /etc/my.cnf
添加mysql.service啟動配置文件。
輸入命令vi /usr/lib/systemd/system/mysql.service打開MySQL的啟動配置文件,按下i鍵,然後在配置文件中寫下如下內容:
[Unit]Description=MySQL Community ServerAfter=network.targetAfter=syslog.target
[Install]WantedBy=multi-user.targetAlias=mysql.service
[Service]User=mysqlGroup=mysqlPermissionsStartOnly=trueExecStart=/usr/local/mysql/bin/mysqldTimeoutSec=600Restart=alwaysPrivateTmp=false
按下Esc鍵,然後輸入:wq並回車以保存並關閉MySQL啟動配置文件。
設置PATH環境變量。
echo "export PATH=$PATH:/usr/local/mysql/bin" > /etc/profile.d/mysql.shsource /etc/profile.d/mysql.sh
設置開機啟動MySQL。
systemctl enable mysql
啟動MySQL服務。
systemctl start mysql
mysql –h 127.0.0.1
步驟四:安裝PHP-FPM。
Nginx作為web服務器,當它接收到請求後,不支持對外部程序的直接調用或者解析,必須通過FastCGI進行調用。如果是PHP請求,則交給PHP解釋器處理,並把結果返回給客戶端。PHP-FPM是支持解析PHP的一個FastCGI進程管理器。提供了更好管理PHP進程的方式,可以有效控制內存和進程、可以平滑重載PHP配置。
安裝依賴包。
yum install libmcrypt libmcrypt-devel mhash mhash-devel libxml2 libxml2-devel bzip2 bzip2-devel
下載穩定版源碼包解壓編譯。
wget http://cn2.php.net/get/php-5.6.38.tar.bz2/from/this/mirror
cp mirror php-5.6.38.tar.bz2
tar xvf php-5.6.38.tar.bz2 -C /usr/local/src
cd /usr/local/src/php-5.6.38
./configure --prefix=/usr/local/php \
--with-config-file-scan-dir=/etc/php.d --with-config-file-path=/etc --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --with-openssl --enable-xml --enable-sockets --enable-fpm --with-mcrypt --with-bz2make && make install
添加PHP和PHP-FPM配置文件。
cp /usr/local/src/php-5.6.38/php.ini-production /etc/php.inicd /usr/local/php/etc/
cp php-fpm.conf.default php-fpm.confsed -i 's@;pid = run/php-fpm.pid@pid = /usr/local/php/var/run/php-fpm.pid@' php-fpm.conf
添加php-fpm.service啟動配置文件。
輸入命令vi /usr/lib/systemd/system/php-fpm.service打開PHP-FPM的啟動配置文件,按下i鍵,然後在配置文件中寫下如下內容:
[Unit]Description=The PHP FastCGI Process ManagerAfter=network.target[Service]Type=simplePIDFile=/usr/local/php/var/run/php-fpm.pid ExecStart=/usr/local/php/sbin/php-fpm --nodaemonize --fpm-config /usr/local/php/etc/php-fpm.confExecReload=/bin/kill -USR2 $MAINPIDPrivateTmp=true[Install]WantedBy=multi-user.target
按下Esc鍵,然後輸入:wq並回車以保存並關閉PHP-FPM啟動配置文件。
啟動PHP-FPM服務並設置開機自動啟動。
systemctl start php-fpm
systemctl enable php-fpm
啟動服務。
service php-fpm start
添加Nginx對FastCGI的支持。
備份默認的Nginx配置文件。
cp /etc/nginx/nginx.conf /etc/nginx/nginx.confbakcp nginx.conf.default nginx.conf.default.bakcp /etc/nginx/nginx.conf.default /etc/nginx/nginx.conf
輸入命令vi /etc/nginx/nginx.conf打開Nginx的配置文件,按下i鍵,在所支持的主頁面格式中添加PHP格式的主頁,類似如下:
location / {
root /etc/nginx/html; index index.php index.html index.htm;
}
取消以下內容前面的註釋:
location ~ .php$ { root html; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; include fastcgi_params;
}
將root html;改成root /etc/nginx/html;。
將fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;改成fastcgi_param SCRIPT_FILENAME /etc/nginx/html/$fastcgi_script_name;。
按下Esc鍵,然後輸入:wq並回車以保存並關閉Nginx配置文件。
輸入命令systemctl restart nginx重新載入Nginx的配置文件。
輸入命令vi /etc/nginx/html/index.php打開index.php文件,按下i鍵,然後在文件中寫入如下內容:
<?php$conn=mysql_connect('127.0.0.1','root','');if ($conn){echo "LNMP platform connect to mysql is successful!";
}else{echo "LNMP platform connect to mysql is failed!";
}
phpinfo();?>
按下Esc鍵,然後輸入:wq並回車以保存並關閉index.php文件。
步驟五:測試訪問
登錄 ECS管理控制檯,單擊左側導航欄中的實例,在實例列表中複製正在部署環境的實例的公網IP地址。用瀏覽器訪問這個公網IP地址,如您看見如下圖所示頁面,則表示LNMP平臺構建完成。