搭建LNMP環境(CentOS 7)-阿裏雲ECS
本文檔介紹如何手動在ECS實例上搭建LNMP環境(CentOS 7),其中LNMP分別代表Linux、Nginx、MySQL和PHP。
本篇教程在示例步驟中使用了以下版本的軟件。操作時,請您以實際軟件版本為准。
適用於熟悉Linux操作系統,剛開始使用阿裏雲進行建站的個人用戶。
使用雲服務器ECS搭建LNMP平台的操作步驟如下:
本文主要說明手動安裝LNMP平台的操作步驟,您也可以在 雲市場 購買LNMP鏡像直接啟動ECS,以便快速建站。
cat /etc/redhat-release
查看系統版本。
輸入systemctl status firewalld
命令查看當前防火牆的狀態。
systemctl stop firewalld
。
systemctl disable firewalld
。
setenforce 0
。
vi /etc/selinux/config
編輯SELinux配置文件。回車後,把光標移動到SELINUX=enforcing
這一行,按下i
鍵進入編輯模式,修改為SELINUX=disabled
, 按下Esc
鍵,然後輸入:wq
並回車以保存並關閉SELinux配置文件。
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 -v
可查看Nginx的版本號。useradd nginx
chown -R nginx:nginx /etc/nginx/
輸入命令vi /usr/lib/systemd/system/nginx.service
打開Nginx的啟動配置文件,按下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
並回車以保存並關閉Nginx啟動配置文件。
systemctl start nginx
systemctl enable nginx
yum install ncurses-devel bison gnutls-devel –y
yum install cmake -y
mkdir /mnt/data groupadd -r mysql useradd -r -g mysql -s /sbin/nologin mysql id mysql
chown -R mysql:mysql /mnt/data
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
> -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
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
輸入命令vi /usr/lib/systemd/system/mysql.service
打開MySQL的啟動配置文件,按下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
並回車以保存並關閉MySQL啟動配置文件。
echo "export PATH=$PATH:/usr/local/mysql/bin" > /etc/profile.d/mysql.sh source /etc/profile.d/mysql.sh
systemctl enable mysql
systemctl start mysql
mysql –h 127.0.0.1
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-bz2 make && make install
cp /usr/local/src/php-5.6.38/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
輸入命令vi /usr/lib/systemd/system/php-fpm.service
打開PHP-FPM的啟動配置文件,按下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
並回車以保存並關閉PHP-FPM啟動配置文件。
systemctl start php-fpm
systemctl enable php-fpm
service php-fpm start
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的配置文件,按下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平台構建完成。