Last active
August 12, 2016 06:47
-
-
Save anhtuank7c/657f46700e3ac12d1ab5e1f6345cb789 to your computer and use it in GitHub Desktop.
Install nginx with ngx_pagespeed on ubuntu 14.04 (blank new server), config nginx to support vhost, create demo vhost, deploy demo cakephp project. (aws ec2)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
## Author: Anh Tuan Nguyen (anhtuank7c@hotmail.com) | |
## Created: 08/08/2016 | |
# update, upgrade to latest source | |
sudo apt-get update -y | |
sudo apt-get upgrade -y | |
# GOOGLE PAGE SPEED | |
sudo apt-get install -y build-essential zlib1g-dev libpcre3 libpcre3-dev unzip | |
# download ngx_pagespeed | |
cd | |
NPS_VERSION=1.11.33.2 | |
wget https://github.com/pagespeed/ngx_pagespeed/archive/release-${NPS_VERSION}-beta.zip -O release-${NPS_VERSION}-beta.zip | |
unzip release-${NPS_VERSION}-beta.zip | |
cd ngx_pagespeed-release-${NPS_VERSION}-beta/ | |
wget https://dl.google.com/dl/page-speed/psol/${NPS_VERSION}.tar.gz | |
tar -xzvf ${NPS_VERSION}.tar.gz # extracts to psol/ | |
# Download and build nginx with support for pagespeed | |
cd | |
# check http://nginx.org/en/download.html for the latest version | |
NGINX_VERSION=1.10.1 | |
wget http://nginx.org/download/nginx-${NGINX_VERSION}.tar.gz | |
tar -xvzf nginx-${NGINX_VERSION}.tar.gz | |
cd nginx-${NGINX_VERSION}/ | |
./configure --add-module=$HOME/ngx_pagespeed-release-${NPS_VERSION}-beta ${PS_NGX_EXTRA_FLAGS} | |
make | |
sudo make install | |
# CREATE START/STOP/RELOAD script | |
sudo echo '#!/bin/sh | |
### BEGIN INIT INFO | |
# Provides: nginx | |
# Required-Start: $all | |
# Required-Stop: $all | |
# Default-Start: 2 3 4 5 | |
# Default-Stop: 0 1 6 | |
# Short-Description: starts the nginx web server | |
# Description: starts nginx using start-stop-daemon | |
### END INIT INFO | |
PATH=/opt/nginx/sbin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/share/nginx:/usr/share/nginx/sbin | |
DAEMON=/usr/local/nginx/sbin/nginx | |
NAME=nginx | |
DESC=nginx | |
test -x $DAEMON || exit 0 | |
# Include nginx defaults if available | |
if [ -f /etc/default/nginx ] ; then | |
. /etc/default/nginx | |
fi | |
set -e | |
case "$1" in | |
start) | |
echo -n "Starting $DESC: " | |
start-stop-daemon --start --quiet --pidfile /usr/local/nginx/logs/$NAME.pid \ | |
--exec $DAEMON -- $DAEMON_OPTS | |
echo "$NAME." | |
;; | |
stop) | |
echo -n "Stopping $DESC: " | |
start-stop-daemon --stop --quiet --pidfile /usr/local/nginx/logs/$NAME.pid \ | |
--exec $DAEMON | |
echo "$NAME." | |
;; | |
restart|force-reload) | |
echo -n "Restarting $DESC: " | |
start-stop-daemon --stop --quiet --pidfile \ | |
/usr/local/nginx/logs/$NAME.pid --exec $DAEMON | |
sleep 1 | |
start-stop-daemon --start --quiet --pidfile \ | |
/usr/local/nginx/logs/$NAME.pid --exec $DAEMON -- $DAEMON_OPTS | |
echo "$NAME." | |
;; | |
reload) | |
echo -n "Reloading $DESC configuration: " | |
start-stop-daemon --stop --signal HUP --quiet --pidfile /usr/local/nginx/logs/$NAME.pid \ | |
--exec $DAEMON | |
echo "$NAME." | |
;; | |
*) | |
N=/etc/init.d/$NAME | |
echo "Usage: $N {start|stop|restart|reload|force-reload}" >&2 | |
exit 1 | |
;; | |
esac | |
exit 0' > /home/ubuntu/nginx | |
sudo mv /home/ubuntu/nginx /etc/init.d/nginx | |
sudo chown root:root /etc/init.d/nginx | |
sudo chmod 0755 /etc/init.d/nginx | |
sudo service nginx start | |
# PHP55-fpm | |
sudo apt-get install -y php5-common php5-fpm php5-mysql php5-sqlite php5-curl php5-gd php5-cli php5-intl php5-apcu | |
# Install git, composer | |
sudo apt-get install curl php5-cli git | |
curl -sS https://getcomposer.org/installer | sudo php -- --install-dir=/usr/local/bin --filename=composer | |
# Update php configs | |
sudo sed -i "s/listen = \/var\/run\/php5-fpm.sock/listen = 127.0.0.1:9000/g" /etc/php5/fpm/pool.d/www.conf # Use a port instead of a socket | |
sudo sed -i "s/;cgi.fix_pathinfo=1/cgi.fix_pathinfo=1/g" /etc/php5/fpm/php.ini | |
sudo sed -i "s/short_open_tag = Off/short_open_tag = On/g" /etc/php5/fpm/php.ini | |
sudo sed -i "s/session.gc_maxlifetime = 1440/session.gc_maxlifetime = 43200/g" /etc/php5/fpm/php.ini # Update session timeout | |
# Remove default html folder, create config sites folder, projects folder | |
sudo rm -rf /usr/local/nginx/html | |
sudo mkdir -p /usr/local/nginx/projects | |
sudo mkdir -p /usr/local/nginx/sites-available | |
sudo mkdir -p /usr/local/nginx/sites-enabled | |
sudo chown -R www-data:www-data /usr/local/nginx/projects | |
sudo chown -R www-data:www-data /usr/local/nginx/logs | |
sudo chown -R www-data:www-data /usr/local/nginx/sites-available/ | |
sudo chown -R www-data:www-data /usr/local/nginx/sites-enabled/ | |
# add user ubuntu to www-data group, then allow all member in group www-data can rwx | |
sudo usermod -aG www-data ubuntu | |
sudo chmod -R g+rwx /usr/local/nginx/projects/ | |
sudo chmod -R g+rwx /usr/local/nginx/sites-available/ | |
sudo chmod -R g+rwx /usr/local/nginx/sites-enabled/ | |
# remove default nginx.conf | |
sudo rm -f /usr/local/nginx/conf/nginx.conf | |
# create new nginx.conf that allow vhost | |
sudo echo 'user www-data; | |
worker_processes auto; | |
#error_log logs/error.log; | |
#error_log logs/error.log notice; | |
#error_log logs/error.log info; | |
#pid logs/nginx.pid; | |
events { | |
worker_connections 1024; | |
} | |
http { | |
include mime.types; | |
default_type application/octet-stream; | |
sendfile on; | |
#tcp_nopush on; | |
keepalive_timeout 65; | |
## | |
# Virtual Host Configs | |
## | |
include /usr/local/nginx/conf.d/*.conf; | |
include /usr/local/nginx/sites-enabled/*; | |
## | |
# Upload file Settings | |
## | |
client_body_in_file_only clean; | |
client_max_body_size 5M; | |
client_body_buffer_size 128k; | |
## | |
# Symlink | |
## | |
disable_symlinks off; | |
}' > /home/ubuntu/nginx.conf | |
sudo mv /home/ubuntu/nginx.conf /usr/local/nginx/conf/ | |
sudo chown root:root /usr/local/nginx/conf/nginx.conf | |
sudo chmod 0644 /usr/local/nginx/conf/nginx.conf | |
# Create vhost demo | |
sudo echo 'server { | |
listen 80; | |
#listen 443 ssl; | |
server_name localhost; | |
pagespeed on; | |
pagespeed FileCachePath /var/ngx_pagespeed_cache; | |
pagespeed CriticalImagesBeaconEnabled false; | |
location ~ ".pagespeed.([a-z].)?[a-z]{2}.[^.]{10}.[^.]+" { | |
add_header "" ""; | |
} | |
location ~ "^/pagespeed_static/" { } | |
location ~ "^/ngx_pagespeed_beacon$" { } | |
root projects/demo/webroot/; | |
index index.php index.html index.htm; | |
location / { | |
try_files $uri /index.php?$args; | |
} | |
#error_page 404 /404.html; | |
# redirect server error pages to the static page /50x.html | |
# | |
#error_page 500 502 503 504 /50x.html; | |
#location = /50x.html { | |
# root projects/demo/webroot/; | |
#} | |
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 | |
# | |
location ~ \.php$ { | |
fastcgi_pass 127.0.0.1:9000; | |
fastcgi_index index.php; | |
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; | |
include fastcgi_params; | |
} | |
}' > /home/ubuntu/demo | |
mv /home/ubuntu/demo /usr/local/nginx/sites-available/ | |
ln -s /usr/local/nginx/sites-available/demo /usr/local/nginx/sites-enabled/demo | |
# install demo cakephp project | |
composer create-project --prefer-dist cakephp/app /usr/local/nginx/projects/demo | |
# allow user in group www-data can wrx | |
sudo chmod -R u+wrx /usr/local/nginx/sites-available/ | |
sudo chmod -R u+wrx /usr/local/nginx/sites-enabled/ | |
sudo chmod -R u+wrx /usr/local/nginx/projects/ | |
# restart php5-fpm, nginx | |
sudo service php5-fpm restart | |
sudo service nginx restart | |
# Start nginx on boot | |
sudo update-rc.d nginx defaults |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
step 1: upload
install_nginx.sh
to/home/ubuntu/
step 2: ssh to server then execute
sudo chmod +x install_nginx.sh
step 3:
./install_nginx.sh
step 4: Composer required press
Enter
to generateSecurity.salt
Test pagespeed:
From your computer, open server address on browser then F12, choose Network tab.
If result have
x-page-speed:"1.11.33.2-0"
is ok.Next test cakephp: access your_server_address.example