基本流程
使用云服務(wù)器搭建LNMP平臺的操作步驟如下:
- 準(zhǔn)備編譯環(huán)境。
- 安裝Nginx。
- 安裝MySQL哨坪。
- 安裝PHP-FPM。
- 測試訪問乍楚。
步驟一:準(zhǔn)備編譯環(huán)境当编。
本文主要說明手動安裝LNMP平臺的操作步驟,您也可以在 阿里云-云市場 購買LNMP鏡像直接啟動ECS徒溪,以便快速建站忿偷。
-
關(guān)閉防火墻臊泌。
輸入
systemctl status firewalld
命令查看當(dāng)前防火墻的狀態(tài)鲤桥。如果防火墻的狀態(tài)參數(shù)是active,則防火墻為開啟狀態(tài)渠概。如果防火墻的狀態(tài)參數(shù)是inactive茶凳,則防火墻為關(guān)閉狀態(tài)。如上圖所示播揪,此處防火墻為開啟狀態(tài)贮喧,需要運行如下命令關(guān)閉防火墻:
-
如果您想臨時關(guān)閉防火墻,輸入命令
systemctl stop firewalld
猪狈。說明 這只是暫時關(guān)閉防火墻箱沦,下次重啟Linux后,防火墻還會開啟罪裹。
-
如果您想永久關(guān)閉防火墻饱普,輸入命令
systemctl disable firewalld
运挫。說明 您可參考firewalld官網(wǎng)信息來決定何時開啟防火墻。
-
-
關(guān)閉SELinux套耕。
-
如果SELinux狀態(tài)參數(shù)是Enforcing,則SELinux為開啟狀態(tài)冯袍。如果SELinux狀態(tài)參數(shù)是Disabled匈挖, 則SELinux為關(guān)閉狀態(tài)。如上圖所示康愤,此處SELinux為開啟狀態(tài)儡循,需要運行如下命令關(guān)閉SELinux:
-
如果您想臨時關(guān)閉SELinux,輸入命令
setenforce 0
征冷。說明 這只是暫時關(guān)閉SELinux择膝,下次重啟Linux后,SELinux還會開啟检激。
-
如果您想永久關(guān)閉SELinux肴捉,輸入命令
vi /etc/selinux/config
編輯SELinux配置文件∈迨眨回車后齿穗,把光標(biāo)移動到SELINUX=enforcing
這一行,輸入i
進入編輯模式饺律,修改為SELINUX=disabled
窃页, 按Esc
鍵,然后輸入:wq
并回車以保存并關(guān)閉SELinux配置文件复濒。說明 您可參考redhat關(guān)于SELinux的官方文檔來決定何時開啟SELinux脖卖。
-
重啟系統(tǒng)使設(shè)置生效。
參考添加安全組規(guī)則芝薇,放行所需端口入方向規(guī)則胚嘲。
步驟二:安裝Nginx。
-
安裝依賴包洛二。
yum groupinstall "Development tools" -y yum install zlib-devel pcre-devel openssl-devel -y yum install epel-release -y yum 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_module make && make install mkdir -p /var/tmp/nginx/client
-
添加運行Nginx服務(wù)進程的用戶晾嘶。
useradd nginx chown -R nginx:nginx /etc/nginx/
-
添加nginx.service啟動配置文件妓雾。
輸入命令
vi /usr/lib/systemd/system/nginx.service
打開Nginx的啟動配置文件,然后在配置文件中寫下如下內(nèi)容:[Unit] Description=nginx - high performance web server Documentation=https://nginx.org/en/docs/ After=network-online.target remote-fs.target nss-lookup.target Wants=network-online.target [Service] Type=forking PIDFile=/var/run/nginx.pid ExecStartPre=/usr/sbin/nginx -t -c /etc/nginx/nginx.conf ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf ExecReload=/bin/kill -s HUP $MAINPID ExecStop=/bin/kill -s TERM $MAINPID [Install] WantedBy=multi-user.target
按下
Esc
鍵垒迂,然后輸入:wq
并回車以保存并關(guān)閉Nginx啟動配置文件械姻。 -
啟動Nginx服務(wù)并設(shè)置開機自動啟動。
systemctl start nginx systemctl enable nginx
-
登錄ECS管理控制臺机断,單擊左側(cè)導(dǎo)航欄中的實例楷拳,在實例列表中找到正在部署環(huán)境的實例绣夺,從這個實例的IP地址項中復(fù)制它的公網(wǎng)IP,用瀏覽器訪問這個IP地址可看到默認(rèn)歡迎頁面欢揖。
步驟三:安裝MySQL陶耍。
-
準(zhǔn)備編譯環(huán)境。
yum install ncurses-devel bison gnutls-devel –y yum install cmake -y
-
準(zhǔn)備MySQL數(shù)據(jù)存放目錄她混。
mkdir /mnt/data groupadd -r mysql useradd -r -g mysql -s /sbin/nologin mysql id mysql uid=995(mysql) gid=993(mysql) groups=993(mysql)
-
更改數(shù)據(jù)目錄屬主和屬組烈钞。
chown -R mysql:mysql /mnt/data
-
下載穩(wěn)定版源碼包解壓編譯。
wget https://downloads.mysql.com/archives/get/file/mysql-5.6.24.tar.gz tar xvf mysql-5.6.24.tar.gz -C /usr/local/src cd /usr/local/src/mysql-5.6.24 cmake . -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 \ -DMYSQL_UNIX_ADDR=/tmp/mysql.sock \ -DWITH_SYSTEMD=auto \ -DINSTALL_SYSTEMD_UNITDIR=/usr/lib/systemd/system \ -DDEFAULT_COLLATION=utf8_general_ci make && make install
-
修改安裝目錄的屬組為mysql坤按。
chown -R mysql:mysql /usr/local/mysql/
-
初始化數(shù)據(jù)庫并復(fù)制配置文件毯欣。
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
-
修改配置文件中的安裝路徑及數(shù)據(jù)目錄存放路徑。
echo -e "basedir = /usr/local/mysql\ndatadir = /mnt/data\n" >> /etc/my.cnf
-
添加mysql.service啟動配置文件臭脓。
輸入命令
vi /usr/lib/systemd/system/mysql.service
打開MySQL的啟動配置文件酗钞,然后在配置文件中寫下如下內(nèi)容:[Unit] Description=MySQL Community Server After=network.target After=syslog.target [Install] WantedBy=multi-user.target Alias=mysql.service [Service] User=mysql Group=mysql PermissionsStartOnly=true ExecStart=/usr/local/mysql/bin/mysqld TimeoutSec=600 Restart=always PrivateTmp=false
按下
Esc
鍵,然后輸入:wq
并回車以保存并關(guān)閉MySQL啟動配置文件来累。 -
設(shè)置PATH環(huán)境變量算吩。
echo "export PATH=$PATH:/usr/local/mysql/bin" > /etc/profile.d/mysql.sh source /etc/profile.d/mysql.sh
設(shè)置開機啟動MySQL。
```
systemctl enable mysql
```
- 啟動MySQL服務(wù)佃扼。
```
systemctl start mysql
mysql –h 127.0.0.1
```
步驟四:安裝PHP-FPM。
Nginx作為web服務(wù)器蔼夜,當(dāng)它接收到請求后兼耀,不支持對外部程序的直接調(diào)用或者解析,必須通過FastCGI進行調(diào)用求冷。如果是PHP請求瘤运,則交給PHP解釋器處理,并把結(jié)果返回給客戶端匠题。PHP-FPM是支持解析PHP的一個FastCGI進程管理器拯坟。提供了更好管理PHP進程的方式,可以有效控制內(nèi)存和進程韭山、可以平滑重載PHP配置郁季。
-
安裝依賴包。
yum install libmcrypt libmcrypt-devel mhash mhash-devel libxml2 libxml2-devel bzip2 bzip2-devel
-
下載穩(wěn)定版源碼包解壓編譯钱磅。
wget http://cn2.php.net/get/php-5.6.23.tar.bz2/from/this/mirror cp mirror php-5.6.23.tar.bz2 tar xvf php-5.6.23.tar.bz2 -C /usr/local/src cd /usr/local/src/php-5.6.23 ./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-bz2 make && make install
-
添加PHP和PHP-FPM配置文件梦裂。
cp /usr/local/src/php-5.6.23/php.ini-production /etc/php.ini cd /usr/local/php/etc/ cp php-fpm.conf.default php-fpm.conf sed -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的啟動配置文件盖淡,然后在配置文件中寫下如下內(nèi)容:[Unit] Description=The PHP FastCGI Process Manager After=network.target [Service] Type=simple PIDFile=/usr/local/php/var/run/php-fpm.pid ExecStart=/usr/local/php/sbin/php-fpm --nodaemonize --fpm-config /usr/local/php/etc/php-fpm.conf ExecReload=/bin/kill -USR2 $MAINPID PrivateTmp=true [Install] WantedBy=multi-user.target
按下
Esc
鍵年柠,然后輸入:wq
并回車以保存并關(guān)閉PHP-FPM啟動配置文件。 -
啟動PHP-FPM服務(wù)并設(shè)置開機自動啟動褪迟。
systemctl start php-fpm systemctl enable php-fpm
-
啟動服務(wù)冗恨。
service php-fpm start
-
添加Nginx對FastCGI的支持答憔。
-
備份默認(rèn)的Nginx配置文件。
cp /etc/nginx/nginx.conf /etc/nginx/nginx.confbak cp nginx.conf.default nginx.conf.default.bak cp /etc/nginx/nginx.conf.default /etc/nginx/nginx.conf
-
輸入命令
vi /etc/nginx/nginx.conf
編輯Nginx的配置文件掀抹,在所支持的主頁面格式中添加PHP格式的主頁虐拓,類似如下:location / { root /etc/nginx/html; index index.php index.html index.htm; }
-
取消以下內(nèi)容前面的注釋:
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
并回車以保存并關(guān)閉Nginx配置文件。
-
輸入命令
systemctl restart nginx
重新載入Nginx的配置文件谱轨。-
輸入命令
vi /etc/nginx/html/index.php
打開index.php文件戒幔,然后在文件中寫入如下內(nè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
并回車以保存并關(guān)閉index.php文件土童。
步驟五:測試訪問
登錄 ECS管理控制臺诗茎,單擊左側(cè)導(dǎo)航欄中的實例,在實例列表中復(fù)制正在部署環(huán)境的實例的公網(wǎng)IP地址献汗。用瀏覽器訪問這個公網(wǎng)IP地址敢订,如您看見如下圖所示頁面,則表示LNMP平臺構(gòu)建完成罢吃。