🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# Linux环境下LNMP环境编译安装 [TOC] ## 0. 本地系统环境 操作系统:CentOS 6.7 2.6.32-573.el6.x86_64 ## 1. nginx的安装 假定安装包安装在 `/application/tools/` 目录下。 ### 安装必备软件包 ~~~ mkdir -p /application/tools yum install pcre-devel zlib-devel gcc openssl-devel -y useradd -M -s /sbin/nologin webadmin cd /usr/local/src && wget -O nginx-1.8.0.tar.gz http://nginx.org/download/nginx-1.8.0.tar.gz tar vzxf nginx-1.8.0.tar.gz && cd nginx-1.8.0 ./configure --prefix=/application/tools/nginx1.8.0 --user=webadmin --group=webadmin --with-select_module --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_stub_status_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_stub_status_module --with-http_gzip_static_module make && make install ln -s /application/tools/nginx1.8.0 /usr/local/nginx echo 'export PATH=/usr/local/mysql/bin:/usr/local/nginx/sbin:/usr/local/bin:/usr/local/php/bin:$PATH' >> /etc/profile source /etc/profile cp nginx /etc/init.d/. # 拷贝的文件nginx为启动脚本,详细内容在下面 chmod a+x /etc/init.d/nginx && chkconfig --add nginx && chkconfig --list | grep nginx && chkconfig --level 2345 nginx on service nginx start ~~~ ### nginx的启动脚本 ~~~ #!/bin/bash # # Startup script for Nginx - this script starts and stops the nginx daemon # # chkconfig: - 85 15 # description: Nginx is an HTTP(S) server, HTTP(S) reverse proxy and IMAP/POP3 proxy server # processname: nginx # config: /usr/local/nginx/conf/nginx.conf # pidfile: /usr/local/nginx/logs/nginx.pid # Source function library. . /etc/rc.d/init.d/functions # Source networking configuration. . /etc/sysconfig/network # Check that networking is up. [ "$NETWORKING" = "no" ] && exit 0 nginx="/usr/local/nginx/sbin/nginx" prog=$(basename $nginx) NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf" [ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx lockfile=/var/lock/subsys/nginx start() { [ -x $nginx ] || exit 5 [ -f $NGINX_CONF_FILE ] || exit 6 echo -n $"Starting $prog: " daemon $nginx -c $NGINX_CONF_FILE retval=$? echo [ $retval -eq 0 ] && touch $lockfile return $retval } stop() { echo -n $"Stopping $prog: " killproc $prog -QUIT retval=$? echo [ $retval -eq 0 ] && rm -f $lockfile return $retval } restart() { configtest || return $? stop sleep 1 start } reload() { configtest || return $? echo -n $"Reloading $prog: " killproc $nginx -HUP RETVAL=$? echo } force_reload() { restart } configtest() { $nginx -t -c $NGINX_CONF_FILE } rh_status() { status $prog } rh_status_q() { rh_status >/dev/null 2>&1 } case "$1" in start) rh_status_q && exit 0 $1 ;; stop) rh_status_q || exit 0 $1 ;; restart|configtest) $1 ;; reload) rh_status_q || exit 7 $1 ;; force-reload) force_reload ;; status) rh_status ;; condrestart|try-restart) rh_status_q || exit 0 ;; *) echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}" exit 2 esac ~~~ **配置nginx虚拟主机** 将 **`include vhost/*.conf;`** 写入到nginx的主配置文件中,`cd /usr/local/nginx/conf && mkdir vhost && cd vhost` **编辑一个独立的虚拟主机配置**,例如: ~~~ server { listen 80; server_name 192.168.2.220; root /data/site/html; index index.php index.html; error_page 400 /errpage/400.html; error_page 403 /errpage/403.html; error_page 404 /errpage/404.html; location ~ .+\.php.*$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name; fastcgi_param SCRIPT_FILENAME $request_filename; include fastcgi_params; } } ~~~ ## 2. MySQL的安装 >[info] MySQL的安装可以参考[**MySQL学习笔记**](http://www.kancloud.cn/curder/mysql/61095)的安装方式 ### 安装依赖软件包 ~~~ yum install gcc gcc-c++ gcc-g77 autoconf automake zlib* fiex* libxml* ncurses-devel libmcrypt* libtool-ltdl-devel* make cmake curl curl-devel freetype freetype-devel libjpeg-turbo libjpeg-turbo-devel openjpeg-libs libpng libpng-devel gd ncurses -y ~~~ ~~~ useradd mysql -s /sbin/nologin tar xf mysql-5.5.29.tar.gz && cd mysql-5.5.29 cmake -DCMAKE_INSTALL_PREFIX=/application/tools/mysql-5.5.29 \ -DMYSQL_DATADIR=/application/tools/mysql-5.5.29/data \ -DSYSCONFDIR=/etc \ -DWITH_MYISAM_STORAGE_ENGINE=1 \ -DWITH_INNOBASE_STORAGE_ENGINE=1 \ -DWITH_MEMORY_STORAGE_ENGINE=1 \ -DWITH_READLINE=1 \ -DMYSQL_UNIX_ADDR=/var/lib/mysql/mysql.sock \ -DMYSQL_TCP_PORT=3306 \ -DENABLED_LOCAL_INFILE=1 \ -DWITH_PARTITION_STORAGE_ENGINE=1 \ -DEXTRA_CHARSETS=all \ -DDEFAULT_CHARSET=utf8 \ -DDEFAULT_COLLATION=utf8_general_ci make install ~~~ ### 初始化MySQL ~~~ ln -s /application/tools/mysql-5.5.29 /usr/local/mysql chown -R mysql:mysql /application/tools/mysql-5.5.29 mv /etc/my.cnf /etc/my.cnf.old && cp support-files/my-medium.cnf /etc/my.cnf cd /usr/local/mysql && ./scripts/mysql_install_db --user=mysql --basedir=/application/tools/mysql-5.5.29 --datadir=/application/tools/mysql-5.5.29/data cd /usr/local/mysql && cp support-files/mysql.server /etc/init.d/mysqld service mysqld start chkconfig --add mysqld chkconfig --level 2345 mysqld on ~~~ ### 其他关于优化MySQL #### 更改mysql用户密码 ~~~ mysqladmin -uroot password 'aaaaaa' ~~~ #### 删除没有用的数据库 ~~~ mysql -uroot -paaaaaa -e 'drop database test;' ~~~ #### 删除空用户 ~~~ mysql -uroot -paaaaaa -e 'delete from mysql.user where password="";flush privileges;' ~~~ ## 3. PHP的安装 **先编译libmcrypt** ~~~ tar xf libmcrypt-2.5.8.tar.bz2 cd libmcrypt-2.5.8 ./configure --prefix=/application/tools/libmcrypt2.5.8 make && make install ln -s /application/tools/libmcrypt2.5.8 /usr/local/libmcrypt ~~~ ### 解压并安装 ~~~ cd /usr/local/src && wget -O php-5.6.17.tar.gz http://cn2.php.net/get/php-5.6.17.tar.gz/from/this/mirror && tar xf php-5.6.17.tar.gz && cd php-5.6.17 ./configure --prefix=/application/tools/php-fpm-5.6.17/ --with-config-file-path=/application/tools/php-fpm-5.6.17/etc --with-mysql=/usr/local/mysql --with-pdo-mysql=/usr/local/mysql --with-mcrypt=/usr/local/libmcrypt --enable-fpm --with-gd --with-openssl --with-zlib --enable-xml --with-xmlrpc --enable-session --with-iconv --with-curl --enable-ctype --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --enable-mbregex --with-jpeg-dir --with-png-dir --with-freetype-dir --enable-gd-native-ttf --with-gettext --enable-mbstring --enable-pcntl --enable-soap --enable-sockets --enable-zip --enable-ftp make && make install ln -s /application/tools/php-fpm-5.6.17 /usr/local/php-fpm ~~~ ~~~ cd /usr/local/src/php-5.6.17 && cp php.ini-production /application/tools/php-fpm-5.6.17/etc/php.ini cd /usr/local/src/php-5.6.17 && cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm && chmod a+x /etc/init.d/php-fpm cd /usr/local/php-fpm/etc && cp php-fpm.conf.default php-fpm.conf chkconfig --add php-fpm chkconfig --level 2345 php-fpm on service php-fpm start ~~~ >[danger] **注意**:切记将修改php-fpm.conf使他的用户和web服务器(也就是这里的nginx)一致。 ~~~ vim /usr/local/php-fpm/etc/php-fpm.conf user = webadmin group = webadmin ~~~ ### 查看进程 ~~~ ps -ef|grep php-fpm ~~~ ### 测试安装结果 ~~~ vim /data/site/html/index.html <?php phpinfo(); ~~~ 在浏览器上运行看到如下结果,即安装ok。 ![](https://box.kancloud.cn/2016-02-26_56cfe36b35178.png)